【问题标题】:Need help understanding Selection Sort Algorithm需要帮助理解选择排序算法
【发布时间】:2018-04-24 16:37:26
【问题描述】:

我昨天在课堂上被分配了这个作业,我以为我理解了选择排序的过程,但现在我有点不确定。我认为每次通过后左侧的数字都会排序,并且在右侧的所有数字都首先排序之前不会再次检查。

以下是说明和我的回答:

在每次选择排序之后显示结果数组 算法。如果算法在给定的通过之前停止,请离开 传递空白。

Original Array: 30 8 2 25 27 20 
PASS 1: 8 30 2 25 27 20 
PASS 2: 8 2 30 25 27 20 
PASS 3: 8 2 25 30 27 20 
PASS 4: 8 2 25 27 30 20 
PASS 5: 8 2 25 27 20 30

谁能告诉我我这样做是否正确?

【问题讨论】:

标签: java arrays sorting


【解决方案1】:

在伪代码中:

Repeat until no unsorted elements remain:
    Search the unsorted part of the data to find the smallest value
    Swap the smallest found value with the first element of the unsorted part

据此,您的数据列表将是...

Original Array: 30 8 2 25 27 20

P1: [2] 8 30 25 27 20 // smallest(2), swap this with the first value(30)

P2: [2 8] 30 25 27 20 // smallest(8), don't need to swap 

P3: [2 8 20] 25 27 30 // smallest(20), swap this with the first ele of unsorted list(30)

P4: [2 8 20 25] 27 30 // smallest(25), don't need to swap

P5: [2 8 20 25 27] 30 // smallest(27), don't need to swap

P6: [2 8 20 25 27 30] // smallest(30), don't need to swap... sorted!

不需要PASS 6,因为最后一个元素已经排序了。

观看来自 CS50 的视频(哈佛大学很好地解释了。):https://www.youtube.com/watch?v=3hH8kTHFw2A

【讨论】:

    【解决方案2】:

    很高兴你尝试过

    算法是:

    repeat (numOfElements - 1) times
    
      set the first unsorted element as the minimum
    
      for each of the unsorted elements
    
        if element < currentMinimum
    
          set element as new minimum
    
      swap minimum with first unsorted position
    

    输出将是:

    Pass 1 : 2 8 30 25 27 20
    Pass 2 : 2 8 30 25 27 20
    Pass 3 : 2 8 20 25 27 30
    Pass 4 : 2 8 20 25 27 30
    Pass 5 : 2 8 20 25 27 30
    

    您可以提供自定义输入,它会显示逐步输出: https://visualgo.net/bn/sorting

    希望这会有所帮助:D

    为你的学习干杯!

    【讨论】:

      【解决方案3】:

      我注意到 2 是数组中的最小元素。所以在第一遍中,它应该在数组的开头。请参考以下示例。

      示例 1: arr[] = 64 25 12 22 11

      // 查找 arr[0...4] 中的最小元素 // 并将其放在开头 11 25 12 22 64

      // 查找 arr[1...4] 中的最小元素 // 并将其放在 arr[1...4] 的开头 11 12 25 22 64

      // 查找 arr[2...4] 中的最小元素 // 并将其放在 arr[2...4] 的开头 11 12 22 25 64

      // 查找 arr[3...4] 中的最小元素 // 并将其放在 arr[3...4] 的开头 11 12 22 25 64

      参考 1:https://www.hackerearth.com/practice/algorithms/sorting/selection-sort/tutorial/

      参考 2:https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm

      【讨论】:

        【解决方案4】:

        如果您查看结果,应该很明显您没有正确完成。八不低于二! :)


        取第一项:30。找到最小值:2。交换它。

        PASS 1: 2 | 8 30 25 27 20 
        

        现在列表的第一部分已排序(用竖线表示)。

        采取下一项:8。找到最小值 - 8 实际上是最小值。不会发生交换。

        PASS 2: 2 8 | 30 25 27 20 
        

        取下一项:30。找到最小值:20。交换它。

        PASS 3: 2 8 20 | 25 27 30 
        

        取下一项:25。这是最小值。不会发生交换。

        PASS 4: 2 8 20 25 | 27 30 
        

        采取下一项:27。这是最小值。不会发生交换。

        PASS 5: 2 8 20 25 27 | 30 
        

        取下一项:30。这是最低要求。不会发生交换。

        列表现已排序。

        【讨论】:

          【解决方案5】:
          ### Step 1 − Set MIN to location 0 or any high Value
          ### Step 2 − Search the minimum element in the list
          ### Step 3 − Swap with value at index of minimum
          ### Step 4 − Repeat until iteration gets over
          <code>
          
              final int[] intArr = new int[]{14, 33, 27, 10, 35, 19, 42, 44};
              int index = -1;
              boolean isMin;
              for (int i = 0; i < intArr.length; i++) {
                int min = 999999999;
                isMin = false;
                for (int j = i + 1; j < intArr.length; j++) {
                  if (intArr[j] < min && intArr[i] >= intArr[j]) {
                    min = intArr[j];
                    index = j;
                    isMin = true;
                  }
                }
                if (isMin) {
                  intArr[index] = intArr[i];
                  intArr[i] = min;
                }
              }
              Arrays.stream(intArr).forEach(System.out::println);
          </code>
          

          【讨论】:

            猜你喜欢
            • 2021-03-22
            • 2019-05-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-11
            • 2020-01-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多