Algs4-2.1.38不同类型的元素
2.1.38不同类型的元素。编写一个测试用例,生成由多种数据类型元素组成的数组,元素的主键值随机,包括:
1)每个元素的主键均为String类型(至少长10个字符),并含一个double值;
2)每个元素的主键均为double类型,并含有10个String值(每个都至少长10个字符);
3)每个元素的主键均为int类型,并含有一个int[20]值
评估并验证这些输入数据对本节讨论的算法的性能影响。
答:希尔排序性能表现仍就很好。选择和插入对String型主键排序相对Double与Integer型主键花费时间约2至3倍。
1)String主键
![图片](http://r.photo.store.qq.com/psb?/f33bb27a-608a-4097-811a-86bbde18cf7a/NKmmv3MFLUFRFcqiOJk58edHFVattKBaJRuPNWh1g4k!/o/dPYAAAAAAAAA&ek=1&kp=1&pt=0&bo=uQF.ALkBfgADEDU!&tl=1&su=011760417&tm=1540602000&sce=0-12-12&rf=2-9)
2)Double主键
![图片](http://a1.qpic.cn/psb?/f33bb27a-608a-4097-811a-86bbde18cf7a/eUe6N7TnLZEp9YCF0ECeIPADDRsa5CAmGPlrHzCeTFM!/b/dCABAAAAAAAA&ek=1&kp=1&pt=0&bo=swFRALMBUQADEDU!&tl=1&su=030492961&tm=1540602000&sce=0-12-12&rf=2-9)
3)Integer主键
![图片](http://r.photo.store.qq.com/psb?/f33bb27a-608a-4097-811a-86bbde18cf7a/G9N4xQoyojBvmEANA2EdEiEeOGQfphK1nQQh399Gt4g!/o/dLAAAAAAAAAA&ek=1&kp=1&pt=0&bo=uAFMALgBTAADEDU!&tl=1&su=063477553&tm=1540602000&sce=0-12-12&rf=2-9)
import java.util.Arrays;
public class E2d1d38
{
/*
private static class Node implements Comparable<Node>
{
String key;
Double value;
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}*/
/*
private static class Node implements Comparable<Node>
{
Double key;
String value;
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}
*/
private static class Node implements Comparable<Node>
{
Integer key;
int[] value=new int[10];
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}
public static double time (String alg,Node[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.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;
Node[] a=new Node[N];
for (int t=0;t<T;t++)
{
for(int i=0;i<N;i++)
{
Node item= new Node();
item.key=StdRandom.uniform(-N,N);
for(int j=0;j<item.value.length;j++)
item.value[j]=StdRandom.uniform(-N,N);
a[i]=item;
/*StdOut.println("---"+a[i].key);
for(int j=0;j<item.value.length;j++)
StdOut.println(a[i].value[j]);
*/
}
total+=time(alg,a);
}
return total;
}
public static void main(String[] args)
{
Integer N=Integer.parseInt(args[0]);
Integer T=Integer.parseInt(args[1]);
Double time;
time=timeRandomInput("Insertion",N,T);
StdOut.printf("Insertion For %d items spend time=%.2f\n",N,time);
time =timeRandomInput("Selection",N,T);
StdOut.printf("Selection For %d items spend time=%.2f\n",N,time);
time =timeRandomInput("Shell",N,T);
StdOut.printf("Shell For %d items spend time=%.2f\n",N,time);
}
}
1)每个元素的主键均为String类型(至少长10个字符),并含一个double值;
2)每个元素的主键均为double类型,并含有10个String值(每个都至少长10个字符);
3)每个元素的主键均为int类型,并含有一个int[20]值
评估并验证这些输入数据对本节讨论的算法的性能影响。
答:希尔排序性能表现仍就很好。选择和插入对String型主键排序相对Double与Integer型主键花费时间约2至3倍。
1)String主键
2)Double主键
3)Integer主键
import java.util.Arrays;
public class E2d1d38
{
/*
private static class Node implements Comparable<Node>
{
String key;
Double value;
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}*/
/*
private static class Node implements Comparable<Node>
{
Double key;
String value;
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}
*/
private static class Node implements Comparable<Node>
{
Integer key;
int[] value=new int[10];
public int compareTo(Node other)
{
return key.compareTo(other.key);
}
}
public static double time (String alg,Node[] a)
{
Stopwatch timer =new Stopwatch();
if(alg.equals("Insertion")) Insertion.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;
Node[] a=new Node[N];
for (int t=0;t<T;t++)
{
for(int i=0;i<N;i++)
{
Node item= new Node();
item.key=StdRandom.uniform(-N,N);
for(int j=0;j<item.value.length;j++)
item.value[j]=StdRandom.uniform(-N,N);
a[i]=item;
/*StdOut.println("---"+a[i].key);
for(int j=0;j<item.value.length;j++)
StdOut.println(a[i].value[j]);
*/
}
total+=time(alg,a);
}
return total;
}
public static void main(String[] args)
{
Integer N=Integer.parseInt(args[0]);
Integer T=Integer.parseInt(args[1]);
Double time;
time=timeRandomInput("Insertion",N,T);
StdOut.printf("Insertion For %d items spend time=%.2f\n",N,time);
time =timeRandomInput("Selection",N,T);
StdOut.printf("Selection For %d items spend time=%.2f\n",N,time);
time =timeRandomInput("Shell",N,T);
StdOut.printf("Shell For %d items spend time=%.2f\n",N,time);
}
}