1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortExamplesSelectionSort { class Program { static void Main(string[] args) { int Count = 10; Random r = new Random(); int[] a = new int[Count]; for (int i = 0; i < a.Length; i++) { a[i] = r.Next(1000); } //SelectionSort(ref a); //InsertionSort(ref a); //BubbleSort(ref a); shellSort2(ref a); foreach (int i in a) Console.WriteLine(i); Console.ReadLine(); } static void SelectionSort(ref int[] a) // complexity O(n^2) { for (int i = 0; i < a.Length; i++) { int min = i; // find min for (int j = i; j < a.Length; j++) { if (a[j] < a[min]) min = j; } // exchange int temp = a[i]; a[i] = a[min]; a[min] = temp; } } static void InsertionSort(ref int[] a) // complexity O(n^2) { for (int i = 0; i < a.Length; i++) { for (int j = i; j>0; j--) { if (a[j] < a[j - 1]) { // exchange int temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; } } } } static void shellSort2(ref int[] a) { int j; int step = a.Length / 2; while (step > 0) { for (int i = 0; i < (a.Length - step); i++) { j = i; while ((j >= 0) && (a[j] > a[j + step])) { int tmp = a[j]; a[j] = a[j + step]; a[j + step] = tmp; j -= step; } } step = step / 2; } } } } |
Сортировка пузырьком
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
static void BubbleSort(ref int[] a) // complexity O(n^2) { for (int i = 0; i < a.Length; i++) { for (int j = 0; j < a.Length-1; j++) { if (a[j] > a[j+1]) { // exchange int temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } } |