gpt4 book ai didi

java - 选择排序,对于 Java

转载 作者:行者123 更新时间:2023-12-03 02:28:32 24 4
gpt4 key购买 nike

我无法理解这段伪代码,也无法将其实现到我的程序中。有人可以更好地解释它或向我展示代码的外观吗?谢谢。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com