-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathSelection_sort.py
More file actions
28 lines (19 loc) · 1.15 KB
/
Copy pathSelection_sort.py
File metadata and controls
28 lines (19 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# SELECTION SORT
"""
In selection sort, you select an element and put that element into its right place.
1) Ascending Order: For every index, you can pick the min element out of the remaining unsorted part and swap with current index.
or
2) Descending Order: For every index, you can pick the max element out of the remaining unsorted part and swap with current index.
#Space Complexity: O(1)
#Time complexity: O(n^2) for both best case and worst case since for every pass the algo involves finding the max/min element.
USE CASE: It performs well on small lists/arrays.
"""
#sort the given array in ascending order
arr=[3,1,4,2,5]
n=len(arr)
for i in range(n-1): # we go upto n-2 index since if arr upto n-2 is sorted, it means whole array will be sorted.
min_el=min(arr[i:n]) #find min element from current index to last index
ind=arr.index(min_el) #find index of min element found
arr[ind],arr[i]=arr[i],arr[ind] #swap min index element with the curr index element
print(arr) #result is sorted array
#note: Selection sort is inplace sorting algorithm. It is not stable since the order is not maintained in case of elements having same value.