Last active
August 29, 2015 14:04
-
-
Save kaka2008/27ccffd5f8a9d40f8a1d to your computer and use it in GitHub Desktop.
分治法合并排序 ,将一个数组递归的分成两部分,然后将其合并,达到排序的目的。时间复杂度为log(2)n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 真正的插入排序<br/> | |
* 时间复杂度为log(2)n (2为底数) | |
* | |
* 在一个数据组中,递归的将其分成两部分,最后,将其合并 | |
* | |
* @author weizhankui | |
* | |
*/ | |
public class MergeSort{ | |
public static void mergeSort(int[] a, int p, int r) { | |
if (p >= r) { | |
return; | |
} | |
int q = (p + r) / 2; | |
mergeSort(a, p, q); | |
mergeSort(a, q + 1, r); | |
Merge.merge_asc(a, p, q, r); | |
} | |
public static void main(String[] args) { | |
int[] a = { 1, 9, 5, 7, 2, 3, 4, 6 }; | |
System.out.println("降序排列前:"); | |
printArray(a); | |
System.out.println("降序排列后:"); | |
mergeSort(a, 0, 7); | |
printArray(a); | |
} | |
public static void printArray(int[] a) { | |
for (int num : a) { | |
System.out.print(num + ","); | |
} | |
System.out.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment