using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SortingAlgos { class Program { static void Main(string[] args) { int[] arr1 = { 53, 10, 2, 62, 128, 93, 28, 18 }; InsertionSort(arr1); PrintArray(arr1); int[] arr2 = { 53, 10, 2, 62, 128, 93, 28, 18 }; SelectionSort(arr2); PrintArray(arr2); int[] arr3 = { 53, 10, 2, 62, 128, 93, 28, 18 }; BubbleSort(arr3); PrintArray(arr3); int[] arr4 = { 53, 10, 2, 62, 128, 93, 28, 18 }; SelectionSortMax(arr4); PrintArray(arr4); int[] arr5 = { 53, 10, 2, 62, 128, 93, 28, 18 }; BubbleSortMin(arr5); PrintArray(arr5); Console.ReadLine(); } static void PrintArray(int[] s) { for (int i = 0; i < s.Length; i++) { Console.Write("{0} ", s[i]); } Console.WriteLine(); } static void InsertionSort(int[] s) { int n = s.Length; for (int i = 1; i < n; i++) { int j; int temp = s[i]; // incoming element for (j = i; j > 0 && s[j - 1] > temp; j--) // adjust elements to the right { s[j] = s[j - 1]; } s[j] = temp; // insert incoming element } } // this version of selection sort repeatedly selects minimum element static void SelectionSort(int[] s) { int n = s.Length; for (int i = 0; i < n - 1; i++) { int lowIndex = i; for (int j = i + 1; j < n; j++) { if (s[j] < s[lowIndex]) { lowIndex = j; } } // swap s[i] and s[lowIndex] int temp = s[i]; s[i] = s[lowIndex]; s[lowIndex] = temp; } } // this version of bubble sort repeatedly positions maximum element static void BubbleSort(int[] s) { int n = s.Length; for (int i = n - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (s[j] > s[j + 1]) { // swap s[j] and s[j+1] int temp = s[j]; s[j] = s[j + 1]; s[j + 1] = temp; } } } } static void SelectionSortMax(int[] s) { // TODO: write a version of selection sort that repeatedly selects maximum element } static void BubbleSortMin(int[] s) { // TODO: write a version of bubble sort that repeatedly positions minimum element } } }