前几种时间复杂度为O (n * n )的排序
选择排序
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 public static void SeletSort (int [] arr ) { if (arrs == null || arrs.Length < 2 ) return ; for (int i = 0 ; i < arr.Length;i++){ int newIndex = i; for (int j = i+1 ;j<arr.Length;j++){ newIndex = arr[j] < arr[newIndex] ? j:newIndex; } Swap(arr,i,newIndex); } } public static void Swap (int [] arr, int i, int j ) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } public static void PrintArray (int [] arrs ) { for (int i = 0 ; i < arrs.Length; i++) { Console.Write(arrs[i] + " " ); } Console.WriteLine(); }
冒泡排序
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 public static void PubbleSort (int [] arrs ) { if (arrs == null || arrs.Length < 2 ) return ; int N = arrs.Length; for (int end = N - 1 ; end >= 0 ; end--) { for (int i = 1 ; i <= end; i++) { if (arrs[i - 1 ] > arrs[i]) { Swap(arrs, i - 1 , i); } } } } public static void Swap (int [] arr, int i, int j ) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } public static void PrintArray (int [] arrs ) { for (int i = 0 ; i < arrs.Length; i++) { Console.Write(arrs[i] + " " ); } Console.WriteLine(); }
插入排序
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 public static void InsertSort1 (int [] arrs ) { if (arrs == null || arrs.Length < 2 ) return ; int N = arrs.Length; for (int i = 1 ;i < N;i++){ for (int j = i -1 ; j >=0 &&arrs[j] > arrs[j+1 ];j--) Swap(arrs, j, j + 1 ); } } public static void InsertSort1 (int [] arrs ) { if (arrs == null || arrs.Length < 2 ) return ; int N = arrs.Length; for (int end = 1 ; end < N; end++) { int newIndex = end; while (newIndex - 1 >= 0 && arrs[newIndex - 1 ] > arrs[newIndex]) { Swap(arrs, newIndex - 1 , newIndex); newIndex--; } } } public static void Swap (int [] arr, int i, int j ) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } public static void PrintArray (int [] arrs ) { for (int i = 0 ; i < arrs.Length; i++) { Console.Write(arrs[i] + " " ); } Console.WriteLine(); }
快速排序 荷兰国旗问题:是将一个包含 0、1、2 三种元素的数组,按照 0、1、2 的顺序进行排序(类似于荷兰国旗的三色排序)。
给定一个数组 nums,其中只包含 0、1、2,要求在不使用排序函数的情况下,原地(in-place) 将它们排序,使得所有 0在前,1在中间,2在后。
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 public static int [] nearestFlag (int [] arrs, int L, int R ){ if (L > R) { return new int [] { -1 , -1 }; } if (L == R) { return new int [] { L, R }; } int less = L - 1 ; int more = R; int index = L; while (index < more) { if (arrs[index] == arrs[R]) { index++; } else if (arrs[index] > arrs[R]) { Swap(arrs, index, --more); } else if (arrs[index] < arrs[R]) { Swap(arrs, index++, ++less); } } Swap(arrs, more, R); return new int [] { less + 1 , more }; } public static void Swap (int [] arr, int i, int j ){ int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; }
随机快速排序(递归) 使用随机数作为快速排序的基准值,遍历整个数组与基准值进行比较,小于的放在左侧,大于放在右侧。在第一次遍历后,分为了3个区间,小于区间,等于区间,大于区间。分别在大于以及小于区间进行同样的次数,直至数组有序。
首先
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 public static void nearestFlag (int [] arrs, int L, int R ){ if (L >= R) return ; int privotIndex = new Random().Next(L, R + 1 );Swap(arrs, privotIndex, R); int pivot = arrs[R];int less = L - 1 ; int more = R; int index = L;while (index < more){ if (arrs[index] < pivot) Swap(arrs, ++less, index++); else if (arrs[index] > pivot) Swap(arrs, --more, index); else index++; } Swap(arrs, more, R); nearestFlag(arrs, L, less); nearestFlag(arrs, more + 1 , R); } public static void Swap (int [] arr, int i, int j ){ int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; }