-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61d7dfc
commit 6151629
Showing
1 changed file
with
22 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
# Python program for implementation of Selection Sort | ||
from typing import List, TypeVar | ||
# Python program for implementation of Selection | ||
# Sort | ||
import sys | ||
|
||
# Declare generic type for the sort function input | ||
T = TypeVar("T") | ||
|
||
# Traverse through all array elements | ||
for i in range(len(A)): | ||
|
||
# Find the minimum element in remaining | ||
# unsorted array | ||
min_idx = i | ||
for j in range(i+1, len(A)): | ||
if A[min_idx] > A[j]: | ||
min_idx = j | ||
|
||
# Swap the found minimum element with | ||
# the first element | ||
A[i], A[min_idx] = A[min_idx], A[i] | ||
|
||
# Driver code to test above | ||
|
||
# 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) | ||
A = [64, 25, 12, 22, 11] | ||
print ("Sorted array") | ||
for i in range(len(A)): | ||
print("%d" %A[i]), |