Download presentation
Presentation is loading. Please wait.
Published bySabrina Bryant Modified over 6 years ago
1
Sorting Terminology Internal – sort done totally in main memory.
External – uses auxiliary storage (disk). Stable – retains original order if keys are the same. In asking if a sort is stable, we are asking can it be reasonably coded to be stable. Adaptive (Non-Oblivious) – takes advantage of existing order to do less work. Sort by address – uses indirect addressing so the record (structure) doesn’t have to be moved. Inversion – a pair of elements that is out of order. Important in determining a lower bound. The number of pairs of elements is (n ways to pick first, n-1 ways to pick the second, divide by two as order isn’t important). On average, only half of these pairs are out of order, so number of inversions is . n2 swaps of adjacent elements are required.
2
Where We Are We have covered stacks, queues, priority queues, and dictionaries Emphasis on providing one element at a time We will now step away from ADTs and talk about sorting algorithms Note that we have already met sorting Priority Queues (heapsort) Binary Search and Binary Search Trees
3
More Reasons to Sort General technique in computing:
Preprocess the data to make subsequent operations (not just ADTs) faster Example: Sort the data so that you can Find the kth largest in constant time for any k Perform binary search to find elements in logarithmic time Sorting's benefits depend on How often the data will change How much data there is
4
Real World versus Computer World
Sorting is a very general demand when dealing with data—we want it in some order Alphabetical list of people List of countries ordered by population Moreover, we have all sorted in the real world Some algorithms mimic these approaches Others take advantage of computer abilities Sorting Algorithms have different asymptotic and constant-factor trade-offs No single “best” sort for all scenarios Knowing “one way to sort” is not sufficient
5
A Comparison Sort Algorithm
We have n comparable elements in an array, and we want to rearrange them to be in increasing order Input: An array A of data records A key value in each data record (maybe many fields) A comparison function (must be consistent and total): Given keys a and b is a<b, a=b, a>b? Effect: Reorganize the elements of A such that for any i and j such that if i < j then A[i] A[j] Array A must have all the data it started with
6
Arrays? Just Arrays? The algorithms we will talk about will assume that the data is an array Arrays allow direct index referencing Arrays are contiguous in memory But data may come in a linked list Some algorithms can be adjusted to work with linked lists but algorithm performance will likely change (at least in constant factors) May be reasonable to do a O(n) copy to an array and then back to a linked list
7
Further Concepts / Extensions
Stable sorting: Duplicate data is possible Algorithm does not change duplicate's original ordering relative to each other In-place sorting: Uses at most O(1) auxiliary space beyond initial array Non-Comparison Sorting: Redefining the concept of comparison to improve speed Other concepts: External Sorting: Too much data to fit in main memory
8
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
9
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Read about on your own to learn how not to sort data Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
10
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
11
Best: _____ Worst: _____ Average: _____
Selection Sort Idea: At step k, find the largest element among the unsorted elements and put it at position size-k Alternate way of saying this: Find largest element, put it last Find next largest element, put it 2nd last Find next largest element, put it 3rd last … Loop invariant: When loop index is i, the last i elements are the i largest elements in sorted order Time? Best: _____ Worst: _____ Average: _____
12
template<class T>
void SelectionSort(T a[], int n) {// Sort the n elements a[0:n-1]. for (int size = n; size > 1; size--) { int j = Max(a, size); // loop Swap(a[j], a[size - 1]); } // for } // SelectionSort
13
Analysis of Selection Sort
AnalysisOnly n moves – use when records (structure) are long. Requires n2/2 compares even when almost already sorted – non-adaptive (Oblivious). Unstable – because it may swap the element in the last location past other instances of itself.
14
Bubble Sort Compares adjacent elements – exchanges if out of order.
Lists get smaller each time – at least one is placed in final order. Place of last swap is as much as you have to look at. Quit if you do no swaps Stable adaptive – if the hasSwapped flag is included Analysis: void bubble(Data[] a, int size) { for (int i = 0; i < size - 1; i++) { bool hasSwapped = false; for (int j = 0; j< size-1-i; j++) if (a[j].value > a[j+1].value) { hasSwapped = true; swap(a[j],a[j+1]); } if (!hasSwapped)return;
15
Insertion Sort Sorts by inserting records into an already sorted portion of the array. Two groups of keys - sorted and unsorted. Insert n times - each time have to move about elements to insert.
16
template<class T>
void InsertionSort(T a[], int n) {// Sort a[0:n-1]. for (int i = 1; i < n; i++) { T t = a[i]; for (int j = i; j > 0 && t < a[j-1]; j--) a[j] = a[j - 1]; a[j] = t; } // for } // InsertionSort As with heaps, “moving the hole” is faster than unnecessary swapping (impacts constant factor)
17
Stable as never swap with other than an adjacent element Improvements
Performs better when the degree of unsortedness is low – recommended for data that is nearly sorted. Adaptive (Non-Oblivious) as if new element to be added is greater than last element of sorted part, we do not have to look at anything else. Stable as never swap with other than an adjacent element Improvements Use binary search compares, but number of moves doesn’t change so no real gain. Linked list storage – can’t use binary search – still O(n2). Could use sentinel containing the key for (int j = i; j > 0 && t < a[j-1]; j--) for (int j = i; t < a[j-1]; j--) Sentinel is extra item added to one end of the list so ensure that the loop terminates without having to include a separate check.
18
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
19
Shell Sort (devised by Donald Shell in 1959)
A subquadratic algorithm whose code is only slightly longer than insertion sort, making it the simplest of the faster algorithms. Avoid large amounts of data movement by: Comparing elements that are far apart. Comparing elements that are less far apart. Gradually shrinks toward insertion sort. Shell sort is known as a diminishing gap sort.
20
Example of Shell sort 81 94 11 96 12 35 17 95 28 58 41 75 15 Original
After 5-sort After 3-sort After 1-sort
21
template<class T>
void ShellSort (T a[], int length ) { int j; for ( int gap = length / 2; gap > 0; gap = gap == 2 ? 1 : static_cast<int>( gap / 2.2 ) ) for ( int i = gap; i < length; i++ ) { T tmp = a[i]; for (j=i ; j >= gap && tmp < a[j-gap]; j -= gap ) a[j] = a[j-gap]; a[j] = tmp; } // for } // ShellSort
22
Worst case running time is O(n2).
When the gap goes to 1, the sort is essentially an insertion sort Can get bad running times if all big numbers are at even positions and all small numbers are at odd positions AND the gaps don’t rearrange them as they are always even. The average running time appears to be between O(n1.25) and O(n1.17) if we use a gap of 2.2. Reasonable sort for large inputs. Complicated analysis. Unstable – as when your gap is five, you swap elements five apart – ignoring duplicates that lie between. Adaptive – as builds off insertion sort, which is adaptive
23
Heap Sort Worst-case running time: Stable? In-place? O(n log n) Why?
buildHeap(…); for(i=0; i < arr.length; i++) arr[i] = deleteMin(); Worst-case running time: Stable? In-place? O(n log n) Why?
24
Divide and Conquer Very important technique in algorithm design
Divide problem into smaller parts Independently solve the simpler parts Think recursion Or potential parallelism Combine solution of parts to produce overall solution (conquer) Note, the work in recursive solutions happens either in dividing the problem in two or in combining the results. The following algorithms are different in what causes the work.
25
Divide-and-Conquer Sorting
Two great sorting methods are fundamentally divide-and-conquer Mergesort: Recursively sort the left half Recursively sort the right half Merge the two sorted halves Quicksort: Pick a “pivot” element Separate elements by pivot (< and >) Recursive on the separations Return < pivot, pivot, > pivot]
26
Mergesort To sort array from position lo to position hi:
1 2 3 4 5 6 7 8 2 9 4 5 3 1 6 a lo hi To sort array from position lo to position hi: If range is 1 element long, it is already sorted! (our base case) Else, split into two halves: mid = (hi+lo)/2 Sort two halves Merge the two halves together Merging takes two sorted parts and sorts everything O(n) but requires auxiliary space…
27
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array aux After merge, we will copy back to the original array
28
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 aux After merge, we will copy back to the original array
29
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 aux After merge, we will copy back to the original array
30
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 aux After merge, we will copy back to the original array
31
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 aux After merge, we will copy back to the original array
32
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 5 aux After merge, we will copy back to the original array
33
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 5 6 aux After merge, we will copy back to the original array
34
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 5 6 8 aux After merge, we will copy back to the original array
35
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 5 6 8 9 aux After merge, we will copy back to the original array
36
Example: Focus on Merging
Start with: 8 2 9 4 5 3 1 6 a After recursion: 2 4 8 9 1 3 5 6 a Merge: Use 3 “fingers” and 1 more array 1 2 3 4 5 6 8 9 aux After merge, we will copy back to the original array 1 2 3 4 5 6 8 9 a
37
Example: Mergesort Recursion
8 2 9 4 5 3 1 6 Divide Divide 8 2 9 4 5 3 1 6 Divide 1 Element Merge 2 8 4 9 3 5 1 6 Merge Merge
38
Mergesort: Time Saving Details
What if the final steps of our merge looked like this? Isn't it wasteful to copy to the auxiliary array just to copy back… 2 4 5 6 1 3 8 9 Main array Auxiliary array
39
Mergesort: Time Saving Details
If left-side finishes first, just stop the merge and copy back: If right-side finishes first, copy remaining into right then copy back: copy first second
40
Merge code only (not sort)
void Merge(int c[], int d[] int l, int m, int r) {// Merge c[l:m]] and c[m:r] to d[l:r]. int i = l, // cursor for first segment j = m+1, // cursor for second k = 0; // cursor for result while ((i <= m) && (j <= r)) if (c[i] <= c[j]) d[k++] = c[i++]; else d[k++] = c[j++]; while (j<=r) d[k++] = c[j++]; while (i<=m) d[k++] = c[i++]; memcpy (&c[l], &d[0], (r-l+1)*intSize); } // Merge
41
Complexity? If merging using files (reading and writing sorted pieces to a file), no problem with storage space. If we have an array of items to be stored, the MergeSort requires an auxiliary storage. Works well for external sorts (not all data needs to be in memory at the same time) as doesn’t need to see entire data (like a quicksort does). Stable, as control order when merging Non-adaptive (Oblivious) as it does the same amount of work regardless of initial order.
42
Suppose we want to use less space?
only make copy of left section, merge back into original location.
43
Quicksort Also uses divide-and-conquer
Recursively chop into halves Instead of doing all the work as we merge together, we will do all the work as we recursively split into halves Unlike MergeSort, does not need auxiliary space O(n log n) on average, but O(n2) worst-case MergeSort is always O(n log n) So why use QuickSort at all? Can be faster than Mergesort Believed by many to be faster Quicksort does fewer copies and more comparisons, so it depends on the relative cost of these two operations!
44
Quicksort Overview Pick a pivot element Partition all the data into:
The elements less than the pivot The pivot The elements greater than the pivot Recursively sort A and C The answer is as simple as “A, B, C” Seems easy but the details are tricky!
45
Quicksort: Think in Terms of Sets
select pivot value 81 31 57 43 13 75 92 65 26 S1 S2 31 75 43 13 65 partition S 81 92 26 57 S1 S2 QuickSort(S1) and QuickSort(S2) 13 26 31 43 57 65 75 81 92 S 13 26 31 43 57 65 75 81 92 Presto! S is sorted [Weiss]
46
Example: Quicksort Recursion
8 2 9 4 5 3 1 6 Divide 5 Divide 3 2 1 4 8 6 9 Divide 1 element 2 1 Conquer 1 2 Conquer Conquer
47
Quicksort Details We have not explained: How to pick the pivot element
Any choice is correct: data will end up sorted But we want the two partitions to be about equal in size How to implement partitioning In linear time In-place
48
Pivots Best pivot? Worst pivot? Median Halve each time
Greatest/least element Problem of size n - 1 O(n2) 8 2 9 4 5 3 1 6 8 2 9 4 5 3 1 6
49
Quicksort: Potential Pivot Rules
When working on range arr[lo] to arr[hi-1] Pick arr[lo] or arr[hi-1] Fast but worst-case occurs with nearly sorted input Pick random element in the range Does as well as any technique But random number generation can be slow Still probably the most elegant approach Median of 3, (e.g., arr[lo], arr[hi-1], arr[(hi+lo)/2]) Common heuristic that tends to work well
50
Partitioning Conceptually easy, but hard to correctly code
Need to partition in linear time in-place
51
int partition( T a[], int l, int r)
//move pivot into location a[l]; if (l>=r) return; i = l; j = r+1; while ( true ) { do {// find >= element on left side i++; } while (a[i] < pivot); do {// find <= element on right side j--; } while ( a[j] > pivot ); if ( i >= j ) break; // swap pair not found Swap( a[i], a[j] ); } // while Swap(a[l],a[j]) return j; }
52
Quicksort Example Step Two: Move Pivot to the lo Position
Step One: Pick Pivot as Median of 3 lo = 0, hi = 10 Step Two: Move Pivot to the lo Position 8 1 4 9 3 5 2 7 6 6 1 4 9 3 5 2 7 8
53
Quicksort Example 6 1 4 9 3 5 2 7 8 Now partition in place Move fingers Swap Move pivot 6 1 4 9 3 5 2 7 8 6 1 4 2 3 5 9 7 8 6 1 4 2 3 5 9 7 8 5 1 4 2 3 6 9 7 8 This is a short example—you typically have more than one swap during partition
54
Quicksort Analysis Best-case: Pivot is always the median T(0)=T(1)=1 T(n)=2T(n/2) + n linear-time partition Same recurrence as Mergesort: O(n log n) Worst-case: Pivot is always smallest or largest T(n) = 1T(n-1) + n Basically same recurrence as Selection Sort: O(n2) Average-case (e.g., with random pivot): O(n log n) (see text) Unstable
55
Oblivious: quicksort does NOT take advantage of partial ordering
56
Quicksort Cutoffs For small n, recursion tends to cost more than a quadratic sort Remember asymptotic complexity is for large n Recursive calls add a lot of overhead for small n Common technique: switch algorithm below a cutoff Rule of thumb: use insertion sort for n < 20 Notes: Could also use a cutoff for merge sort None of this affects asymptotic complexity, just real-world performance
57
Quicksort Cutoff Skeleton
void quicksort(int[] arr, int lo, int hi) { if(hi – lo < CUTOFF) insertionSort(arr,lo,hi); else … } This cuts out the vast majority of the recursive calls Think of the recursive calls to quicksort as a tree Trims out the bottom layers of the tree Smaller arrays are more likely to be nearly sorted (good for insertion sort)
58
If have a lot of duplicates…
59
Linked Lists and Big Data
Mergesort can very nicely work directly on linked lists Heapsort and Quicksort do not InsertionSort and SelectionSort can too but slower Mergesort also the sort of choice for external sorting Quicksort and Heapsort jump all over the array Mergesort scans linearly through arrays In-memory sorting of blocks can be combined with larger sorts Mergesort can leverage multiple disks
60
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
61
Breaking the Ω(n LOG N) BARRIER for Sorting
Nothing is ever straightforward in computer science… Breaking the Ω(n LOG N) BARRIER for Sorting
62
Sorting: The Big Picture
Horrible algorithms: Ω(n2) Simple algorithms: O(n2) Fancier algorithms: O(n log n) Bogo Sort Stooge Sort Comparison lower bound: (n log n) Insertion sort Selection sort Bubble Sort Shell sort … Specialized algorithms: O(n) Heap sort Merge sort Quick sort (avg) … Bucket sort Radix sort
63
BucketSort (a.k.a. BinSort)
If all values have keys which are integers between 1 and K (or any small range), Create an array of size K Put each element in its proper bucket (a.ka. bin) Output result via linear pass through array of buckets count array 1 Chris 2 Garret 3 Melia 4 Peter 5 Rob Example: K=5 Input: (5 Rob , 1 Chris , 2 Garret , 4 Peter , 3 Melia) Output:
64
BucketSort (a.k.a. BinSort)
If all values are integers, but there are multiples. Create an array of size K Put each element in its proper bucket (a.ka. bin) If data is only integers, only need to store the count of how times that bucket has been used Output result via linear pass through array of buckets count array 1 2 3 4 5 Example: K=5 Input: (5, 1, 3, 4, 3, 2, 1, 1, 5, 4, 5) Output:
65
BucketSort (a.k.a. BinSort)
If all values to be sorted are known to be integers between 1 and K (or any small range), Create an array of size K Put each element in its proper bucket (a.ka. bin) If data is only integers, only need to store the count of how times that bucket has been used Output result via linear pass through array of buckets Value count 1 3 2 4 5 Example: K=5 Input: (5, 1, 3, 4, 3, 2, 1, 1, 5, 4, 5) Output:
66
BucketSort (a.k.a. BinSort)
If all values to be sorted are known to be integers between 1 and K (or any small range), Create an array of size K Put each element in its proper bucket (a.ka. bin) If data is only integers, only need to store the count of how times that bucket has been used Output result via linear pass through array of buckets count array 1 3 2 4 5 Example: K = 5 Input: (5, 1, 3, 4, 3, 2, 1, 1, 5, 4, 5) Output: (1, 1, 1, 2, 3, 3, 4, 4, 5, 5, 5) What is the running time?
67
Analyzing Bucket Sort Overall: O(n+K)
Linear in n, but also linear in K (n log n) lower bound does not apply because this is not a comparison sort Good when K is small (or not much larger) than n Do not spend time doing comparisons of duplicates Bad when K is much larger than n Wasted space / time during final linear O(K) pass
68
Sorting Summary Simple O(n2) sorts can be fastest for small n
Selection sort, Insertion sort (is linear for nearly-sorted) Both stable and in-place Good for “below a cut-off” to help divide-and-conquer sorts O(n log n) sorts Heapsort, in-place but not stable Mergesort, not in-place but stable and works as external sort Quicksort, in-place but not stable and O(n2) in worst-case Often fastest, but depends on costs of comparisons/copies Ω(n log n) worst and average bound for comparison sorting Non-comparison sorts Bucket sort good for small range of key values
69
Indirect Sorting If we are sorting an array whose elements are large, copying the items can be very expensive. Not all of our sorts did the same amount of moving of data. Which ones were particularly good about limiting the amount of moving? We can get around this by doing indirect sorting. We have an additional array of pointers where each element of the pointer array points to an element in the original array. When sorting, we compare keys in the original array but we swap the element in the pointer array. This saves a lot of copying at the expense of indirect references to the elements of the original array. We must still rearrange the final array.
70
Best way to sort? It depends!
71
Timsort Clever use of knowledge of real world cases
Many times data is mainly ordered (add new students to existing sorted class, combine two sections both of which are already sorted)
72
Uses Natural mergesort
Must take advantage of special cases WITHOUT making non-special cases worse. Takes advantage of existing order. How much work is involved? 6 16 20 22 31 37 44 63 91 93 19 23 29 33 39 40 50 59 62 64 70 73 74 75 97 99 5 25 49 53 61 80 7 43 72 77
73
Uses Natural mergesort
Takes advantage of existing order. Want to take advantage of values that are in memory. If lots of small runs, what happens to run time? Height of “work block” is dependent on number of runs. 16 20 22 31 37 44 63 91 93 19 23 29 33 11 40 50 62 64 1 73 75 97 99 5 25 15 53 61 80 7 43 72 77
74
Extend small runs - MinRun
If runs are too small, increase the size by using Insertion Sort on small pieces. For files less than 64, minrun is just 64. We do just a single insertion sort. 16 20 22 31 37 44 63 91 93 19 23 29 33 11 40 50 62 64 1 73 75 97 99 5 25 15 53 61 80 7 43 72 77 16 20 22 31 37 44 63 91 93 11 19 23 29 33 40 50 1 62 64 73 5 25 15 53 75 97 99 7 43 61 72 77 80
75
Idea Runs of lengths: should that be combined as ( ) + 610 or (310 + ( )) Does it matter?
76
Order of merging If we wanted to decrease total run time, what order would we merge the pieces below? What if some pieces weren’t merged as many times as other pieces? Regular Way Modified order - Note not all elements are touched in each row of work
77
Order of merging Takes advantage of existing order
If we wanted to decrease total run time, what order would we merge the pieces below? What if some pieces weren’t merged as many times as other pieces? Regular Way Modified order - Note not all elements are touched in each row of work
78
What to merge – the RULES
In order to balance run lengths (while keeping a low bound on the number of runs) we maintain two invariants on the stack entries, where A, B and C are the lengths of the three rightmost not-yet merged slices: 1. |A |> |B|+|C| (if not, merge B with the smaller of A and C) 2. |B| > |C| (if not, merge A and B)
79
Require the rules 1. |A |> |B|+|C| (if not, merge B with the smaller of A ,C) 2. |B| > |C| (if not, merge A and B) What would you do if these were the sizes of the runs: unhappy (rule 2) happy unhappy(rule 1) unhappy (rule 1) happy unhappy (rule 1) unhappy (rule 1) > 24
80
What about Inverted Order?
What if we get a section that is in strictly inverse order? Why would we need to insist on strictly decreasing order? 6 16 20 22 31 37 44 63 91 93 99 97 75 73 70 62 50 39 33 13 11 1 2 4 5 25 49 53 61
81
Merging Merging adjacent runs of lengths X and Y in-place is very difficult, but if we have temp memory equal to min(|X|, |Y|), it's easy. We only need to copy the one chunk If X is the smaller-sized chunk (function merge_low), Copy X to a temp array, leave Y alone, and then we can do the obvious merge algorithm left to right. There's always a free area in the original area equal to the number not yet merged from the copied array.
82
Merging low Even if almost all of the copied chunk get moved before items from Y, there is room for them. Once items from Y are moved, there is more room for the copied items
83
What about if left side is bigger?
We don’t want to move the bigger piece. What can we do?
84
Other Refinements Merging
A binary search examines A to find the first position larger than the first element of B (a'). Note that A and B are already sorted individually. When a' is found, the algorithm can ignore elements before that position while inserting B. Similarly, the algorithm also looks for the smallest element in B (b') greater than the largest element in A (a). The elements after b' can also be ignored for the merging. 14 16 31 34 35 37 38 41 44 46 50 57 28 30 33 36 40 42 48 49 61 62 65 14 16 31 34 35 37 38 41 44 46 50 57 28 30 33 36 40 42 48 49 61 62 65 Ignore the ends and just merge the middle runs
85
Galloping Assume A is the shorter run. We enter galloping mode when we keep taking elements from B. In galloping mode, we first look for A[0] in B (using a modified binary search). We do this via "galloping by powers of two", comparing A[0] in turn to B[0], B[1], B[3], B[7], ..., B[2**j - 1], ..., until finding the k such that B[2**(k-1) - 1] < A[0] <= B[2**k - 1]. This takes at most roughly lg(|B|) comparisons, and, unlike a straight binary search, favors finding the right spot (which is likely early in B).
86
Galloping X Y 3 6 9 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 6 9 10 53 73 77 82 95 102 103 110 115 120 130 144
87
Galloping
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.