我在理解此伪代码并将其实现到我的程序时遇到麻烦。谁能更好地解释它,或者告诉我代码的外观?谢谢。
A - an array containing the list of numbers
numItems - the number of numbers in the list
for i = 0 to numItems - 1
for j = i+1 to numItems
if A[i] > A[j]
// Swap the entries
Temp = A[i]
A[i] = A[j]
A[j] = Temp
End If
Next j
Next i
最佳答案
好吧,让我们将伪代码翻译成伪英语。
A - an array containing the list of numbers
numItems - the number of numbers in the list
for i = 0 to numItems - 1
for j = i+1 to numItems
if A[i] > A[j]
// Swap the entries
Temp = A[i]
A[i] = A[j]
A[j] = Temp
End If
Next j
Next i
可能读为
Count through each item, from the beginning to the end, calling it X
While considering item X, count through each item after it, from just
after X to the end, calling it Y
If X is bigger than Y, swap the two, temporarily storing X in Temp
so it doesn't get lost when we copy Y into X. Copy Y into X, and then
Copy the temporarily stored old value of X (remember it is in Temp)
back into Y. Now the values of X and Y are swapped (so X is now smaller
than Y)
现在,用代码编写代码是您的工作。
关于java - 选择排序,对于Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10852522/