using System;
using System.Collections.Generic;
using System.Text;
namespace Con_Sort
{
struct Book
{
public Book(string author, int year)
{
this._year = year;
this._author = author;
}
private int _year;
public int Year
{
get { return this._year; }
set { this._year = value; }
}
private string _author;
public string Author
{
get { return this._author; }
set { this._author = value; }
}
public override string ToString()
{
return String.Format("Название книги :{0}, Год :{1}", Author, Year);
}
}
static class BubbleSort
{
public static void Sort<T>(T[] array, Comparison<T> comparison)
{
for (int i = array.Length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (comparison(array[j], array[j + 1]) > 0)
{
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
static class InsertSort
{
public static void Sort<T>(T[] array, Comparison<T> comparison)
{
int i, j, k;
for (i = 1; i < array.Length; i++)
{
T temp = array[i];
k = i;
for (j = i - 1; (comparison(array[j-1], temp) > 0)&&(j>=0); j--)
{
array[j + 1] = array[j];
}
array[j + 1] = temp;
}
}
}
static class selectSort
{
public static void Sort<T>(T[] array, Comparison<T> comparison)
{
int i, j, k;
for (i = 0; i < array.Length; i++)
{
T temp = array[i];
k = i;
for (j = i + 1; j < array.Length; j++) // цикл выбора наименьшего элемента
if ((comparison(array[j], temp) > 0))
{
k = j; temp = array[j]; // k - индекс наименьшего элемента
}
array[k] = array[i];
array[i] = temp; // меняем местами наименьший с a[i]
}
}
}
class Teenager
{
private static Random random = new Random();
private static readonly string[] messages = new string[6]
{
"Троесент!",
"Ватсон!",
"Фаронов!",
"Шилд!",
"Агуров!",
"Глинський!"
};
public static string Complain()
{
return messages[random.Next(messages.Length)];
}
}
class Program
{
static void PrintBookTitles(Book[] books)
{
foreach (Book book in books)
{
Console.WriteLine(book);
}
}
static void Main(string[] args)
{
Book[] books = new Book[10];
Random random = new Random();
for (int i = 0; i < books.Length; i++)
{
books[i] = new Book(Teenager.Complain(), random.Next(2007));
}
PrintBookTitles(books);
//Console.Read();
selectSort.Sort(books, delegate(Book a, Book b)
{
return a.Year.CompareTo(b.Year);
});
Console.WriteLine("После сортировки:");
PrintBookTitles(books);
Console.ReadLine();
}
}
}