Chapter 2: Selection Sort
Array
good at getting the address of an element
Linked List
linked lists are so much better at inserts
delete an element
Selection sort
Step 1: Find the the smallest element
Step 2: Use it on selection sort function
def findSmallest(arr): smallest = arr[0] //Stores the smallest value smallest_index = 0 //Stores the index of the smallest value for i in range(1,len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index print(findSmallest([5,3,6,2,10])) => 3 def selectionSort(arr): ///Sorts an array newArr = [] for i in range(len(arr)): smallest = findSmallest(arr) newArr.append(arr.pop(smallest)) //add it to the new array return newArr print selectionSort([5,3,6,2,10]) => [2, 3, 5, 6, 10]
Last updated
Was this helpful?