SIDEBAR

Algorithms. Sort Selection (Slow Sorting)

0 comments
Jan 07 2018
Post's featued image.

#A FUNCTION TO GET SMALLEST VALUE FROM THE PASSING IN ARRAY

def findSmallest(arr):
    smallest_item = arr[0]
    smallest_index = 0
    for i in range(1, len(arr)):
        
        if arr[i]<smallest_item:
            smallest_item=arr[i]
            smallest_index = i
    return smallest_index     

#A FUNCTION FOR THE SORT SELECTION ALGORITHM

def selectionSort(arr):
    sortedArr = []
    for i in range(len(arr)):
        smallest = findSmallest(arr)
        sortedArr.append(arr.pop(smallest))
    return sortedArr

#DATA TO CHECK

array_to_sort = [40,22,7,33,6,62,9]

sortedArray = selectionSort(array_to_sort) 

print (sortedArray)
[6, 7, 9, 22, 33, 40, 62]

Submit a Comment