radix sort counting sort overview of sorts asymptotic ...1 radix sort counting sort overview of...
Post on 23-May-2020
15 Views
Preview:
TRANSCRIPT
1
Radix sort
Counting sort
Overview of sorts asymptotic complexities
Sorting experiment
ALG 09
A4B33ALG 2011 / 09
2
DaD
DbD
DCaaDb
aDDCCC
aDCDDb
babbbC
aCb
Cbb
Cba
5
109
1
6
2
7
3
8
4
1211
13
B_ _ a _ _ b _ _ C _ _ D
Cbb DaDaDb
DCa CCCaDD
DDbaDCbbC
babDbD
Cba1 23
4 56
789
1011
12
aCb13
12
567
34
1098
1211
13
Unsorted Sort by the 3rd symbol
Radix sort
New order 1Previous order 1
A4B33ALG 2011 / 09
3
5
109
1
6
2
7
3
8
4
1211
13
B_ a _ _ b _ _ C _ _ D_Cbb
DaD
aDb
DCa
CCC
aDD
DDb
aDCbbC
bab
DbD
Cba
aCb
12
56
734
10
98
1211
13
Sorted from3rd symbol
DCaCbaCbb
aDbDDb
babaCbCCC aDCbbC
DaD
aDDDbD
123
45
678 910
11
1213
Sort by the 2nd symbol
Radix sortA4B33ALG 2011 / 09
4
5
109
1
6
2
7
3
8
4
1211
13
Ba _ _ b _ _ C _ _ D_ _
12
56
7
34
10
98
1211
13DCa
CbaCbb
aDbDDb
bab
aCbCCC
aDC
bbC
DaD
aDD
DbDbab DaDCba
CbbbbC DbDDCa
aCb
CCCaDb
DDbaDCaDD
1 2345 6
7
8
910
111213
Sorted!Sorted!
Sorted form 2nd symbol Sort by the 1st symbol
Radix sortA4B33ALG 2011 / 09
5
Example
DaD
DbD
DCaaDb
aDDCCC
aDCDDb
babbbC
aCb
Cbb
Cba
5
109
1
6
2
7
3
8
4
1211
13
B_ _ a
_ _ b
_ _ C
_ _ D
DaD
aDb
DCa
CCC
aDD
DDb
aDCbbC
bab
DbD
Cba
13
4
5
7
89
10
12
aCb13
12
567
34
1098
1211
13
Unsorted Sorted by the 3rd symbol
2611
start & end next
abC
6
Cbb
4152
1213911
123456789
10111213
D
712811109013000
33
Example
Radix sort ImplementationAuxiliary index arrays
register modified order
A4B33ALG 2011 / 09
6
DaD
DbD
DCaaDb
aDDCCC
aDCDDb
babbbC
aCb
Cbb
Cba
5
109
1
6
2
7
3
8
4
1211
13
Unsorted
Radix sort Implementation
367
128111090
13000
abC
4 12
D
1 135 92 11
Example:The listin the
"b" slot
One array for all lists
Array of pointersto the start and tothe end of the listfor each symbol
Radix sort can be performedwithout moving the original data.
It suffices just to manipulate thepointer arrays which containall information about thecurrent progress of the sort.
Here, both arrays reflect the statusafter sorting by
the 3rd character.s e
A4B33ALG 2011 / 09
7
DaD
DbD
DCaaDb
aDDCCC
aDCDDb
babbbC
aCb
Cbb
Cba
5
109
1
6
2
7
3
8
4
1211
13
Unsorted
907
130086112015
abC
10 2
D
12 114 53 6
Example:The listin the
"b" slot
Radix sort Implementation
51187000609413
abC
13 6
D
10 912 52 7
After: sortedby the 2nd symbol
After: sorted by the 1st symbol = all sorted
s e s e
A4B33ALG 2011 / 09
8
51187000609413
13 610 912 52 7
abCD
DaD
DbD
DCaaDb
aDDCCC
aDCDDb
babbbC
aCb
Cbb
Cba
5
109
1
6
2
7
3
8
4
1211
13
Unsorted
Radix sort Implementation
After: sorted by the 1st symbol = all sorted
Just print the data in theorder given by the lists: a b C D
aCbaDbaDCaDDbabbbCCbaCbbCCCDaDDbDDCaDDb
13386109121521147
s e
A4B33ALG 2011 / 09
9
Cbb
5
109
1
6
2
7
3
8
4
1211
13
907
130086112015
abC
10 2
D
12 114 53 6
From sorted by 2nd symbol to sorted by 1st symbol
s e
DaDaDbDCaCCCaDDDDbaDCbbCbabDbDCbaaCb
d
0000000000000
abC
0 0
D
0 00 00 0
s1s1 e1e1
d1d1
1
2
5
6
7
3
4
10
9
8
121113
Update arrays s, e, d:Fill temporary arrrays
s1, e1, d1and copy their contents
back to s, e, d.
Implementation will notcopy anything, it will
only swap the pointersto the original andtemporary arrays.
Arrays will specifyorder after
sorted by the1st symbol.
Arrays specify
order after sorted by the2nd symbol.
A4B33ALG 2011 / 09
10
Cbb
5
109
1
6
2
7
3
8
4
1211
13
907
130086112015
abC
10 2
D
12 114 53 6
s e
DaDaDbDCaCCCaDDDDbaDCbbCbabDbDCbaaCb
d
0000000000000
abC
0 0
D
0 00 00 0
s1s1 e1e1
d1d1
0000000000000
0 010 100 00 0
s1s1 e1e1
d1d1
0000000000000
0 010 100 02 2
s1s1 e1e1
d1d1
0000000000000
0 010 1012 122 2
s1s1 e1e1
0000000000010
0 010 1012 12 2
s1s1 e1e1
0000000009010
0 010 912 12 2
s1s1 e1e1
01100000009010
0 010 912 12 11
s1s1 e1e1
d1d1 d1d1 d1d1 d1d1
1
2
5
6
7
3
4
10
9
8
121113
Sortedby the
2ndsymbol
From sorted by 2nd symbol to sorted by 1st symbolA4B33ALG 2011 / 09
11
Cbb
5
109
1
6
2
7
3
8
4
1211
13
907
130086112015
abC
10 2
D
12 114 53 6
DaDaDbDCaCCCaDDDDbaDCbbCbabDbDCbaaCb
d
abCD
01100000009410
0 010 912 12 4
d1d1
01100000009410
13 1310 912 12 4
d1d1
51100000009410
13 1310 912 52 4
d1d1
51100000009413
13 310 912 52 4
d1d1
51107000009413
13 310 912 52 7
d1d1
51180000009413
13 810 912 52 7
d1d1
51180000609413
13 610 912 52 7
d1d1
1
2
5
6
7
3
4
10
9
8
121113
From sorted by 2nd symbol to sorted by 1st symbol
FinishedFinished
A4B33ALG 2011 / 09
Sortedby the
2ndsymbol
s e s1s1 e1e1 s1s1 e1e1 s1s1 e1e1 s1s1 e1e1 s1s1 e1e1 s1s1 e1e1 s1s1 e1e1
12
Radix sort Implementation
void radix_sort (String [] a) {int len = ...; // number of chars used (2^16?)int [] s = new int [len];int [] e = new int [len];int [] s1 = new int [len];int [] e1 = new int [len];int [] d = new int [a.length];int [] d1 = new int [a.length];int [] aux;
initStep(a, s, e, d); // 1st pass with last char
for (int p = a[0].length()-2; p >= 0; p-- ) {radixStep(a, p, s, e, d, s1, e1, d1); // do the jobaux = s; s = s1; s1 = aux; // just swap arraysaux = e; e = e1; e1 = aux; // dtto aux = d; d = d1; d1 = aux; // dtto
} output(a, s, e, d); // print sorted array
}
A4B33ALG 2011 / 09
13
Radix sort Implementation
void initStep (String[] a, int[] s, int [] e, int[] d){int pos = a[0].length()-1; // last char in stringint c; // char as array index for Radix sortfor (int i = 0; i < s.length; i++) // init arrayss[i] = e[i] = -1; // empty
for (int i = 0; i < a.length; i++) { // all last charsc = (int) a[i].charAt(pos); // char to indexif (s[c] == -1) e[c]= s[c] = i; // start new list
else {d[e[c]] = i; // extend existing list e[c] = i;
} } }
Add spaces to the shorter strings to make all stringsof the same length.
Caution: The arrays in the code are indexed from 0,The arrays in the code are indexed from 1.
A4B33ALG 2011 / 09
14
Radix sort Implementation
void radixStep(String[]a, int pos, int[] s, int[] e, int[] d, int[] s1, int[] e1, int[] d1){
int j; // index traverses old listsint c; // char as array index for Radix sortfor (int i = 0; i < s.length; i++) // init arrays
s1[i] = e1[i] = -1; // for (int i = 0; i < s.length; i++) // all used charsif (s[i] != -1) { // unempty list j = s[i]; while (true) { // scan the listc = (int) a[j].charAt(pos); // char to indexif (s1[c] == -1) e1[c]= s1[c] = j; // start new list
else {d1[k1[c]] = j; // extend existing liste1[c2] = j;
}if (j == e[i]) break;j = d[j]; // next string index
} } }
A4B33ALG 2011 / 09
15
d symbols ............ d loops
loop ............
d << n ..….
(n) operations
Asymptotic complexity of Radix sort is (d·n)
Resume
(d·n) operationstotal ............
(n) operations
Radix sort does not change the order of equal values.
It is a stable sort.
Radix sortA4B33ALG 2011 / 09
16
2 0 4 02 0 1 3
563 8 810 8 10 6 10 83
5 63 3 6 8 8 8 8 10 10 10
3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
Frequencyfrequency.length == k
Inputinput.length == N
k = max(input) - min(input) + 1
Outputoutput.length == N
Counting sortA4B33ALG 2011 / 09
17
Step 1
Fill the frequency array
563 8 810 8 10 6 10 83
2 0 4 02 0 1 3
3 4 5 6 7 8 9 10
0 0 0 00 0 0 0
3 4 5 6 7 8 9 10
One pass through the input array
Reset frequency array
Counting sortA4B33ALG 2011 / 09
18
Step 2
33 44 55 66 77 88 99 10102 0 1 2 0 4 0 3
for(i = low+1; i <= high; i++)freq[i] += freq[i-1]
One pass
Update the frequncy array
Elem freq[ j ] denotes position of the last elem with value jin the output array
1 2 3 4 5 6 7 8 9 10 11 12 5 63 3 6 8 8 8 8 10 10 10
33 44 55 66 77 88 99 10102 2 3 5 5 9 9 12
Frequency array changes its meaning
Counting sortA4B33ALG 2011 / 09
19
Step 3
1 2 3 4 5 6 7 8 9 10 11 12 5
33 44 55 66 77 88 99 10102 2 3 5 5 9 9 12
for(i = N; i > 0; i--) {output[freq[input[i]]] = input[i];freq[input[i]]--;
}
i == N
33 44 55 66 77 88 99 10102 2 2 5 5 9 9 12
563 8 810 8 10 6 10 831 2 3 4 5 6 7 8 9 10 11 12 i == 12
Counting sortA4B33ALG 2011 / 09
20
1 2 3 4 5 6 7 8 9 10 11 12 5
33 44 55 66 77 88 99 10102 2 2 5 5 9 9 12
33 44 55 66 77 88 99 10102 2 2 5 5 8 9 12
563 8 810 8 10 6 10 831 2 3 4 5 6 7 8 9 10 11 12 i == 11
8
Step 3
i == N-1
Counting sort
for(i = N; i > 0; i--) {output[freq[input[i]]] = input[i];freq[input[i]]--;
}
A4B33ALG 2011 / 09
21
1 2 3 4 5 6 7 8 9 10 11 12 5
33 44 55 66 77 88 99 10102 2 2 5 5 8 9 12
33 44 55 66 77 88 99 10102 2 2 5 5 8 9 11
563 8 810 8 10 6 10 831 2 3 4 5 6 7 8 9 10 11 12 i == 10
8 10
Step 3
i == N-2
etc...
Counting sort
for(i = N; i > 0; i--) {output[freq[input[i]]] = input[i];freq[input[i]]--;
}
A4B33ALG 2011 / 09
22
1 2 3 4 5 6 7 8 9 10 11 12 5
33 44 55 66 77 88 99 10101 2 2 3 5 6 9 10
for(i = N; i > 0; i--) {output[freq[input[i]]] = input[i];freq[input[i]]--;
}
33 44 55 66 77 88 99 10101 2 2 4 5 5 9 10
563 8 810 8 10 6 10 831 2 3 4 5 6 7 8 9 10 11 12 i == 3
8 103 6 8 8 8 106
Step 3
i == 3
etc...
Counting sortA4B33ALG 2011 / 09
23
Selection sort
Insertion sort
Bubble sort *)
Quick sort
Merge sort
Heap sort
Radix sort
Counting sort
Worst case Best caseAverage,
"expected"case
(n2) (n2) (n2)
(n2) (n2)(n)
(n2) (n2) (n2)
(n2) (nlog(n))
(nlog(n)) (nlog(n)) (nlog(n))
(nlog(n)) (nlog(n)) (nlog(n))
(n) (n) (n)
(n) (n) (n)
Array size n
Sorts complexities overview
(nlog(n))
No
No **)
No
Yes
Yes
Yes
Yes
Yes
Stable
**) Stable slow versions exist*) Not recommended to use
A4B33ALG 2011 / 09
24
Small sorting experiment
Conclusion
There is no particular sort method which would be optimal in all circumstances.
The performance is influenced by the data size and by the degree of the original organisation (partial order) of the data.
Environment
Intel(R) 1.8 GHz, Microsoft Windows XP SP3, jdk 1.6.0_16.
Organization
Explored the sorts which compare the elements value (double). Each datasets of particular datasizes used in all sorts.
Arrays values generated by the pseudorandom generator.The results are the averages of repeated runs.
A4B33ALG 2011 / 09
25
Select Insert QuickBubble Merge HeapSort
0.00040.0005
0.028
2.36
228
22 900
38 min
0.00010.0002
0.016
1.30
130
12 800
22 min
0.00040.0005
0.043
4.45
450
45 000
75 min
0.00040.0004
0.081
0.12
1.57
18.7
237
0.00070.0009
0.014
0.19
2.40
31.4
385
0.00050.0005
0.011
0.17
2.31
31.4
570
0.026 0.003 0.030 0.010 0.011 0.011
2.31 0.18 2.86 0.16 0.15 0.16
229 17.5 285 1.93 1.68 2.11
38 min 2.9 min 47.5 min 336 301 381
22 900 1 760 28 500 27.4 24.6 25.5
0%90%
1 000 0001 000 000
100 000100 000
10 00010 000
1 0001 000
1010
100100 0%
90%0%90%0%90%0%90%0%90%
Array size%
sorted
Small sorting experiment
Time in milliseconds if not indicated otherwise
Degree of order. The array is first sorted and then x% ofrandomly chosen elements randomly change value.
A4B33ALG 2011 / 09
26
Select Insert QuickBubble Merge HeapSort
11.3
3.4
20
146
1 220
9 960
0.260.7
1.8
11
83
686
5 400
0.961.4
5.4
37.5
287
2 410
19 000
11
1
1
1
1
1
1.82.5
1.75
1.65
1.53
1.7
1.6
1.31.4
1.35
1.4
1.48
1.7
2.41
2.46 0.28 2.9 1 1.07 1.07
15 1.2 18.5 1 0.95 1.03
118 9.1 148 1 0.87 1.09
6 820 521 8 480 1 0.9 1.14
837 64.1 1 040 1 0.9 0.93
Slowest StableSelection and Bubble sort do not compete.
0%90%
1 000 0001 000 000
100 000100 000
10 00010 000
1 0001 000
1010
100100 0%
90%0%90%0%90%0%90%0%90%
Array size % sorted
Small sorting experiment
Fastest
Ratio of slowdown ( >1 ) compared to Quick sort
A4B33ALG 2011 / 09
27
0%90%
Select Insert QuickBubble Merge Heap
1 000 0001 000 000
100 000100 000
10 00010 000
1 0001 000
1010
100100
Sort
0%90%0%90%0%90%0%90%0%90%
0.81
0.91
0.981
1.01
1.01
1.01
0.51
0.21
0.141
0.141
0.141
0.141
0.81
0.681
0.641
0.631
0.631
0.631
11
1.271
1.311
1.231
1.461
1.421
0.81
0.781
0.751
0.71
0.781
0.781
11
11
0.951
0.911
0.811
0.671
Array size % sorted
Small sorting experiment
Ratio of slowdown ( > 1) when comparingthe sort speeds
of unsorted and partially sorted data.
Stable
A4B33ALG 2011 / 09
top related