标签:merge 技术分享 iss img public pareto %s photo cab
2.1.25不需要交换的插入排序。在插入排序的实现中使较大元素右移一位只需访问一次数组(而不用使用exch())。使用SortCompare来评估这种做法的效果。
答:交换的成本较大,所以不交换方式性能提升10%
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 Insertion3
{
public static void sort(Comparable[] a)
{
int N=a.length;
Comparable t;
for (int i=0;i<N;i++)
{
t=a[i];
int j;
for(j=i;j>0 && less(t,a[j-1]);j--)
a[j]=a[j-1];
a[j]=t;
}
}
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);
//
for (int i=0;i<N;i++)
StdOut.printf("%4f ",a[i]);
}
}
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 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\n %s is",N,alg1);
StdOut.printf(" %.2f times faster than %s\n",t2/t1,alg2);
}
}
标签:merge 技术分享 iss img public pareto %s photo cab
原文地址:https://www.cnblogs.com/longjin2018/p/9860047.html