Final Exam Flashcards
Descibe selection sort
At iteration 0, find the smallest element in the vect, swap it with the element at idx 0
At iteration 1, find the second smallest element, swap it with the element at idx 1
Repeat until sorted
Describe bubble sort
run thru elements from smallest idx to largest
At each iteration, if the element that your inspecting is greater than the next, swap
Describe insertion sort
At iteration i, make sure that the first i+1 values in the vector are sorted
What is the run time of bubble sort
O(n^2)
What is the run time of selection sort
O(n^2)
What is the run time of insertion sort
O(n^2). However, with a “nearly” sorted vector, this can be O(n)
What is the run time of sorting with the STL’s multiset
O(n log n)
Describe heap sort
Uses a priority queue to sort in place
What is the run time of heap sort
O(n log n)
Describe merge sort
Split the vector into two equal parts, and recursively sort both parts. Merge the two sorted sub vectors as the recursion returns
What is the run time of merge sort
O(n log n)
Describe quick sort
Partition the vector. Keep a left and a right pointer. Increment the left pointer until you have a value greater than or equal to the pivot, decrement the right pointer until you have a value less than or equal to the pivot. Swap the two elements, then repeat until sorted
What is the run time of quick sort
O(n log n)
Describe bucket sort
Approximate where we think each element should be in the vector. Once we have a vector that is almost sorted, use insertion sort to finish the job.
What is the run time of bucket sort
Average case: O(n)