关于排序稳定性测试用例的问题
来源:3-2 不会忘记的5种手写排序:插入、选择、冒泡

老鼠不上树
2021-07-19
老师,对于一个对象序列,我们可以通过标识它的稳定性的属性来进行稳定性测试,但是对于一个整形数组,能标识它稳定性的只有它原来的数组下标,我能想到的测试方法只能是新建一个类来对数组中的整数进行包装
class StableInteger {
public int ele;
public int loc;
public StableInteger(int ele, int loc) {
this.ele = ele;
this.loc = loc;
}
}
public void sortStabilityTest(int[] A) {
StableInteger[] arr = new StableInteger[10];
Random random = new Random(1);
for (int i = 0; i < 10; i++) {
arr[i] = new StableInteger(random.nextInt(), i);
}
SelectionSort.sort(arr);
for (int i = 0, j = 1; i < j && j < arr.length && arr[i].ele==arr[j].ele; i++, j++) {
if (arr[i].loc > arr[j].loc) {
Assert.fail("Not stable!")
}
}
}
请问老师我的做法是否有问题?对一个普通的整形数组测试排序稳定性还有更简单的方法吗?
写回答
1回答
-
求老仙
2021-08-05
你那个stableinteger类型用pair替代就可以了。
00
相似问题