Java 插入排序,比较次数
大家好,对于一个作业,我们必须计算许多算法的比较次数。我在使用Sedgewick&Wayne的《算法》一书中的代码。我不知道我的代码到底哪里错了。。。当我们要比较一些东西时,我计算我的比较Java 插入排序,比较次数,java,algorithm,sorting,insertion-sort,Java,Algorithm,Sorting,Insertion Sort,大家好,对于一个作业,我们必须计算许多算法的比较次数。我在使用Sedgewick&Wayne的《算法》一书中的代码。我不知道我的代码到底哪里错了。。。当我们要比较一些东西时,我计算我的比较 public long sort(Comparable[] a) { if (a == null) { throw new IllegalArgumentException("argument 'array' must not be null."); }
public long sort(Comparable[] a) {
if (a == null) {
throw new IllegalArgumentException("argument 'array' must not be null.");
}
int N = a.length;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
this.comparisons++;
if(less(a[j], a[j-1]))
exch(a, j, j-1);
}
assert isSorted(a, 0, i);
}
assert isSorted(a);
return this.comparisons;
}
这两个人应该是平等的
iSorted方法:
private boolean isSorted(Comparable[] a) {
System.out.println("here");
return isSorted(a, 0, a.length - 1);
}
// is the array sorted from a[lo] to a[hi]
private boolean isSorted(Comparable[] a, int lo, int hi) {
System.out.println("here1");
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
private boolean被排序(可比较的[]a){
System.out.println(“此处”);
返回被排序(a,0,a.length-1);
}
//数组是否从[lo]排序到[hi]
私有布尔值被排序(可比较的[]a,int-lo,int-hi){
System.out.println(“此处1”);
对于(int i=lo+1;i比较次数应该正好是N*(N-1)/2。可能您在其他地方弄乱了比较
字段,因此我建议使用局部变量:
public long sort(Comparable[] a) {
if (a == null) {
throw new IllegalArgumentException("argument 'array' must not be null.");
}
int N = a.length;
int comparisonsCount = 0; // use this instead
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
comparisonsCount++; // edit here
if(less(a[j], a[j-1]))
exch(a, j, j-1);
}
assert isSorted(a, 0, i);
}
assert isSorted(a);
return comparisonsCount; // and here
}
公共长排序(可比[]a){
如果(a==null){
抛出新的IllegalArgumentException(“参数'array'不能为null”);
}
int N=a.长度;
int comparisonCount=0;//请改用此值
对于(int i=0;i0;j--){
ComparisonCount++;//在此处编辑
if(小于(a[j],a[j-1]))
行政会议(a、j、j-1);
}
断言排序(a,0,i);
}
断言分类(a);
返回comparisoncount;//和此处
}
那么,你又遇到了什么错误?你想让你的代码进行插入排序,并返回它所做的比较次数?它目前在做什么?不,我没有收到错误,它排序得很好,但比较次数不正确…发布你的less
方法。我已经更新了部分。比较次数应该是多少ns be,以及您的代码报告了多少?以下是compare to的作用:将此对象与指定对象进行顺序比较。返回一个负整数、零或正整数,因为此对象小于、等于或大于指定对象。从您的角度看,这看起来是正常的。因为您正在比较下一个数字是否小于上一个数字。结果现在不同了,但仍然不好。但我开始质疑我必须使用的测试方法…我更新了部分…但是如果你使用局部变量而不是字段,你的比较计数变量nbCompares1
和nbCompares2
将相等是的,但我仍然得到两个不同的值,我真的我不明白你的isSorted()
method发布了它们,我也只是注释掉了,结果没有改变。
private boolean isSorted(Comparable[] a) {
System.out.println("here");
return isSorted(a, 0, a.length - 1);
}
// is the array sorted from a[lo] to a[hi]
private boolean isSorted(Comparable[] a, int lo, int hi) {
System.out.println("here1");
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
public long sort(Comparable[] a) {
if (a == null) {
throw new IllegalArgumentException("argument 'array' must not be null.");
}
int N = a.length;
int comparisonsCount = 0; // use this instead
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
comparisonsCount++; // edit here
if(less(a[j], a[j-1]))
exch(a, j, j-1);
}
assert isSorted(a, 0, i);
}
assert isSorted(a);
return comparisonsCount; // and here
}