static void Main(string[] args) { InsertionSortDemo(); Console.ReadLine(); } static void InsertionSortDemo() { Random rnd = new Random(); int[] arr = new int[100]; for (int i = 0; i < 100; i++) { arr[i] = rnd.Next(0, 1000); } Console.WriteLine("Raw data:"); foreach (var a in arr) { Console.Write(a + " "); } InsertionSort(arr); Console.WriteLine(" After insertion sort:"); foreach(var a in arr) { Console.Write(a + " "); } } static void InsertionSort(int[] arr) { int inner, temp; for (int outer = 0; outer < arr.Length; outer++) { temp = arr[outer]; inner = outer; while (inner > 0 && arr[inner - 1] >= temp) { arr[inner] = arr[inner - 1]; inner--; } arr[inner] = temp; } } static void SelectSortDemo() { Random rnd = new Random(); int[] arr = new int[100]; for (int i = 0; i < 100; i++) { arr[i] = rnd.Next(0, 10000); } Console.WriteLine("Raw data:"); foreach (var a in arr) { Console.Write(a + " "); } SelectionSort(arr); Console.WriteLine(" After selection sort:"); foreach(var a in arr) { Console.Write(a + " "); } } public void SelectSort(int[] arr) { int min; for(int outer=0;outer<=arr.Length;outer++) { min = outer; for(int inner=outer+1;inner<=arr.Length;inner++) { if(arr[inner]<arr[min]) { min = inner; } } if(min!=outer) { int temp = arr[min]; arr[min] = arr[outer]; arr[outer] = temp; } } } static void SelectionSortDemo() { Random rnd = new Random(); int[] arr = new int[100]; for (int i = 0; i < 100; i++) { arr[i] = rnd.Next(0, 1000); } Console.WriteLine("Raw data:"); foreach (var a in arr) { Console.Write(a + " "); } int[] selectionArr = SelectionSort(arr); Console.WriteLine(" Selection sort:"); foreach(var a in selectionArr) { Console.Write(a + " "); } } static int[] SelectionSort(int[] arr) { int min = 0; for(int i=0;i<arr.Length-1;i++) { min = i; for(int j=i+1;j<arr.Length;j++) { if(arr[j]<arr[min]) { min = j; } } if (i != min) { int temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } } return arr; } static void BubbleDemo() { Random rnd = new Random(); int[] arr = new int[100]; for(int i=0;i<100;i++) { arr[i] = rnd.Next(0, 1000); } Console.WriteLine("Raw data:"); foreach(var a in arr) { Console.Write(a + " "); } int[] ascArr = BubbleSort(arr, true); Console.WriteLine(" Asc order:"); foreach(var a in ascArr) { Console.Write(a + " "); } int[] descArr = BubbleSort(arr, false); Console.WriteLine(" Desc order:"); foreach(var a in descArr) { Console.Write(a + " "); } } static int[] BubbleSort(int[] arr,bool isAsc) { if(arr==null && arr.Length==0) { return null; } for(int i=0;i<arr.Length;i++) { for(int j=i+1;j<arr.Length;j++) { //Ascending if(isAsc) { if(arr[i]>arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } //Descending else { if(arr[i]<arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } return arr; }