- /*插入排序*/
- public static void insertionSort(int[] a)
- {
- int j;
- for(int p = 1; p < a.length; p++)
- {
- int tmp = a[p];
- for(j = p; j > 0 && tmp < a[j - 1]; j--)
- a[j] = a[j - 1];
- a[j] = tmp;
- }
- }
- //-----------------------------------------------------------
- /*希尔排序*/
- public static void shellsort(int[] a)
- {
- int j;
- for(int gap = a.length / 2; gap > 0; gap /= 2)
- for(int i = gap; i < a.length; i++)
- {
- int tmp = a[i];
- for(j = i; j >= gap && tmp < a[j - gap]; j-=gap)
- a[j] = a[j - gap];
- a[j] = tmp;
- }
- }
- //-----------------------------------------------------------
- /*归并排序*/
- private static void mergeSort(int []a, int []tmpArray, int left, int right)
- {
- if(left < right)
- {
- int center = (left + right) / 2;
- mergeSort(a, tmpArray, left, center);
- mergeSort(a, tmpArray, center + 1, right);
- merge(a, tmpArray, left, center + 1, right);
- }
- }
- public static void mergeSort(int []a)
- {
- mergeSort(a, tmpArray, 0, a.length - 1);
- }
- //该片段来自于http://www.codesnippet.cn/detail/1501201511539.html
来源: http://www.codesnippet.cn/detail/1501201511539.html