sorting hkoi training team (advanced) 2006-01-21
TRANSCRIPT
![Page 1: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/1.jpg)
Sorting
HKOI Training Team (Advanced)
2006-01-21
![Page 2: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/2.jpg)
What is sorting?
Given: A list of n elements: A1,A2,…,An
Re-arrange the elements to make them follow a particular order, e.g.Ascending Order: A1 ≤ A2 ≤ … ≤ An
Descending Order: A1 ≥ A2 ≥ … ≥ An
We will talk about sorting in ascending order only
![Page 3: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/3.jpg)
Why is sorting needed?
Some algorithms works only when data is sortede.g. binary search
Better presentation of dataOften required by problem setters, to reduce
workload in judging
![Page 4: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/4.jpg)
Why learn Sorting Algorithms?
C++ STL already provided a sort() function
Unfortunately, no such implementation for Pascal This is a minor point, though
![Page 5: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/5.jpg)
Why learn Sorting Algorithms?
Most importantly, OI problems does not directly ask for sorting, but its solution may be closely linked with sorting algorithms
In most cases, C++ STL sort() is useless. You still need to write your own “sort”
So… it is important to understand the idea behind each algorithm, and also their strengths and weaknesses
![Page 6: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/6.jpg)
Some Sorting Algorithms…
Bubble Sort Insertion Sort Selection Sort Shell Sort Heap Sort Merge Sort Quick Sort Counting Sort Radix Sort
How many of them do you
know?
![Page 7: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/7.jpg)
Bubble, Insertion, Selection…
Simple, in terms of Idea, and Implementation
Unfortunately, they are inefficientO(n2) – not good if N is large
Algorithms being taught today are far more efficient than these
![Page 8: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/8.jpg)
Shell Sort
Named after its inventor, Donald ShellObservation: Insertion Sort is very
efficient whenn is smallwhen the list is almost sorted
![Page 9: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/9.jpg)
Shell Sort
Divide the list into k non-contiguous segments Elements in each segments are k-elements
apart In the beginning, choose a large k so that all
segments contain a few elements (e.g. k=n/2) Sort each segment with Insertion Sort
2 1 4 7 4 8 3 6 4 774
![Page 10: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/10.jpg)
Shell Sort
Definition: A list is said to be “k-sorted” when A[i] ≤ A[i+k] for 1 ≤ i ≤ n-k
Now the list is 5-sorted
2 1 4 4 4 8 3 6 7 7
![Page 11: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/11.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 4 7 4 8 3 6 4 7
Insert≥2 Insert≥1
![Page 12: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/12.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 4 7 4 8 3 6 4 7
Insert≥4 Insert≥7
![Page 13: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/13.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 4 7 4 8 3 6 4 7
Insert<4<4≥2
![Page 14: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/14.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 47 4 83 6 4 7
Insert<8<7≥1
![Page 15: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/15.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 474 83 6 4 7
Insert≥4
![Page 16: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/16.jpg)
Shell Sort
After each pass, reduces k (e.g. by half) Although the number of elements in each
segments increased, the segments are usually mostly sorted
Sort each segments with Insertion Sort again
2 1 474 83 6 4 7
Insert<8≥7
![Page 17: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/17.jpg)
Shell Sort
Finally, k is reduced to 1 The list look like mostly sorted Perform 1-sort, i.e. the ordinary Insertion Sort
2 1 474 83 6 4721 4 74 83 64 7
![Page 18: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/18.jpg)
Shell Sort – Worse than Ins. Sort?
In Shell Sort, we still have to perform an Insertion Sort at last
A lot of operations are done before the final Insertion Sort
Isn’t it worse than Insertion Sort?
![Page 19: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/19.jpg)
Shell Sort – Worse than Ins. Sort?
The final Insertion Sort is more efficient than before
All sorting operations before the final one are done efficiently
k-sorts compare far-apart elementsElements “moves” faster, reducing
amount of movement and comparison
![Page 20: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/20.jpg)
Shell Sort – Increment Sequence
In our example, k starts with n/2, and half its value in each pass, until it reaches 1, i.e. {n/2, n/4, n/8, …, 1}
This is called the “Shell sequence”In a good Increment Sequence, all
numbers should be relatively prime to each other
Hibbard’s Sequence: {2m-1, 2m-1-1, …, 7, 3, 1}
![Page 21: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/21.jpg)
Shell Sort – Analysis
Average Complexity: O(n1.5)Worse case of Shell Sort with Shell
Sequence: O(n2)When will it happen?
![Page 22: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/22.jpg)
Heap Sort
In Selection Sort, we scan the entire list to search for the maximum, which takes O(n) time
Are there better way to get the maximum?
With the help of a heap, we may reduce the searching time to O(lg n)
![Page 23: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/23.jpg)
Heap Sort – Build Heap
1. Create a Heap with the list
2 8 5 7 1 42
8 5
7 1 4
![Page 24: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/24.jpg)
Heap Sort
2. Pick the maximum, restore the heap property
28 57 1 4
2
8
57
1 4
![Page 25: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/25.jpg)
Heap Sort
3. Repeat step 2 until heap is empty
2 857 14
2
5
7
1
4
![Page 26: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/26.jpg)
Heap Sort
3. Repeat step 2 until heap is empty
2 85 714
2
5
14
![Page 27: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/27.jpg)
Heap Sort
3. Repeat step 2 until heap is empty
2 85 714
2 1
4
![Page 28: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/28.jpg)
Heap Sort
3. Repeat step 2 until heap is empty
2 85 71 42
1
![Page 29: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/29.jpg)
Heap Sort
3. Repeat step 2 until heap is empty
2 85 71 41
![Page 30: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/30.jpg)
Heap Sort – Analysis
Complexity: O(n lg n)Not a stable sortDifficult to implement
![Page 31: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/31.jpg)
Merging
Given two sorted list, merge the list to form a new sorted list
A naïve approach: Append the second list to the first list, then sort themSlow, takes O(n lg n) time
Are there any better way?
![Page 32: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/32.jpg)
Merging
We make use of a property of sorted lists: The first element is always the minimumWhat does that imply?
An additional array is needed store temporary merged list
Pick the smallest number from the un-inserted numbers and append them to the merged list
![Page 33: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/33.jpg)
Merging
List A
List B
1 3 7 9
2 3 6
Temp
![Page 34: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/34.jpg)
Merge Sort
Merge sort follows the divide-and-conquer approachDivide: Divide the n-element sequence into
two (n/2)-element subsequencesConquer: Sort the two subsequences
recursivelyCombine: Merge the two sorted
subsequence to produce the answer
![Page 35: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/35.jpg)
Merge Sort
1. Divide the list into two
2. Call Merge Sort recursively to sort the two subsequences
Merge Sort
2 8 5 7 1 485
Merge Sort
1 4 7
![Page 36: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/36.jpg)
Merge Sort
3. Merge the list (to temporary array)
2 85 1 4 7
4. Move the elements back to the list
![Page 37: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/37.jpg)
Merge Sort – Analysis
Complexity: O(n lg n)Stable Sort
What is a stable sort?Not an “In-place” sort
i.e. Additional memory requiredEasy to implement, no knowledge of
other data structures needed
![Page 38: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/38.jpg)
Stable Sort
What is a stable sort?The name of a sorting algorithmA sorting algorithm that has stable
performance over all distribution of elements, i.e. Best ≈ Average ≈ Worse
A sorting algorithm that preserves the original order of duplicated keys
![Page 39: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/39.jpg)
Stable Sort
1 3 5 3 4 2Original List a b
1 2 3 3 4 5Stable Sort a b
1 2 3 3 4 5Un-stable Sort b a
![Page 40: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/40.jpg)
Stable Sort
Which sorting algorithms is/are stable?
Stable Un-stable
Bubble Sort
Merge Sort
Insertion Sort
Selection Sort
Shell Sort
Heap Sort
![Page 41: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/41.jpg)
Stable Sort
In our previous example, what is the difference between 3a and 3b?
When will stable sort be more useful?Sorting recordsMultiple keys
![Page 42: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/42.jpg)
Quick Sort
Quick Sort also uses the Divide-and-Conquer approachDivide: Divide the list into two by partitioningConquer: Sort the two list by calling Quick
Sort recursivelyCombine: Combine the two sorted list
![Page 43: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/43.jpg)
Quick Sort – Partitioning
Given: A list and a “pivot” (usually an element in the list)
Re-arrange the elements so thatElements on the left-hand side of “pivot” are
less than the pivot, andElements on the right-hand side of the
“pivot” are greater than or equal to the pivot
Pivot< Pivot ≥ Pivot
![Page 44: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/44.jpg)
Quick Sort – Partitioning
e.g. Take the first element as pivot
Swap all pairs of elements that meets the following criteria:The left one is greater than or equal to pivotThe right one is smaller than pivot
Swap pivot with A[hi]
4 6 7 0 9 3 9 4
Pivot lo hi≥ pivot? < pivot?< pivot?< pivot?≥ pivot? < pivot?< pivot?
![Page 45: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/45.jpg)
Quick Sort
After partitioning:
Apply Quick Sort on both lists
4 670 93 9 4
PivotQuick Sort Quick Sort
6 7 9 94
![Page 46: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/46.jpg)
Quick Sort – Analysis
ComplexityBest: O(n lg n)Worst: O(n2)Average: O(n lg n)
When will the worst case happen?How to avoid the worst case?In-Place SortNot a stable sort
![Page 47: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/47.jpg)
Counting Sort
Consider the following list of numbers
5, 4, 2, 1, 4, 3, 4, 2, 5, 1, 4, 5, 3, 2, 3, 5, 5Range of numbers = [1,5]We may count the occurrence of each
number
1 2 3 4 5
2 3 3 4 5
![Page 48: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/48.jpg)
Counting Sort (1)
With the frequency table, we can reconstruct the list in ascending order
1 2 3 4 5
2 3 3 4 5
1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5
![Page 49: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/49.jpg)
Counting Sort (1)
Can we sort records with this counting sort?
Is this sort stable?
![Page 50: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/50.jpg)
Counting Sort (2)
An alternative way: use cumulative frequency table and a temporary array
Given the following “records”
3 2 1 2 2 3
1 2 3
Frequency Table
1 3 2
Cumulative
4 6
![Page 51: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/51.jpg)
Counting Sort (2)
1 2 3
1 4 6
13 2 2 2 3
5320 1 4
![Page 52: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/52.jpg)
Counting Sort – Analysis
Complexity: O(n+k), where k is the range of numbers
Not an In-place sortStable Sort (Method 2)Cannot be applied on data with wide
ranges
![Page 53: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/53.jpg)
Radix Sort
Counting Sort requires a “frequency table”
The size of frequency table depends on the range of elements
If the range is large (e.g. 32-bit), it may be infeasible, if not impossible, to create such a table
![Page 54: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/54.jpg)
Radix Sort
We may consider a integer as a “record of digits”, each digit is a key
Significance of keys decrease from left to right
e.g. the number 123 consists of 3 digitsLeftmost digit: 1 (Most significant)Middle digit: 2Rightmost digit: 3 (Least signficant)
![Page 55: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/55.jpg)
Radix Sort
Now, the problem becomes a multi-key record sorting problem
Sort the records on the least significant key with a stable sort
Repeat with the 2nd least significant key, 3rd least significant key, and so on
![Page 56: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/56.jpg)
Radix Sort
For all keys in these “records”, the range is [0,9] Narrow range
We apply Counting Sort to do the sorting here
![Page 57: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/57.jpg)
Radix Sort
101 97 141 110 997 733
Original List
0
![Page 58: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/58.jpg)
Radix Sort
Sort on the least significant digit
101 097 141 110 997 733
0 1
1 3
2 3
3 4
4 4
5 4
6 4
7 6
8 6
9 6
![Page 59: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/59.jpg)
Radix Sort
Sort on the 2nd least significant digit
101 097141110 997733
0 1
1 2
2 2
3 3
4 4
5 4
6 4
7 4
8 4
9 6
![Page 60: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/60.jpg)
Radix Sort
Lastly, the most significant digit
101 097141110 997733
0 1
1 4
2 4
3 4
4 4
5 4
6 4
7 5
8 5
9 6
![Page 61: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/61.jpg)
Radix Sort – Analysis
Complexity: O(dn), where d is the number of digits
Not an In-place SortStable SortCan we run Radix Sort on
Real numbers?String?
![Page 62: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/62.jpg)
Choosing Sorting Algorithms
List SizeData distributionData TypeAvailability of Additional MemoryCost of Swapping/Assignment
![Page 63: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/63.jpg)
Choosing Sorting Algorithms
List Size If N is small, any sorting algorithms will do If N is large (e.g. ≥5000), O(n2) algorithms
may not finish its job within time limitData Distribution
If the list is mostly sorted, running QuickSort with “first pivot” is extremely painful
Insertion Sort, on the other hand, is very efficient in this situation
![Page 64: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/64.jpg)
Choosing Sorting Algorithms
Data Type It is difficult to apply Counting Sort and
Radix Sort on real numbers or any other data types that cannot be converted to integers
Availability of Additional MemoryMerge Sort, Counting Sort, Radix Sort
require additional memory
![Page 65: Sorting HKOI Training Team (Advanced) 2006-01-21](https://reader035.vdocuments.site/reader035/viewer/2022062518/56649e9a5503460f94b9cede/html5/thumbnails/65.jpg)
Choosing Sorting Algorithms
Cost of Swapping/AssignmentMoving large records may be very time-
consumingSelection Sort takes at most (n-1) swap
operationsSwap pointers of records (i.e. swap the
records logically rather than physically)