Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Sorting/selectionsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Intuition is to pick smallest element every times loop proceeds
"""

def selectionSort(arr):
"""
The selectionSort function implements the selection sort algorithm to sort an array in ascending
order.

:param arr: The parameter `arr` is a list of elements that you want to sort using the selection sort
algorithm

Time complexity: O(N^2), (where N = size of the array), for the best, worst, and average cases.
Space Complexity: O(1)
"""
n = len(arr)

for i in range(0,n-1):
min_idx = i
for j in range(i,n):
if arr[j] < arr[min_idx]:
min_idx = j
# at this point we have collected information which element is smallest (between i and n-1)and its index is stored in min_idx
arr[i], arr[min_idx] = arr[min_idx], arr[i]


temp = input("Enter numbers separated by a comma:\n").strip()
arr = [int(item) for item in temp.split(",")]
selectionSort(arr)
print(arr)