💻
Grokking Algorithm
  • Chapter 1: Big O notation & Binary Search
  • Chapter 2: Selection Sort
  • Chapter 3: Recursion
  • Chapter 4: QuickSort
  • Chapter 5: Hash Tables
  • Chapter 6: Breadth-first Search
  • Chapter 7: Dijkstra's Algorithm
  • Chapter 8: Greedy Algorithm
  • Chapter 9: Dynamic Programming
  • Chapter 10: K-nearest neighbors
  • Chapter 11: Next?
Powered by GitBook
On this page

Was this helpful?

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]
PreviousChapter 1: Big O notation & Binary SearchNextChapter 3: Recursion

Last updated 5 years ago

Was this helpful?