选择排序,对于 Java [英] Selection Sort, For Java
本文介绍了选择排序,对于 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解此伪代码并将其实现到我的程序中.任何人都可以更好地解释它或向我展示代码的外观吗?谢谢.
I am having trouble understanding this pseudocode, and implementing it into my program. Can anybody explain it better or show me how the code would look? Thanks.
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
推荐答案
好吧,让我们将伪代码翻译成伪英语.
Well, let's translate the pseudo-code to pseudo-English.
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)
现在你的工作是用代码编写它.
Now it's your job to write it in code.
这篇关于选择排序,对于 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文