# python选择排序_Python选择排序

python选择排序

Here you’ll learn about python selection sort algorithm with program example.

Selection sort is one of the easiest sorting algorithm out there. In Selection sort, to sort an unsorted array, all we have to do is find the minimum in the array and swap it with the first element in the unsorted array. After each swapping increase the starting index of the array by one.

This is for sorting in ascending order. If we want to sort it into descending order find the maximum instead of minimum and replace with the first element in unsorted array.

## Python选择排序 (Python Selection Sort)

### 例(Example)

We have an unsorted array-

[4,8,19,2,28,21]

[4,8,19,2,28,21]

Step 1:

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

### 算法(Algorithm)

If we have an array of n elements.

Step 1:- set MIN = 0

Step 2:- find minimum element in array

If exists then swap with element at MIN

Step 3:- increase MIN by 1

Step 4:- go to Step 2 until  array is sorted (for n-1 times)

### 时间复杂度(Time Complexity)

• Best Case = O(n)2

最佳情况= O(n)2

• Average Case = O(n)2

平均情况= O(n)2

• Worst Case = O(n)2

最坏情况= O(n)2

Note: for larger arrays we never use selection sort.

### Python选择排序程序 (Python Selection Sort Program)

Below is the implementation of selection sort in python.

arr =[4,8,19,2,28,21]

min = 0    #set min = 0
n = len(arr)

while(min <= n-1):
s_i = min
while(s_i <= n-1):      #finding minimum
if (arr[s_i] < arr[min]):
arr[min],arr[s_i] = arr[s_i],arr[min]   #swapping element at start index with minimum
s_i = s_i+1

min = min+1

for element in arr:
print element

Output

2 4 8 19 21 28

2 4 8 19 21 28

Comment below if you have any queries related to above python selection sort tutorial.

python选择排序

• 0
点赞
• 0
评论
• 2
收藏
• 一键三连
• 扫一扫，分享海报

12-08 6051

03-28 472
05-21 5006
07-12 1073
10-24 425
05-15 3003
07-04 236
11-07 357
06-26 951
05-27 5153
08-10 354
04-21 73