我正在嘗試創建自己的程式來對整數陣列進行選擇排序。我提出了以下程式,它適用于某些陣列,但不適用于其他陣列,例如這個。我一直在試圖追蹤問題,我認為這可能與我放置min = num [x];線路的位置有關。但是,我不確定應該將它移到哪里才能解決問題。有沒有人有什么建議?謝謝你。
ps 我在底部提供了一些我的測驗用例及其結果。
int [] num = {4, 9, 7, 6, 0, 1, 3, 5, 8, 2};
int min = num [0];
int temp;
int index = 0;
for (int x = 0; x < num.length; x )
{
min = num [x];
temp = num [x];
for (int y = x 1; y < num.length; y )
{
if (num [y] < min)
{
min = num [y];
index = y;
}
}
num [x] = min;
num [index] = temp;
min = num [x];
}
輸出測驗用例:
array: {4, 9, 7, 6, 0, 1, 3, 5, 8, 2}
result: [0, 1, 2, 3, 4, 4, 5, 7, 8, 8]
array: {7, 5, 8, 9, 1, 6, 3, 0, 2, 4}
result: [0, 1, 2, 3, 4, 5, 6, 7, 7, 8]
array: {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
result: [0, 1, 2, 3, 4, 9, 6, 7, 8, 9]
uj5u.com熱心網友回復:
有一個大問題和多個小問題,請參閱評論:
int [] num = {4, 9, 7, 6, 0, 1, 3, 5, 8, 2};
int min = num [0]; // you don't need this variable and assignment here, you can declare it inside loop
int temp; // the same for this variable
int index = 0; // the same here
for (int x = 0; x < num.length; x )
{
min = num [x];
temp = num [x];
// here you need to re-initialize index variable to some value, otherwise it could be some garbage from previous iterations
for (int y = x 1; y < num.length; y )
{
if (num [y] < min)
{
min = num [y];
index = y;
}
}
num [x] = min;
num [index] = temp;
min = num [x]; // this assignment does not make sense as you're overwriting it next iteration
}
你可以稍微簡化一下代碼:
for (int x = 0; x < num.length; x ) {
int index = x;
for (int y = x 1; y < num.length; y )
if (num[y] < num[index])
index = y;
int temp = num[x];
num[x] = num[index];
num[index] = temp;
}
uj5u.com熱心網友回復:
您撰寫的代碼存在一些問題:
最外層的回圈,即帶有 index 的回圈
x,應該從 0 迭代到num.length - 1,因為最內層的回圈從 開始x 1。如果您迭代直到num.length - 1包含,那么在最外層回圈的最后一次迭代期間,y將對應于num.length,這不是實際元素,實際上最內層回圈甚至不會開始。這將是一個無用的迭代。最內層回圈中的
if陳述句只需要保存要排序的剩余元素中新的最小元素的索引(從x到num.length-1)。您也不需要保存該值。您的交換部分有一個最終的額外任務,它沒有任何用處。此外,最好將所有分配保存在代碼的一個特定點中,以提高可讀性。此外,擁有三個臨時變數(
min和temp)index超出了您的實際需要。您只需要 2 個臨時變數 (minIndexandtemp),其中第一個存盤找到的新最小值的索引,而第二個用于在第 x 個元素和 position 中的元素之間的值交換期間使用minIndex。
這是帶有錯誤解釋和演算法邏輯的固定代碼。
public static void main(String[] args) {
int[] num = {4, 9, 7, 6, 0, 1, 3, 5, 8, 2};
int minIndex, temp;
//Your outermost loop should iterate till num.length - 1, since the innermost loop looks for any smaller element after x.
//The outermost loop is used to guarantee that at each iteration, the x-th element contains the smallest value among the ones left to sort.
for (int x = 0; x < num.length - 1; x ) {
//Assuming the x-th element is the smallest among the ones left to sort (from x to num.lenght-1)
minIndex = x;
//Looking for a smaller element than the x-th after the x position
for (int y = x 1; y < num.length; y ) {
//You only need to save the index of the new smallest element in minIndex.
//Once you've completed the innermost loop, then you'll swap the x-th element with the element in position minIndex
if (num[y] < num[minIndex]) {
minIndex = y;
}
}
//Swapping the x-th element with the minIndex-th element after the innermost loop has completed
temp = num[x];
num[x] = num[minIndex];
num[minIndex] = temp;
}
System.out.println(Arrays.toString(num));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482207.html
