在伪代码中:
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.