前面写了js的排序实现,总得玩玩java的哈。
同样,冒泡、选择、快速(这三个之前实现过也写过文章)、堆排序,然后做比较。
主要遇到的难点:
- -||想轻松点写个封装计时的逻辑,不想每调用一个排序就要写一个计时代码。想想,还是javascript写起来方便;
java的话,我想到的方法是写一个抽象类:抽象出排序方法,实现一个排序计时方法(该方法调用了抽象排序,但在先后排序时加入计时代码[感觉像是aop操作]);
接着所有排序类都继承这个抽象类,并实现排序方法,调用的时候直接调用继承的排序计时方法,这样就不用写多余的代码了。(不过这还搞继承,有点。。。不知道有啥高招呢?)
下面是展示各排序代码:
冒泡:
public class BubbleSort extends SortAbstract{ public static void main(String[] args) {
int[] a = {1,8,5,6,3,7,5,4,8,9,12,2};
new BubbleSort().sort(a);
for(int c : a){
System.out.println(c);
}
}
public void sort(int[] array){
if(array.length == 0)return ;
for(int i=0;i<array.length-1;i++){//i是计数标记
for(int j=0;j<array.length-i-1;j++){//注意终止条件的判断,冒泡的亮点在于从头到尾一对一对比较
if(array[j]>array[j+1]){
swap(array, j, j+1);
}
}
}
}
public static void swap(int[] array,int i,int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
选择:
public class ChooseSort extends SortAbstract{
public static void main(String[] args) {
int[] a = {1,8,5,6,3,7,5,4,8,9,12,2};
new ChooseSort().sort(a);
for(int c : a){
System.out.println(c);
}
} public void sort(int[] array){
if(array.length == 0)return ;
for(int i=0;i<array.length-1;i++){
for(int j=i+1;j<array.length;j++){
if(array[i]>array[j]){
swap(array, i, j);
}
}
}
} public static void swap(int[] array,int i,int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
快速:
public class QuickSort extends SortAbstract{
public static void qSort(int[] num,int low,int high){
if(low<high){
int pivotloc = partition(num,low,high);
qSort(num, low, pivotloc-1);
qSort(num, pivotloc+1, high);
}
}
public void sort(int[] array){
qSort(array, 0, array.length-1);
}
public static int partition(int[] num,int low,int high){
int mid = num[low];
int pivotkey = num[low];
while(low<high){
while(low<high&&num[high]>=pivotkey){
--high;
}
num[low] = num[high];
while(low<high&&num[low]<=pivotkey){
++low;
}
num[high]=num[low];
}
num[low] = mid;
return low;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num = {9,18,-8,-6,-57,5,62,0};
qSort(num, 0, num.length-1);
for(int i=0;i<num.length;i++){
System.out.println(num[i]);
}
} }
堆:
public class HeapSort extends SortAbstract {
public void sort(int[] array){
if(array.length<=1){
return;
}
int len = array.length;
for(int i=len/2+1;i>=0;i--){//初始最大堆 无序数组,由最右一个非子节点开始。这里len/2 +1 没想明白
maxHeapify(array, i, len);
}
for(int i = len-1; i>0; i-- ){
swap(array, 0, i); //每次将堆根节点与尾部交换,然后逻辑上数组放弃掉尾部,实际有点像尾部冒泡
maxHeapify(array, 0, --len); //从顶部开始顶堆调整
}
}
public static int getLeft(int index){
return index*2+1;
}
public static int getRight(int index){
return (index+1)*2;
}
public static void swap(int[] array, int i, int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void maxHeapify(int[] array, int index, int len){
if(array.length==0 || len<=1){
return;
}
int largest;
int left = getLeft(index);
int right = getRight(index);
if(left<len&&array[index]<array[left]){
largest = left;
}else{
largest = index;
}
if(right<len && array[right]>array[largest]){
largest = right;
}
if(largest!=index){
swap(array, largest, index); //交换两个位置的元素,并递归调用调整交换的孩子节点
maxHeapify(array, largest, len);
}
}
public static void main(String[] args){
int[] a = {1,8,5,6,3,7,5,4,8,9,12,2};
new HeapSort().sort(a);
for(int c : a){
System.out.println(c);
}
}
}
计时抽象类:
public abstract class SortAbstract {
public abstract void sort(int[] array);
public void runWithTimer(int[] array){
Date start = new Date();
this.sort(array);
Date end = new Date();
System.out.println("排序时间:(ms)"+(end.getTime()-start.getTime()));
}
}
测试用例:
public class SortTestMain {
public static void main(String[] args){
int[] small = {6,44,33,2,3,5,2,1,7,9,8,14};
BubbleSort bubbleSort = new BubbleSort();
ChooseSort chooseSort = new ChooseSort();
QuickSort quickSort = new QuickSort();
HeapSort heapSort = new HeapSort();
System.out.println("冒泡排序:");
//int[] smallUse = small.clone();
bubbleSort.runWithTimer(small.clone());
System.out.println("选择排序:");
chooseSort.runWithTimer(small.clone());
System.out.println("快速排序:");
quickSort.runWithTimer(small.clone());
System.out.println("堆排序:");
heapSort.runWithTimer(small.clone()); System.out.println("对a[10000]排序:");
int[] big = new int[10000];
for(int i=0; i<10000; i++){
big[i] = (int)Math.floor(Math.random()*100001);
}
System.out.println("冒泡排序:");
//int[] smallUse = small.clone();
bubbleSort.runWithTimer(big.clone());
System.out.println("选择排序:");
chooseSort.runWithTimer(big.clone());
System.out.println("快速排序:");
quickSort.runWithTimer(big.clone());
System.out.println("堆排序:");
heapSort.runWithTimer(big.clone());
}
}
测试结果:
结论:
几个元素的排序,基本很快。只有当数据开始多时,堆和快速开始展现出优势。