Browse Source

SelectionSort

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
pull/5/head
Sunny Pate 6 years ago
parent
commit
83c55b8e46
1 changed files with 21 additions and 0 deletions
  1. +21
    -0
      sorting/selectionSort.py

+ 21
- 0
sorting/selectionSort.py View File

@ -0,0 +1,21 @@
# coding: utf-8
# In[50]:
alist = [54,26,93,17,77,31,44,55,20]
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
return alist
print(shortBubbleSort(alist))

Loading…
Cancel
Save