标签:
一。 归并排序
O(nlogn)的时间复杂度的排序方法中,稳定的只有归并排序,堆排序和快速排序都是不稳定的。
在数组长度比较短的情况下,不进行递归,而是选择其他的排序方案,比如插入排序。
/** * Algorithm of mergeSort * @param nums */ public void mergeSort(int[] nums) { mergeSortHelp(nums, 0, nums.length-1); } public void mergeSortHelp(int[] nums, int low, int high) { if(low >= high) return; int mid = (low + high) / 2; mergeSortHelp(nums, low, mid); mergeSortHelp(nums, mid+1, high); merge(nums, low, mid, high); } public void merge(int[] nums, int low, int mid, int high) { int[] copy = new int[nums.length]; int s1 = low, s2 = mid+1; int t = low; //the index of copy while(s1 <= mid && s2 <= high) { if(nums[s1] <= nums[s2]) copy[t++] = nums[s1++]; else copy[t++] = nums[s2++]; } while(s1 <= mid) copy[t++] = nums[s1++]; while(s2 <= high) copy[t++] = nums[s2++]; for(int i=low; i<=high; i++) nums[i] = copy[i]; }
二。 外排序(External sorting)
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/little-YTMM/p/5515515.html