Week 4 Flashcards
What are the three simple algorithms?
Bubble sort, selection sort, insertion sort.
What are the two complex sorting algorithms.
Merge sort and quick sort.
What is the complexity for the best case of insertion sort?
The best case complexity is O(n)
What is the complexity for the average and worst case for insertion sort.
The worst case is O(n^2) and the average case is O(n^2).
What is the theoretical best running time we can achieve for a comparision based sort?
O(n log(n)) for the worst and average cases.
What is Quick Sort?
It is another divide and conquer based technique.
Choose one array element to be the pivot and partitions the array into 2 sub arrays such that,
* subarray 1 contains only elements less than or equal to the pivot
* Sub array 2 contains only elements greater than or equal to the pivot
* The pivot stays in place and is in its final position.
For quick sort, why is it inefficient if the pivot is the first or last element?
The algorithm will fare poorly when the array is nearly in sorted order.
Try to choose pivots that divide the array into nearly equal halves.
During quick sort what happens when we exchange the first or the last element?
During partitioning, the pivot is sometimes moved out of the way by exchanging with the first or last element.
To avoid index bound checks, the largest element is put into the last array position. This is done before the actual sort.
In quick sort do we care if we choose the partition as the first, last or middle? (Assume the data is random).
No since the data is random, the choice won’t matter.
When does the worst case occur in quick sort?
It occurs when the pivot is the largest or the smallest element.
Results in a sub array of size 0 and another of size n -1.
Complexity of O(n^2)
When does the best case occur for quick sort?
The best case occurs when the pivots always create equal sized subarrays. The complexity is O(nlog(n))
For quick sort what is the average case complexity?
The average case complexity is O(nlog(n)).
What are the advantages of quick sort vs merge sort?
Quick sort is normally the quickest algorthm, (unless the pivot is chosen as the smallest or largest).
Unlike merge sort, quick sort does not need extra memory.
Quick sort is not appropriate for an array that is less than 30 items. (In this case an insertion sort is better).
What is an internal sort?
Data is kept in primary memory.
What is an external sort?
Data is kept in secondary storage. Used when data is too large to be loaded to memory at once.
Not all elements are accessible.