Java排序算法 归并排序

归并排序(Merge)是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。

归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。 将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。

归并排序算法稳定,数组需要O(n)的额外空间,链表需要O(log(n))的额外空间,时间复杂度为O(nlog(n)),算法不是自适应的,不需要对数据的随机读取。

工作原理:

1、申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列

2、设定两个指针,最初位置分别为两个已经排序序列的起始位置

3、比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置

4、重复步骤3直到某一指针达到序列尾

5、将另一序列剩下的所有元素直接复制到合并序列尾

代码实现:

  1. public<wbr></wbr>void<wbr>mergeSort(){<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  2. long[]<wbr>workSpace<wbr>=<wbr></wbr></wbr></wbr>new<wbr></wbr>long[nElems];<wbr><wbr><wbr></wbr></wbr></wbr>
  3. recMergeSort(workSpace,0,nElems-1);<wbr><wbr><wbr></wbr></wbr></wbr>
  4. }<wbr><wbr><wbr></wbr></wbr></wbr>
  5. private<wbr></wbr>void<wbr>recMergeSort(</wbr>long[]<wbr>workSpace,<wbr></wbr></wbr>int<wbr>lowerBound,<wbr></wbr></wbr>int<wbr>upperBound){<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  6. if(lowerBound<wbr>==<wbr>upperBound){<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  7. return;<wbr><wbr><wbr></wbr></wbr></wbr>
  8. }<wbr><wbr><wbr></wbr></wbr></wbr>
  9. else{<wbr><wbr><wbr></wbr></wbr></wbr>
  10. int<wbr>mid=(lowerBound+upperBound)/</wbr>2;<wbr><wbr><wbr></wbr></wbr></wbr>
  11. recMergeSort(workSpace,<wbr>lowerBound,<wbr>mid);<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  12. recMergeSort(workSpace,<wbr>mid+</wbr>1,<wbr>upperBound);<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  13. merge(workSpace,<wbr>lowerBound,<wbr>mid+</wbr></wbr>1,<wbr>upperBound);<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  14. }<wbr><wbr><wbr></wbr></wbr></wbr>
  15. }<wbr><wbr><wbr></wbr></wbr></wbr>
  16. private<wbr></wbr>void<wbr>merge(</wbr>long[]<wbr>workSpace,<wbr></wbr></wbr>int<wbr>lowPtr,<wbr></wbr></wbr>int<wbr>highPtr,<wbr></wbr></wbr>int<wbr>upperBound){<wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  17. int<wbr>j<wbr>=<wbr></wbr></wbr></wbr>0;<wbr><wbr><wbr></wbr></wbr></wbr>
  18. int<wbr>lowerBound<wbr>=<wbr>lowPtr;<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. int<wbr>mid<wbr>=<wbr>highPtr<wbr>-<wbr></wbr></wbr></wbr></wbr></wbr>1;<wbr><wbr><wbr></wbr></wbr></wbr>
  20. int<wbr>n<wbr>=<wbr>upperBound-lowerBound+</wbr></wbr></wbr>1;<wbr><wbr><wbr></wbr></wbr></wbr>
  21. while(lowPtr<=mid&&highPtr<=upperBound){<wbr><wbr><wbr></wbr></wbr></wbr>
  22. if(theArray[lowPtr]<theArray[highPtr]){<wbr><wbr><wbr></wbr></wbr></wbr>
  23. workSpace[j++]=theArray[lowPtr++];<wbr><wbr><wbr></wbr></wbr></wbr>
  24. }<wbr><wbr><wbr></wbr></wbr></wbr>
  25. else{<wbr><wbr><wbr></wbr></wbr></wbr>
  26. workSpace[j++]=theArray[highPtr++];<wbr><wbr><wbr></wbr></wbr></wbr>
  27. }<wbr><wbr><wbr></wbr></wbr></wbr>
  28. }<wbr><wbr><wbr></wbr></wbr></wbr>
  29. while(lowPtr<=mid){<wbr><wbr><wbr></wbr></wbr></wbr>
  30. workSpace[j++]<wbr>=<wbr>theArray[lowPtr++];<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  31. }<wbr><wbr><wbr></wbr></wbr></wbr>
  32. while(highPtr<=upperBound){<wbr><wbr><wbr></wbr></wbr></wbr>
  33. workSpace[j++]<wbr>=<wbr>theArray[highPtr++];<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  34. }<wbr><wbr><wbr></wbr></wbr></wbr>
  35. for(j=0;j<n;j++){<wbr><wbr><wbr></wbr></wbr></wbr>
  36. theArray[lowerBound+j]=workSpace[j];<wbr><wbr><wbr></wbr></wbr></wbr>
  37. }<wbr><wbr><wbr></wbr></wbr></wbr>
  38. }<wbr><wbr></wbr></wbr>

归并排序是比较稳定的排序.即相等的元素的顺序不会改变.如输入记录 1(1) 3(2) 2(3) 2(4) 5(5) (括号中是记录的关键字)时输出的 1(1) 2(3) 2(4) 3(2) 5(5) 中的2 和 2 是按输入的顺序.这对要排序数据包含多个信息而要按其中的某一个信息排序,要求其它信息尽量按输入的顺序排列时很重要.这也是它比快速排序优势的地方.

你可能感兴趣的:(Java排序)