sorting algorithms o(n 2 ) algorithms o(n log n) algorithms something even better?
TRANSCRIPT
![Page 1: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/1.jpg)
Sorting Algorithms
O(n2) algorithms
O(n log n) algorithms
Something even better?
![Page 2: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/2.jpg)
Sortinga frequently used process
Nature tends towards disorder Human prefer order
e.g. address books
shopping lists
databases
![Page 3: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/3.jpg)
Insertion sort – O(n2)
Commonly used for hand-sorting Use two lists – unsorted and sorted
unsorted = input list (size = n)
sorted = an empty list
loop from i =0 to n-1 do n loops
sorted.insertInOrder(unsorted[i]) o(n)
You have just implemented insertInOrder()
![Page 4: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/4.jpg)
Bubble sort – O(n2) Imagine an unsorted list held vertically Smaller values are “light” and bubble up
void bubbleSort() { int i,j; int last=current_size-1; for(i=0;i<last; i++) { fot(j=last;j>I;j--) { if(data[j] is less than data[j-1]) swap(j, j-1); } }}
![Page 5: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/5.jpg)
Quicksort – the first O(n log n) algorithm (1962)
Using divide-and-conquer technique
Select a pivot, split list into 2 sublists: smaller and larger
Repeat this to all sublists until sublist’s size is reduced to 1
5 2 1 9 3 8 7
2 1 3 5 9 8 7
1 2 3 5 7 8 9
1 2 3 5 8 7 9
pivot
![Page 6: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/6.jpg)
Quicksort – average O(n log n) the worst case O(n2)
quicksort(int fm, int to) {
int p; // pivot position
if(fm < to) {
p = partition(fm,to);
quicksort(fm, p-1);
quicksort(p+1,to);
}
}
5 2 1 9 3 8 7
2 1 3 5 9 8 7
1 2 3 5 7 8 9
1 2 3 5 8 7 9
Time per level
O(n)
O(n)
O(n)
O(n)
O(n log n)Total =
![Page 7: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/7.jpg)
Merge sort - O(n log n)(ref. last week’s lecture)
Why O(n log n)? Merging 2 sorted
lists takes O(n1+n2), n1 and n2 are sizes of these 2 lists
There are log n levels of merging, each level takes O(n)
17 24 31 45 50 63 85 96
85 24 63 45 17 31 96 90
24 85 45 63 17 31 90 96
24 45 63 85 17 31 90 968
8
84 4
2 222
![Page 8: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/8.jpg)
Can we have an O(n) sorting algorithm?
Bucket-sort
Yes, if we know the range of sorted items.
Let the range be [1,k],
If(k<O(n)), we can use extra momey
to make k “buckets”. Then put each
item into the correct bucket.
We can do sorting without comparisons!
Why O(n)? – only need go through n items once
1p 2p 5p 10p 20p 50p
1 2 3 4 5 6
A bag of coins
£1
£2
Total n
7
8
![Page 9: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/9.jpg)
Radix-sort - repeatedly apply Bucket-sort digit-by-digit
An example – assume that we know sorted items are integers at most 3 digits (i.e. less than 1000)
Input list – 314,17,802,509,87,352,199,128.
Input list 10 buckets
314
80217
50987
352199128
By 1st digit
0123456
789
jointhem
802352314
1787
128509119
The 1st
phase:
sortedby units
![Page 10: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/10.jpg)
Radix-sort – cont.
The 2nd phase, sorted by 2nd digit (tens).
list from last phase – 802,352,314,17,87,128,509,119.
list from last phase 10 buckets
By 2nd digit
0123456
789
802352
1787
128509119
314
802
352
17
87
128
509
119
314jointhem
![Page 11: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/11.jpg)
Radix-sort – cont.
The last phase, sorted by 3rd digit.
list from last phase – 802,509,314,17,119,128,352,87.
list from last phase sorted!
By 3rd digit
0123456
789
802
352
17
87
128
509
119
314
802
352
1787
128
509
119
314
jointhem
![Page 12: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/12.jpg)
Radix-sort - nearly finished codeOnly for students having problem with Java programming!void radixSort() { int k,j; int b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; // need 10 counters for 10 buckets // declare 10 buckets, B0, B2, ……, B9 String[] B0 = new String[limit]; …… // add other 9 more declarations for(k=1; k<5; k++) { // loop for digits. (we know that there are not more than 5 digits) b0=b1=b2=b3=b4=b5=b6=b7=b8=b9=0; // set all counter to 0 for(j=0; j<current_size; j++) { // loop for all items in the list, put them into correct buckets char c = Func.getNthDigit(data[j], k); switch (c) { case '0': B0[b0++]=data[j]; break; ...... case '9': B9[b9++]=data[j]; break; } } // join the buckets j = 0; for(k=0; k<b0; k++) data[j++] = B0[k]; .... for(k=0; k<b9; k++) data[j++] = B9[k]; }}
![Page 13: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/13.jpg)
Radix-sorttime complexity?
O(n×k) where n is the length of the given list, k is the maximum number of digits used in the list
To satisfy O(n×k) =< O(n log(n)), we need k =< log(n)
Bucket-sort or Radix-sort – trade off between space and time
![Page 14: Sorting Algorithms O(n 2 ) algorithms O(n log n) algorithms Something even better?](https://reader035.vdocuments.site/reader035/viewer/2022072014/56649eb35503460f94bba724/html5/thumbnails/14.jpg)
Time complexity
LIST
STACK
QUEUE
TREE
binarysearchtree
sortingdivide-conquertim
ing
L
LIST
insertiondeletion