Skip to content

Commit

Permalink
Update selection sort.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Jul 2, 2022
1 parent 4f75f1b commit 9079b6d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions 03_Sorting-Algorithms/selection sort.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
a = [1,4,45,66,8,89,54,0,5,6,75,675,7,56]
# Selection Sort Algorithm

for i in range(len(a)-1):
m = min(a[i+1:])
mi = a.index(m)
if m < a[i]:
a[i], a[mi] = a[mi], a[i]
arr = [10, 16, 8, 12, 15, 3, 9, 5]

for i in range(len(arr)):
mi = i # index of minimum element in arr[i:]
for j in range(i+1, len(arr)):
if arr[mi] > arr[j]:
mi = j
# Swap the found minimum element with the first element
arr[i], arr[mi] = arr[mi], arr[i]

print(arr)

# Time: O(N^2)
# Space: O(1)

print(a)

0 comments on commit 9079b6d

Please sign in to comment.