Python Selection Sort

Document 对象参考手册Python3 Examples

Selection sort is a simple and intuitive sorting algorithm. Its working principle is as follows. First, find the minimum (or maximum) element in the unsorted sequence and place it at the beginning of the sorted sequence. Then, continue to find the minimum (or maximum) element from the remaining unsorted elements and place it at the end of the sorted sequence. And so on, until all elements have been sorted.

Example

import sys A = [64, 25, 12, 22, 11] for i in range(len(A)): min_idx = i for j in range(i+1, len(A)): if A[min_idx] > A[j]: min_idx = j A[i], A[min_idx] = A[min_idx], A[i] print ("Sorted array:") for i in range(len(A)): print("%d" %A[i]),

The output of the above code is:

排序后的数组:
11
12
22
25
64

Document 对象参考手册Python3 Examples

Other Extensions