几种常见的排序算法
2017-03-14 14:25
141 查看
交换两个对象(排序中会用到)
在排序的时候,需要实现IComparer接口,可以用在合适的地方。
private static void Interop_ExchangeValue<T>(ref T m, ref T n) { T Temp = m; m = n; n = Temp; }
插入排序
public static void InsertSort<T>(ref T[] array, IComparer<T> comparer) { if (array.Length > 0) { for (int i = 1; i < array.Length; i++) { int front = i - 1; int index = i; while (front >= 0) { if (comparer.Compare(array[index], array[front]) < 0) { Interop_ExchangeValue(ref array[index], ref array[front]); index = front; } front--; } } } }
选择排序
public static void SelectSort<T>(ref T[] array, IComparer<T> comparer) { if (array.Length > 0) { for (int i = 0; i < array.Length; i++) { T minValue = array[i]; int minIndex = i; for (int j = i; j < array.Length; j++) { if (comparer.Compare(minValue, array[j]) > 0) { minValue = array[j]; minIndex = j; } } Interop_ExchangeValue(ref array[i], ref array[minIndex]); } } }
冒泡排序
public static void BubbleSort<T>(ref T[] array, IComparer<T> comparer) { if (array.Length > 0) { for (int i = 0; i < array.Length - 1; i++) { for (int j = array.Length - 1; j > i; j--) { if (comparer.Compare(array[j - 1], array[j]) > 0) { Interop_ExchangeValue(ref array[j - 1], ref array[j]); } } } } }
双向冒泡排序
public static void BidirectionalBubleSort<T>(ref T[] array, IComparer<T> comparer) { if (array.Length > 0) { int end = array.Length; int start = -1; bool swapped = false; do { swapped = false; start++; end--; for (int j = start; j < end; j++) { if (comparer.Compare(array[j], array[j + 1]) > 0) { Interop_ExchangeValue(ref array[j], ref array[j + 1]); swapped = true; } } for (int j = end - 1; j >= start; j--) { if (comparer.Compare(array[j], array[j + 1]) > 0) { Interop_ExchangeValue(ref array[j], ref array[j + 1]); swapped = true; } } } while (start < end && swapped); } }
快速排序
public static void QuickSort<T>(ref T[] array, IComparer<T> comparer) { if (array.Length > 0) { Interop_QuickSort<T>(ref array, 0, array.Length - 1, comparer); } } private static void Interop_QuickSort<T>(ref T[] array, int left, int right, IComparer<T> comparer) { if (left < right) { T key = array[(left + right) / 2]; int i = left - 1; int j = right + 1; while (true) { while (comparer.Compare(array[++i], key) < 0) ; while (comparer.Compare(array[--j], key) > 0) ; if (i >= j) { break; } Interop_ExchangeValue(ref array[i], ref array[j]); } Interop_QuickSort(ref array, left, i - 1, comparer); Interop_QuickSort(ref array, j + 1, right, comparer); } }
堆排序
public static void HeapSort<T>(ref T[] array, IComparer<T> comparer) { for (int i = (array.Length - 1) / 2; i >= 0; i--) { Interop_HeapAdjust(ref array, i, array.Length - 1, comparer); } for (int i = array.Length - 1; i >= 1; i--) { Interop_ExchangeValue<T>(ref array[0], ref array[i]); Interop_HeapAdjust(ref array, 0, i - 1, comparer); } } private static void Interop_HeapAdjust<T>(ref T[] array, int index, int length, IComparer<T> comparer) { T Temp = array[index]; int j = index * 2 + 1; while (j <= length) { if (j < length) { if (comparer.Compare(array[j], array[j + 1]) < 0) { j = j + 1; } } if (comparer.Compare(Temp, array[j]) < 0) { array[index] = array[j]; index = j; j = 2 * index + 1; } else { j = length + 1; } } array[index] = Temp; }
在排序的时候,需要实现IComparer接口,可以用在合适的地方。
相关文章推荐
- 用Java实现几种常见的排序算法
- 用php实现几种常见的排序算法
- 几种常见的排序算法
- 用Java实现几种常见的排序算法
- 黑马程序员 知识点总结-Java中几种常见的排序算法
- 几种常见排序算法的比较与实现
- 用Java实现几种常见的排序算法
- 用php实现几种常见的排序算法
- Java之美[从菜鸟到高手演变]之常见的几种排序算法-插入、选择、冒泡、快排、堆排等 .
- 几种比较常见的排序算法
- 几种常见排序算法
- JavaScript版几种常见排序算法
- 几种常见的排序算法---选择排序
- 几种常见的排序算法对比
- 几种常见的排序算法,选择排序,冒泡排序,希尔排序,堆排序,快速排序,归并排序,基数排序的比较
- JavaScript中几种常见排序算法小结
- 几种常见简单排序算法
- 几种常见的排序算法概述
- 用Java实现几种常见的排序算法
- 几种常见的排序算法及实现(一)