Sorting Flashcards
when does insertion sort run in linear time?
when the number of inversions is order of N
This is the algorithm of choice for up to moderately large input.
Shell sort
What is the mergeSort caller algorithm?
- mergeSort(array [] a)
- Array [] b = new Array[a.length];
- Mergesort(a, b, 0, a.length-1);
Derive the runtime analysis of Mergesort
The number of comparisons is nearly optimal for these two algorithms.
heapsort, mergesort
What is the recurrence analysis of Mergesort?
T(N) = 2T(N/2) + N
Describe the partitioning algorithm for quicksort
- The first step of the partitioning strategy gets the pivot elements out of the way by swapping it with the last element.
- I starts at the first element and j starts at the second to last element.
- While I is to the left of j, we move I right, skipping over elements that are smaller than the pivot.
- We move j left, skipping over elements that are larger than the pivot.
- When I and j have stopped, I is pointing at a large element and j is pointing at a small element (relative to the pivot).
- If I is to the left of j, those elements are swapped.
- We repeat this process until I and j cross.
- The final part of the partitioning is to swap the pivot element with the element pointed to by i.
what is the form of sedgewicks sequence?
9*4^i -9*2^i + 1, this is most easily implemented using an array.
Analysis of recursive algorithms always requires what?
Solving a recurrence relation
What is the recurrence relation for quicksort?
T(N) = T(i) +T(N-i-1) + cN, where i equal the size of the first partition
How many comparisons are used by heapsort in the absolute worst case?
2NlogN-O(N)
What is the worst possible choice for a pivot and why?
The first and last element, because if the input is already sorted, quicksort will take quadratic time to do nothing
Quicksort is commonly used in what language?
C++
Describe Radix sort
Radix sort assumes we know the range of integers to be sorted.
We choose a radix, say r = 10.
We create r temporary lists.
In the first pass we go through the original list, copying values into the linked lists, based on their least significant digit.
We concatenate the temporary lists and restart the process based on the second digit, and so on.
What is the worst case runtime and average run time of quickselect?
O(N^2) and O(N)
What is the merge algorithm?
- Merge(array[] a, array[] b, int left, int rightPos, int rightEnd)
- Int leftEnd = rightPos -1;
- Int bcounter = left;
- Int numElements = rightEnd - left + 1;
- While(leftPos <= leftEnd && rightPos <= rightEnd)
- If(a[leftPos] < a[rightPos]) b[bcounter++] = a[leftPos++];
- Else b[bcounter++] = a[rightPos++];
- While(leftPos<= leftEnd) b[bcounter++] = a[leftPos++];
- While(rightPos<= rightEnd) b[bcounter++] = a[rightPos++];
- For(int i = 0; i < numElements; i++, rightEnd–) a[rightEnd] = b[rightEnd];
What is the mergesort algorithm?
- Mergesort(array [] a, tempArray b[], int left, int right)
- If(left < right) // this is the base case
- Int center = (left+right)/2;
- mergeSort(a, b, left, center)
- mergeSort(a, b, center+1, right)
- Merge(a, b, left, center+1, right)
- If(left < right) // this is the base case
What is external sorting?
sorting that takes place on external memory because the items are too numerous to fit in main memory.
Using a priority queue, we can find the kth largest number in what time?
O(N + klogN)
Prove that the comparisons needed for any comparison based sorting algorithm is NlogN
Describe insertion sort:
Insertion sort consists of N-1 passes. For pass p =1 through N-1, insertion sort ensures that the elements in positions 0 through p are in sorted order. Insertion sort makes use of the fact that elements in positions 0 through p-1 are already known to be in sorted order.
Information-theoretic lower bound states what?
Information-theoretic lower bound- if there are P different possible cases to distinguish, and the questions are of the form Yes/No, then ceiling(log P) questions are always required to solve the problem.
What is a stable sorting algorithm?
Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.
What is the best case scenario for quick sort?
In the best case, the pivot picked always belongs in the middle of the array creating two equal subarrays.
What is the Simple Quick Sort algorithm?
- SimpleQuicksort(List items)
- If(items.size() > 1)
- List smaller = new ArrayList<>();
- List same = new ArrayList<>();
- List larger = new ArrayList<>();
- Integer chosenItem = items.get(items.size()/2)
- For(Integer I : items)
- If(I < chosenItem)
- Smaller.add(i)
- Else if(I > chosenItem)
- Larger.add(i)
- Else
- Same.add(i)
- If(I < chosenItem)
- Items.clear();
- Items.addAll(smaller);
- Items.addAll(same);
- Items.addAll(larger);
any algorithm that sorts by exchanging adjacent elements requires what time on average?
N2
What kind of pivot selection can guarantee linear time of quickselect?
The median of median of five.
What was one of the first algorithms to break the quadratic barrier?
Shell Sort