选择排序,对于 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10852522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:51:35  来源:igfitidea点击:

Selection Sort, For Java

javapseudocode

提问by King

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 

回答by Edwin Buck

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

which might read

这可能会读

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.

现在你的工作是用代码编写它。

回答by tskuzzy

The name of the algorithm tells you a whole lot about it. It first selectsthe smallest element of the list and puts it in the first spot. Then it selects the second and puts it in the second, etc.

算法的名称告诉你很多关于它的信息。它首先选择列表中最小的元素并将其放在第一个位置。然后它选择第二个并将其放入第二个,依此类推。

How do you find the smallest element? Well you look through the list, and if the element you're looking at is smaller than the element at the beginning of the list, swap it in.

你如何找到最小的元素?好吧,您查看列表,如果您正在查看的元素小于列表开头的元素,请将其换入。

How do you find the second smallest? You know that the smallest element is already in the first spot. So that means the second smallest must be the smallest element between the second element and the end of the list. So you go through and do the appropriate swaps again.

你如何找到第二小的?您知道最小的元素已经在第一个位置。所以这意味着第二个最小的元素必须是第二个元素和列表末尾之间的最小元素。所以你再次进行适当的交换。

Lather, rinse, and repeat.

起泡,冲洗,然后重复。

If you're wondering how elements are swapped. Just think about how you would do it by hand. You pick element 1 up and put it in a safe place, put element 2 in element 1's old spot, then put element 1 in element 2's old spot. The temporary variable used would represent the safe place you put element 1.

如果您想知道如何交换元素。想想你将如何手工完成。你拿起元素 1 并将其放在安全的地方,将元素 2 放在元素 1 的旧位置,然后将元素 1 放在元素 2 的旧位置。使用的临时变量将代表您放置元素 1 的安全位置。

Wikipediahas a nice article on it.

维基百科上有一篇不错的文章。

回答by ChadNC

If all you want to do is sort an array of ints into a specific order and your professor doesn't mind you using a utility class then Go Here.

如果您想做的只是将整数数组按特定顺序排序并且您的教授不介意您使用实用程序类,那么Go Here

However, if you are required to write the sorting algorithm yourself then Go Herethere are explanations and examples.

但是,如果您需要自己编写排序算法,那么Go Here有解释和示例。