Sorting Algorithms Flashcards
What is the order of runtime from fastest to slowest?
1, log(n), n, nlog(n), n^2, 2^n, n!
What is the runtime (worst case and best case) of bubble sort?
Worst case: n^2, array is reversed
Best case: n, if data is sorted and the algorithm is optimized. Meaning there is a way to exit early.
What is the runtime (worst case and best case) of insertion sort?
Worst case: n^2, if array is reversed
Best case: n, if array is sorted
What is the runtime (worst case and best case) of selection sort?
n^2 for best and worst case. No mechanism to know when it’s done working so it looks through the whole list again and again
What are the non-comparison sorting algorithms? What is their runtime?
Counting sort
Radix sort
Both run in linear, n, time.
What are the divide and conquer algorithms?
Merge sort
Quick sort
What is the runtime (worst case and best case) of merge sort?
It is always nlog(n). The best case and worst case are nlog(n). No mechanism to figure out if it’s done working, so it will just keep splitting and working on the list. Needs extra space.
What is the runtime (worst case and best case) of quick sort?
Worst case: n^2
Best case: nlog(n)
The runtime is based on the position of the pivot used. Pivot should be somewhere in/near the middle to get the best case runtime.
What does merge sort need that quick sort does not need?
Merge sort needs needs extra space. So an extra array needs to be created.