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[] array, Comparison 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 VstavSort { public static void Sort1(T[] array, Comparison comparison) { int i, j; for (i = 0; i < array.Length - 1; i++) { T temp = array[i]; for (j = i - 1; (comparison(array[i-1], temp) > 0)&&(j<0); j--) { array[j + 1] = array[j]; } array[j + 1] = temp; } } } 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(); VstavSort.Sort1(books, delegate(Book a, Book b) { return a.Year.CompareTo(b.Year); }); Console.WriteLine("После сортировки:"); PrintBookTitles(books); Console.ReadLine(); } } }