1、int array = new int 6, 6, 5, 4, 3, 2, 1, 0 ;array = Sort(array);foreach (var item in array)Console.WriteLine(item.ToString();Console.ReadKey();static int Sort(int array)for (int i = 0; i array.Length; i+)for (int j = 0; j array.Length - i -1; j+)if (arrayj+1 arrayj)Swap(array, j, j+1);return array;s
2、tatic void Swap(int array, int firstIndex, int secondIndex)int preserve = arrayfirstIndex;arrayfirstIndex = arraysecondIndex;arraysecondIndex = preserve;4.2 简单选择排序array = SelectSort(array);static int SelectSort(int array)for (int j = i;if (arrayj arrayi)Swap(array, j, i);4.3 快速排序array = QuickSort(ar
3、ray, 0, array.Length - 1);static int QuickSort(int array,int start, int end)int low = start;int high = end;int mark = start;while (low arraymark)Swap(array, low, mark);mark = low;continue;elselow+;while (mark arrayhigh)Swap(array, high, mark);mark = high;high-;if (mark - start 1)QuickSort(array, sta
4、rt, mark-1);if (end - mark QuickSort(array, mark+1, end);4.4 堆排序int array = new int 5,6,7,8,2,3,4,6,5,4,3,2 ;array = HeapSort(array);public static void CreateHeap(int array, int lastIndex)for (int i = lastIndex; i 0; i-)if (arrayi arrayi / 2)Swap(array, i, i / 2);public static int HeapSort(int array
5、)for (int i = array.Length - 1;CreateHeap(array, i);Swap(array, 0, i);4.5 二分插入排序int array = new int 3,2,1,4,5,67,678678,234234,2,3,4,5,6,7,8,4 ;array = HalfInsertSort(array);public static int HalfInsertSort(int array)int start = 0;int end = i - 1;while (start = end)int middle = (start + end) / 2;if
6、(arraymiddle start; j-)arrayj = arrayj - 1;arraystart = insertValue;4.6 直接插入排序array = InsertSort(array);static int InsertSort(int array) i;if (arrayj 4.7 归并排序int array = new int 1,1,2,2,2,2,3,3,2,2,2,22222222,1,1,1,1,1,1,1,1,0,0,0,0,7;array = MergeSort(array, 0, array.Length - 1);public static int M
7、ergeSort(int array, int start, int end)int middle = start;if (start end)middle = (start + end) / 2;MergeSort(array, start, middle);MergeSort(array, middle + 1, end);Merge(array, start, end);public static void Merge(int array, int start, int end)Queue container = new Queue();int first = start;int sec
8、ond = middle + 1;while (first = middle & second if (arrayfirst arraysecond)container.Enqueue(arrayfirst+);container.Enqueue(arraysecond+);= middle)while (second 0) point; i +) j += point)for (int k = i; k arrayj) int insertValue=arrayj;for (int t = j; t k; t -= point) arrayt = arrayt - point;arrayk = insertValue;point = point / 2;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1