Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

9. Implement Selection Sort

 def selectionSort(array):

n = len(array)

for i in range(n):

# Initially, assume the first element of the unsorted part as the minimum.

minimum = i

for j in range(i+1, n):

if (array[j] < array[minimum]):

# Update position of minimum element if a smaller element is found.

minimum = j

# Swap the minimum element with the first element of the unsorted part.

temp = array[i]

array[i] = array[minimum]

array[minimum] = temp

return array

# Driver code

array = [13, 4, 9, 5, 3, 16, 12]

print(“The sorted array is:”, selectionSort(array))

Post a Comment

0 Comments