2.1.28相等的主键。对于主键仅可能取两种值的数组,评估和验证插入排序和选择排序的性能,假设两种主键值出现的概率相同。
试验结果:插入排序用时只需选择排序的1/4,主要原因是插入排序在将元素放入到合适的位置时与有序元素不需要进行全部的比较,而选择排序会需要将元素与所有未排序的元素全部比较。
1)使用相同元素个数的两个不同的0,1随机数组,得到的试验结果如下:
2)使用相同元素个数与元素值的0,1数组得到的测试结果如下:
public class Insertion
{
public static void sort(Comparable[] a)
{
int N=a.length;
for (int i=0;i<N;i++)
{
for(int j=i;j>0 && less(a[j],a[j-1]);j--)
exch(a,j,j-1);
}
}
private static boolean less(Comparable v,Comparable w)
{ return v.compareTo(w)<0;}
private static void exch(Comparable[] a,int i,int j)
{
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a)
{
for (int i=0;i<a.length;i++)
StdOut.print(a[i]+" ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{
for (int i=0;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int i=0;i<N;i++)
a[i]=StdRandom.uniform(0.0,N*1.0);
//
sort(a);
}
}
public class Selection
{
public static void sort(Comparable[] a)
{
int N=a.length;
for (int i=0;i<N;i++)
{
int min=i;
for (int j=i+1;j<N;j++)
if (less(a[j],a[min])) min=j;
exch(a,i,min);
//showAnimation(a);
}
}
private static boolean less(Comparable v,Comparable w)
{ return v.compareTo(w)<0;}
private static void exch(Comparable[] a,int i,int j)
{
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a)
{
for (int i=0;i<a.length;i++)
StdOut.print(a[i]+" ");
StdOut.println();
}
private static void showAnimation(Comparable[] a)
{
StdDraw.setXscale(0.0,a.length);
StdDraw.setYscale(0.0,a.length);
StdDraw.setPenRadius(0.005);
StdDraw.pause(100);
StdDraw.clear(StdDraw.GRAY);
StdDraw.setPenColor(StdDraw.BLACK);
for(int i=0;i<a.length;i++)
{
StdDraw.line(i*1.0,0.0,i*1.0,(Double)a[i]*1.0);
}
}
public static boolean isSorted(Comparable[] a)
{
for (int i=0;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int i=0;i<N;i++)
a[i]=StdRandom.uniform(0.0,N*1.0);
//
StdDraw.pause(5000);
sort(a);
}
}
//使用相同的0,1随机数组时
public class SortCompare
{
/*
public static double time (String alg,Double[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.sort(a);
//exercise2.1.24
if(alg.equals("Insertion2")) Insertion2.sort(a);
//exercise2.1.25
if(alg.equals("Insertion3")) Insertion3.sort(a);
//exercise2.1.26
if(alg.equals("Insertion4")) Insertion3.sort(a);
if(alg.equals("Selection")) Selection.sort(a);
if(alg.equals("Shell")) Shell.sort(a);
// if(alg.equals("Merge")) Merge.sort(a);
// if(alg.equals("Quick")) Quick.sort(a);
// if(alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static double timeRandomInput(String alg,int N,int T)
{
double total =0.0;
Double[] a=new Double[N];
for (int t=0;t<T;t++)
{
for (int i=0;i<N;i++)
a[i]=StdRandom.uniform();
total+=time(alg,a);
}
return total;
}//end timeRandomInput
*/
public static double time (String alg,Integer[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.sort(a);
//exercise2.1.24
if(alg.equals("Insertion2")) Insertion2.sort(a);
//exercise2.1.25
if(alg.equals("Insertion3")) Insertion3.sort(a);
//exercise2.1.26
if(alg.equals("Insertion4")) Insertion3.sort(a);
if(alg.equals("Selection")) Selection.sort(a);
if(alg.equals("Shell")) Shell.sort(a);
// if(alg.equals("Merge")) Merge.sort(a);
// if(alg.equals("Quick")) Quick.sort(a);
// if(alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static void main(String[] args)
{
/*
String alg1=args[0];
String alg2=args[1];
int N=Integer.parseInt(args[2]);
int T=Integer.parseInt(args[3]);
double t1=timeRandomInput(alg1,N,T);
double t2=timeRandomInput(alg2,N,T);
StdOut.printf("For %d random Doubles %s is",N,alg1);
StdOut.printf(" %.2f times faster than %s ",t2/t1,alg2);
*/
double prevT1=0.0;
double prevT2=0.0;
double prevT3=0.0;
double thisT1=0.0;
double thisT2=0.0;
double thisT3=0.0;
for (int i=128;i<1000000;i=2*i)
{
prevT1=thisT1;
prevT2=thisT2;
prevT3=thisT3;
//
Integer[] a=new Integer[i];
Integer[] aCopy=new Integer[i];
for (int j=0;j<i;j++)
{
a[j]=StdRandom.uniform(0,2);
aCopy[j]=a[j];
}
//
thisT1=time("Insertion",a);
thisT2=time("Selection",aCopy);
//thisT3=timeRandomInput("Shell",i,1);
//
StdOut.printf("---For %d random Doubles ",i);
StdOut.printf("Insertion use time=%.2f ,tihisTime/prevTime=%.2f ",thisT1,thisT1/prevT1);
StdOut.printf("Selection use time=%.2f ,tihisTime/prevTime=%.2f ",thisT2,thisT2/prevT2);
StdOut.printf("Insertion use time/Selection use time=%.2f ",thisT1/thisT2);
// StdOut.printf("Shell use time=%.2f ,tihisTime/prevTime=%.2f ",thisT3,thisT3/prevT3);
}
}
}
试验结果:插入排序用时只需选择排序的1/4,主要原因是插入排序在将元素放入到合适的位置时与有序元素不需要进行全部的比较,而选择排序会需要将元素与所有未排序的元素全部比较。
1)使用相同元素个数的两个不同的0,1随机数组,得到的试验结果如下:
2)使用相同元素个数与元素值的0,1数组得到的测试结果如下:
public class Insertion
{
public static void sort(Comparable[] a)
{
int N=a.length;
for (int i=0;i<N;i++)
{
for(int j=i;j>0 && less(a[j],a[j-1]);j--)
exch(a,j,j-1);
}
}
private static boolean less(Comparable v,Comparable w)
{ return v.compareTo(w)<0;}
private static void exch(Comparable[] a,int i,int j)
{
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a)
{
for (int i=0;i<a.length;i++)
StdOut.print(a[i]+" ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{
for (int i=0;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int i=0;i<N;i++)
a[i]=StdRandom.uniform(0.0,N*1.0);
//
sort(a);
}
}
public class Selection
{
public static void sort(Comparable[] a)
{
int N=a.length;
for (int i=0;i<N;i++)
{
int min=i;
for (int j=i+1;j<N;j++)
if (less(a[j],a[min])) min=j;
exch(a,i,min);
//showAnimation(a);
}
}
private static boolean less(Comparable v,Comparable w)
{ return v.compareTo(w)<0;}
private static void exch(Comparable[] a,int i,int j)
{
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a)
{
for (int i=0;i<a.length;i++)
StdOut.print(a[i]+" ");
StdOut.println();
}
private static void showAnimation(Comparable[] a)
{
StdDraw.setXscale(0.0,a.length);
StdDraw.setYscale(0.0,a.length);
StdDraw.setPenRadius(0.005);
StdDraw.pause(100);
StdDraw.clear(StdDraw.GRAY);
StdDraw.setPenColor(StdDraw.BLACK);
for(int i=0;i<a.length;i++)
{
StdDraw.line(i*1.0,0.0,i*1.0,(Double)a[i]*1.0);
}
}
public static boolean isSorted(Comparable[] a)
{
for (int i=0;i<a.length;i++)
if(less(a[i],a[i-1])) return false;
return true;
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
Double[] a=new Double[N];
for(int i=0;i<N;i++)
a[i]=StdRandom.uniform(0.0,N*1.0);
//
StdDraw.pause(5000);
sort(a);
}
}
//使用相同的0,1随机数组时
public class SortCompare
{
/*
public static double time (String alg,Double[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.sort(a);
//exercise2.1.24
if(alg.equals("Insertion2")) Insertion2.sort(a);
//exercise2.1.25
if(alg.equals("Insertion3")) Insertion3.sort(a);
//exercise2.1.26
if(alg.equals("Insertion4")) Insertion3.sort(a);
if(alg.equals("Selection")) Selection.sort(a);
if(alg.equals("Shell")) Shell.sort(a);
// if(alg.equals("Merge")) Merge.sort(a);
// if(alg.equals("Quick")) Quick.sort(a);
// if(alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static double timeRandomInput(String alg,int N,int T)
{
double total =0.0;
Double[] a=new Double[N];
for (int t=0;t<T;t++)
{
for (int i=0;i<N;i++)
a[i]=StdRandom.uniform();
total+=time(alg,a);
}
return total;
}//end timeRandomInput
*/
public static double time (String alg,Integer[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.sort(a);
//exercise2.1.24
if(alg.equals("Insertion2")) Insertion2.sort(a);
//exercise2.1.25
if(alg.equals("Insertion3")) Insertion3.sort(a);
//exercise2.1.26
if(alg.equals("Insertion4")) Insertion3.sort(a);
if(alg.equals("Selection")) Selection.sort(a);
if(alg.equals("Shell")) Shell.sort(a);
// if(alg.equals("Merge")) Merge.sort(a);
// if(alg.equals("Quick")) Quick.sort(a);
// if(alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static void main(String[] args)
{
/*
String alg1=args[0];
String alg2=args[1];
int N=Integer.parseInt(args[2]);
int T=Integer.parseInt(args[3]);
double t1=timeRandomInput(alg1,N,T);
double t2=timeRandomInput(alg2,N,T);
StdOut.printf("For %d random Doubles %s is",N,alg1);
StdOut.printf(" %.2f times faster than %s ",t2/t1,alg2);
*/
double prevT1=0.0;
double prevT2=0.0;
double prevT3=0.0;
double thisT1=0.0;
double thisT2=0.0;
double thisT3=0.0;
for (int i=128;i<1000000;i=2*i)
{
prevT1=thisT1;
prevT2=thisT2;
prevT3=thisT3;
//
Integer[] a=new Integer[i];
Integer[] aCopy=new Integer[i];
for (int j=0;j<i;j++)
{
a[j]=StdRandom.uniform(0,2);
aCopy[j]=a[j];
}
//
thisT1=time("Insertion",a);
thisT2=time("Selection",aCopy);
//thisT3=timeRandomInput("Shell",i,1);
//
StdOut.printf("---For %d random Doubles ",i);
StdOut.printf("Insertion use time=%.2f ,tihisTime/prevTime=%.2f ",thisT1,thisT1/prevT1);
StdOut.printf("Selection use time=%.2f ,tihisTime/prevTime=%.2f ",thisT2,thisT2/prevT2);
StdOut.printf("Insertion use time/Selection use time=%.2f ",thisT1/thisT2);
// StdOut.printf("Shell use time=%.2f ,tihisTime/prevTime=%.2f ",thisT3,thisT3/prevT3);
}
}
}