-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSelectionSort.py
More file actions
28 lines (22 loc) · 768 Bytes
/
SelectionSort.py
File metadata and controls
28 lines (22 loc) · 768 Bytes
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
# Python program for implementation of Selection Sort
from typing import List, TypeVar
# Declare generic type for the sort function input
T = TypeVar("T")
# Function to do selection sort
def selection_sort(arr: List[T]) -> None:
# Traverse through all array elements
for i in range(len(arr)):
# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i + 1, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j
# Swap the found minimum element with
# the first element
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# Driver code to test above
if __name__ == "__main__":
a = [64, 25, 12, 22, 11]
selection_sort(a)
print("Sorted array", a)