闲来没事把以前的排序算法有拿出来 弄了个例子。以后忘记了 回头看看
以前没有做过测试 这次顺便测试了一下 快速排序果真是快啊。 本机10W数据 冒泡28s 快速 15毫秒。。差的好多。
package mathTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Shor {
public static void main(String[] args) {
Shor shor=new Shor();
for(int e=0;e<5;e++){
double data[]=new double[100000];
double data2[]=new double[100000];
double data3[]=new double[100000];
double data4[]=new double[100000];
for(int i=0;i<100000;i++){
data[i]=Math.random()*100;
data2[i]=Math.random()*100;
data3[i]=Math.random()*100;
data4[i]=Math.random()*100;
}
Date date4=new Date();
shor.quickSort(data4);
System.out.println("快速排序"+(new Date().getTime()-date4.getTime()));
Date date3=new Date();
shor.bubbleSort(data3);
System.out.println("冒泡排序"+(new Date().getTime()-date3.getTime()));
Date date2=new Date();
shor.insertionSort(data2);
System.out.println("插入排序"+(new Date().getTime()-date2.getTime()));
Date date=new Date();
List<Integer> hList=shor.getH(data.length);
shor.doShort(data, hList);
System.out.println("希尔排序:"+(new Date().getTime()-date.getTime()));
}
}
//快速
public void quickSort(double[] data){
doQicksort(data, 0, data.length-1);
}
public void doQicksort(double[] data,int left,int right){
int center=getPartition(data, left, right);
double partiton=data[center];
if(right-left>2){
int leftPos=left+1;
int rightPos=right-1;
while(leftPos<rightPos){
while(data[leftPos]<partiton){
leftPos++;
}
while(data[rightPos]>partiton){
rightPos--;
}
if(leftPos<rightPos){
exchange(data, leftPos, rightPos);
}
}
doQicksort(data, left, leftPos-1);
doQicksort(data, leftPos+1, right);
}
}
//这个方法不仅获取划分值。也给三个(或者小于3个)排序了
//获取的是划分值的坐标 不是值
public int getPartition(double[] data,int left,int right){
int length=right-left+1;
int center=left+length/2;
if(data[left]>data[center]){
exchange(data, left, center);
}
if(data[center]>data[right]){
exchange(data, center, right);
}
if(data[left]>data[center]){
exchange(data, left, center);
}
if(length>3){
doWhileExchange(data,center);
}
return center;
}
public void doWhileExchange(double[] data,int center){
int leftPos=0;
}
//交换
public void exchange(double[] data,int left,int right){
double x=data[left];
data[left]=data[right];
data[right]=x;
}
//冒泡
public void bubbleSort(double[] data){
for(int i=0;i<data.length-1;i++){
for(int j=0;j<data.length-i-1;j++){
double x;
if(data[j]>data[j+1]){
exchange(data, j, j+1);
// x=data[j];
// data[j]=data[j+1];
// data[j+1]=x;
}
}
}
}
//插入排序
public void insertionSort(double[] data){
shellSort(data,1);
}
//希尔排序
public void doShort(double[] data,List<Integer> hList){
for(Integer h:hList){
shellSort(data, h);
}
}
public List<Integer> getH(double n){
List<Integer> hList=new ArrayList<Integer>();
int h=1;
hList.add(h);
while((h=3*h+1)<n){
hList.add(0,h);
}
return hList;
}
public void shellSort(double[] data,int h){
for(int i=0;i<data.length;i+=h){
double shorData=data[i];
int index=i-h;
while(index>=0&&shorData<data[index]){
data[index+h]=data[index];
index-=h;
}
data[index+h]=shorData;
// for(int j=i-h;i>0;j-=h){
// if(shorData<data[i]){
// data[index]=data[j];
// }else{
// data[j]=shorData;
// }
// }
}
}
}