Java排序算法(二)_希尔排序、快速排序、归并排序(二)
2014-11-24 07:36:57
·
作者:
·
浏览: 1
nt(排序前:);
int[] array = {3,5,1,6,2,8,7,4,9};
for(int i:array) {
System.out.print(i + );
}
System.out.print(
排序后:);
QuickSort quickSort = new QuickSort();
quickSort.sort(array, 0, array.length-1);
for(int i:array) {
System.out.print(i + );
}
//2.测试归并排序
System.out.println(
--归并排序--);
System.out.print(排序前:);
int[] array2 = {3,6,1,2,8,7,9,5,4};
for(int i:array2) {
System.out.print(i + );
}
System.out.print(
排序后:);
MergeSort mergeSort = new MergeSort();
mergeSort.sort(array2, 0, 8);
for(int i:array2) {
System.out.print(i + );
}
//3.测试希尔排序
System.out.println(
--希尔排序--);
System.out.print(排序前:);
int[] array3 = {4,7,2,1,8,9,3,6,5};
for(int i:array3) {
System.out.print(i + );
}
System.out.print(
排序后:);
ShellSort shellSort = new ShellSort();
shellSort.sort(array3, 3);
for(int i:array3) {
System.out.print(i + );
}
}
}
运行结果:
--快速排序--
排序前:3 5 1 6 2 8 7 4 9
排序后:1 2 3 4 5 6 7 8 9
--归并排序--
排序前:3 6 1 2 8 7 9 5 4
排序后:1 2 3 4 5 6 7 8 9
--希尔排序--
排序前:4 7 2 1 8 9 3 6 5
排序后:1 2 3 4 5 6 7 8 9
以上三种排序有个共同点,都是比较型的排序算法,按照算法导论公开课中讲到的凡是比较型的排序算法其运行时间不可能低于theta(nlgn),视频中给出了证明,这里不再赘述。具体可查看公开课的第五章—线性时间排序。