0% found this document useful (0 votes)
41 views1 page

Selection Sort Algorithm in Python

This Python code defines a selection sort function that takes an array as a parameter, iterates through the array to find the minimum value and swaps it into the current sorting position, returning the sorted array; it then prompts the user for array size and values, calls the selection sort function, and prints the unsorted and sorted arrays.

Uploaded by

03rajput.ki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views1 page

Selection Sort Algorithm in Python

This Python code defines a selection sort function that takes an array as a parameter, iterates through the array to find the minimum value and swaps it into the current sorting position, returning the sorted array; it then prompts the user for array size and values, calls the selection sort function, and prints the unsorted and sorted arrays.

Uploaded by

03rajput.ki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

def selection_sort(arr):

for i in range(len(arr)):

min_index = i
for j in range(i+1,len(arr)):
if arr[j]< arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]

return arr

n = int(input("enter number of elements in array: "))


arr = []

for _ in range(n):
element = int(input())
arr.append(element)

print("unsorted array is: ", arr)


sorted_arr = selection_sort(arr)
print("sorted array is: ", sorted_arr)

You might also like