Skip to content

Commit

Permalink
Edited python Selection Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushbhatt2000 committed Oct 15, 2020
1 parent 61d7dfc commit 6151629
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions Python/SelectionSort.py
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]),

0 comments on commit 6151629

Please sign in to comment.