Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection sort algorith using python programming #971

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions Python/SelectionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def selection_sort(arr):
# Get the length of the array
n = len(arr)

# Loop through the array
for i in range(n):
# Assume the current index is the minimum
min_idx = i

# Loop through the unsorted part of the array
for j in range(i+1, n):
# If a smaller element is found, update the minimum index
if arr[j] < arr[min_idx]:
min_idx = j

# Swap the minimum element with the first element of the unsorted part
arr[i], arr[min_idx] = arr[min_idx], arr[i]

# Return the sorted array
return arr