我对这个练习题很困惑。问题是
在选择排序算法的最外层循环的前3次传递之后,写下数组元素的状态。
int[] numbers = {45, 78, 89, 34, 23, 12, 67, 56};
selectionSort(numbers);
after pass 1:
after pass 2:
after pass 3:
答案 0 :(得分:0)
我不会展示你的确切例子,因为我认为这是家庭作业,但我会告诉你另一个例子:
首先是选择排序的psudo代码:
repeat (numOfElements - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
示例:强>
int[] numbers = {8, 23, 7, 34, 50, 15, 16};
selectionSort(numbers);
将8设置为当前最小值,然后遍历剩余的未排序元素以找到真正的最小值。
用第一个未排序的元素(8)交换最小值(7)。
after pass 1: {7, 23, 8, 34, 50, 15, 16}
用第一个未排序的元素(23)交换最小值(8)。
after pass 2: {7, 8, 23, 34, 50, 15, 16}
用第一个未排序的元素(23)交换最小值(15)。
after pass 3: {7, 8, 15, 34, 50, 23, 16}
用第一个未排序的元素(34)交换最小值(16)。
after pass 4: {7, 8, 15, 16, 50, 23, 34}
将第一个未分类元素(50)与最小值(23)交换。
after pass 5: {7, 8, 15, 16, 23, 50, 34}
用第一个未排序的元素(50)交换最小值(34)。
after pass 6: {7, 8, 15, 16, 23, 34, 50}
排序!