Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
Introduction to Programming (Java)7/12
Michal Krátký
Department of Computer ScienceTechnical University of Ostrava
Introduction to Programming (Java)2008/2009
c©2006–2008 Michal Krátký Introduction to Programming (Java) 1/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Introduction
Input: order sequence (numbers, words, ...)Output: sorted sequence permutationWhy? More efficient searching
References
D. E. Knuth. The art of computer programming, volume 3Sorting and Searching. Addison-Wesley PublishingCompany, 1973.N. Wirth: Algoritmy a štruktúry údajov, Alfa, Bratislava,1989.J. Dvorský. Základy algoritmizace - sylaby. 2005. In Czech.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 2/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Sorting
we sort an order set Uan item of U is the keyeach key if often a member of a record including otherattribute values
Example:(a) A ∈ N, A = {1, 2, 3, . . .}(b) A ⊆ A1 × A2 × . . .× An, A = {(5606034215,′RottenJohnny′, . . .), (6409045321,′Vedder Eddie′, . . .), . . .}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 3/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Sorting
A sorting method is:
stable – if it preserves relative ordering (from the inputsequence point of view) items with the same key
natural – if the sorting cost depends on the orderliness ofthe input set
in situ/in place – if the method transforms the input using asmall, constant amount of extra storage space
c©2006–2008 Michal Krátký Introduction to Programming (Java) 4/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
A Classification of Sorting Algorithm
Address Sorting – there is a relation between the key andposition in the sorted sequence. No comparison is appliedin this case.
Associative/Comparisons Sorting – comparisons of twokeys is applied during the algorithm run-time
Hybrid Sorting – a combination of both methods
c©2006–2008 Michal Krátký Introduction to Programming (Java) 5/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Associative Sorting
Cost of these algorithms is influenced by two operations:comparison of two itemsswap of items in the memory
The cost is measured by the number of comparisons andswaps.
The comparison is related to key, however whole record may beswapped. Of course it depends on the implementation.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 6/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Permutation
Definition (Permutation).Permutation of a set X with n items is a bijection f : X → X .
An Example:X = {a, b, c, d} and the permutation f : X → X is defined asfollows f (a) = c, f (b) = b, f (c) = d , f (d) = a, then
f =
(a b c dc b d a
)
The number of all permutations of the set with n items is n!
c©2006–2008 Michal Krátký Introduction to Programming (Java) 7/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Inversion of a Permutation
Definition (Inversion of a Permutation).Let f be a permutation of X = {1, . . . , n}. We propose that acouple of different items (i , j) is the inversion of thepermutation f , if: (i − j) · (f (i)− f (j)) < 0.
An Example
f =
(1 2 3 42 4 3 1
)
An Even/Odd permutation means the even/odd number ofinversions. inv(f ) is the number of the permutation inversions.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 8/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Associative Sorting
Efficiency of the algorithm can depend on orderliness ofthe input set
We can measure the orderliness rate by the number ofinversions:
inv(f ) = 0: the input sequence is sortedinv(f ) is maximal, n(n−1)
2 : the input sequence is sorted inthe inverse orderif all permutations are uniformly possible, the average inv(f )is approximately 1
4 n(n − 1)
From this point of view, associative sorting algorithmsremove inversions by the swap operations as long as thesequence is sorted
c©2006–2008 Michal Krátký Introduction to Programming (Java) 9/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Insert Sort
Idea: We take item by item and insert it into the sorted partof the sequence.Array is broken into two parts: sorted part (from 0 to i − 1)and unsorted part (from i to n − 1).We select an item from the unsorted part and insert it inthe sorted part in order to this part was still sorted.This step continues while the unsorted part is empty.In the start of the algorithm the sorted part includes onlyone item.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 10/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Insert Sort, An Example
Input: 44 | 55 12 42 94 18 6 6744 55 | 12 42 94 18 6 6712 44 55 | 42 94 18 6 6712 42 44 55 | 94 18 6 6712 42 44 55 94 | 18 6 6712 18 42 44 55 94 | 6 676 12 18 42 44 55 94 | 67
Output: 6 12 18 42 44 55 67 94 |
Sorted Part →
c©2006–2008 Michal Krátký Introduction to Programming (Java) 11/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.1, setRandom()
/ / Set ar ray ’ s i tems at random values ,/ / the maximal i tem value i s maxValues t a t i c void setRandom ( i n t [ ] data , i n t maxValue ){
for ( i n t i = 0 ; i < data . leng th ; i ++){
data [ i ] = ( i n t ) ( Math . random ( ) ∗ maxValue ) ;}
}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 12/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.1, print()
s t a t i c void p r i n t ( i n t [ ] data ){
for ( i n t i = 0 ; i < data . leng th ; i ++){
System . out . p r i n t ( data [ i ] ) ;i f ( i ! = data . length −1){
System . out . p r i n t ( " , " ) ;}
}System . out . p r i n t l n ( " " ) ;
}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 13/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.1, insertSort()
s t a t i c void i n s e r t S o r t ( i n t [ ] data ){
i n t i , j , v ;for ( i = 0 ; i < data . leng th ; i ++){
v = data [ i ] ;j = i ;while ( ( j > 0 ) & & ( data [ j −1] > v ) ){
data [ j ] = data [ j −1];j−−;
}data [ j ] = v ;
}}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 14/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.1, file Example0701
public class Example0701{
public s t a t i c void main ( S t r i n g [ ] args ){
f i n a l i n t SIZE = 3 0 ;f i n a l i n t MAX_VALUE = 1000 ;i n t [ ] data = new i n t [ SIZE ] ;
setRandom ( data , MAX_VALUE) ;p r i n t ( data ) ;i n s e r t S o r t ( data ) ;p r i n t ( data ) ;
}
/ / methods . . .}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 15/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Insert Sort, Const Analysis 1/2
The number of comparisons in the i-th step Ci : 1 ≤ Ci ≤ i − 1.If all permutations of n keys are uniformly possible: Ci = i/2.
The number of swaps Si = Ci .
The overall number of comparisons and swaps are as follows:
Cmin = n − 1 Smin = 2(n − 1)
Cavg = 14(n2 + n + 2) Savg = 1
4(n2 + 9n − 10)
Cmax = 12(n2 + n)− 1 Smax = 1
2(n2 + 3n − 4)
c©2006–2008 Michal Krátký Introduction to Programming (Java) 16/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Insert Sort, Const Analysis 2/2
If the input sequence is sorted, Cmin and Smin appear. Wecall this case as the best case.
On the other hand, if the input sequence is sorted in theinverse order, Cmax and Smax appear. We call this case asthe worst case.
Consequently, this algorithm is natural and stable
c©2006–2008 Michal Krátký Introduction to Programming (Java) 17/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Binary Insert Sort
Obviously, the right position of the current item is possibleto find by the binary searching
In this way, we can write the binary insert sort algorithm
c©2006–2008 Michal Krátký Introduction to Programming (Java) 18/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.2, binaryInsertSort() 1/3
s t a t i c void b i n a r y I n s e r t S o r t ( i n t [ ] data ){
i n t i , j , value ;i n t low , hi , m;
for ( i = 1 ; i < data . leng th ; i ++){
value = data [ i ] ;
c©2006–2008 Michal Krátký Introduction to Programming (Java) 19/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.2, binaryInsertSort() 2/3
/ / f i n d the r i g h t p o s i t i o n/ / by the b inary searching a lgo r i t hmlow =0;h i = i ;while ( low < h i ){
m = ( low + h i ) / 2 ;i f ( data [m] < = value ){
low = m + 1 ;}else{
h i = m;}
} / / wh i le
c©2006–2008 Michal Krátký Introduction to Programming (Java) 20/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.2, binaryInsertSort() 3/3
for ( j = i ; j > h i ; j −−) / / swap i tems{
data [ j ] = data [ j −1];}data [ h i ] = value ;
} / / f o r} / / b i n a r y I n s e r t S o r t
c©2006–2008 Michal Krátký Introduction to Programming (Java) 21/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.2, file Example0702
public class Example0702{
public s t a t i c void main ( S t r i n g [ ] args ){
f i n a l i n t SIZE = 3 0 ;f i n a l i n t MAX_VALUE = 1000 ;i n t [ ] data = new i n t [ SIZE ] ;
setRandom ( data , MAX_VALUE) ;p r i n t ( data ) ;b i n a r y I n s e r t S o r t ( data ) ;p r i n t ( data ) ;
}
/ / methods . . .}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 22/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Binary Insert Sort, Cost Analysis
The number of comparisons is as follows:
C =n∑
i=1
dlog ie
However, the number of swaps is the same: O(n2).
c©2006–2008 Michal Krátký Introduction to Programming (Java) 23/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Select Sort
Idea: We select the minimal item from the unsorted part ofthe array.
This item is inserted into the sorted part as the last item.
Consequently, the size of the sorted part in increased by 1.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 24/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Select Sort, Example
Input: 44 55 12 42 94 18 6 676 | 55 12 42 94 18 44 676 12 | 55 42 94 18 44 676 12 18 | 42 94 55 44 676 12 18 42 | 94 55 44 676 12 18 42 44 | 55 94 676 12 18 42 44 55 | 94 67
Output: 6 12 18 42 44 55 67 | 94
Sorted Part →
c©2006–2008 Michal Krátký Introduction to Programming (Java) 25/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.3, setRandom()
/∗ ∗∗ Set random values i n the array ,∗ maximal value < maxValue .∗ ∗ /
s t a t i c void setRandom ( double [ ] data ,double maxValue )
{for ( i n t i = 0 ; i < data . leng th ; i ++){
data [ i ] = Math . random ( ) ∗ maxValue ;}
}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 26/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.3, print()
/ / p r i n t the ar rays t a t i c void p r i n t ( double [ ] data ){
for ( i n t i = 0 ; i < data . leng th ; i ++){
System . out . p r i n t ( data [ i ] ) ;i f ( i ! = data . length −1){
System . out . p r i n t ( " , " ) ;}
}System . out . p r i n t l n ( " " ) ;
}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 27/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.3, selectSort() 1/2
s t a t i c void s e l e c t S o r t ( double [ ] data ){
i n t i , j , min ;double t ;
for ( i = 0 ; i < data . leng th ; i ++){
min = i ;
c©2006–2008 Michal Krátký Introduction to Programming (Java) 28/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.3, selectSort() 2/2
for ( j = i + 1 ; j < data . leng th ; j ++){
i f ( data [ j ] < data [ min ] ){
min = j ;}
}t = data [ min ] ;data [ min ] = data [ i ] ;data [ i ] = t ;
}}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 29/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.3, file Example0703.java
public class Example0703{
public s t a t i c void main ( S t r i n g [ ] args ){
f i n a l i n t SIZE = 2 0 ;f i n a l double MAX_VALUE = 1 0 0 . 0 ;
double [ ] data = new double [ SIZE ] ;setRandom ( data , MAX_VALUE) ;p r i n t ( data ) ;s e l e c t S o r t ( data ) ;p r i n t ( data ) ;
}
/ / methods . . .}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 30/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Select Sort, Cost Analysis
The number of comparisons C does not depend on the initialordering. In this way, this algorithm is less natural then InsertSort.
C =12(n2 − n)
The number of swaps S:
Smin = 3(n − 1)
Smax =
⌈(n2
4
)⌉+ 3(n − 1)
Savg.= n(ln n + γ)
Select Sort is commonly supposed as more efficient than InsertSort, exception a sorted or almost sorted sequence.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 31/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Shell Sort
Obviously, the number of inversions is decreased by one ifwe change the adjacent items.
Therefore, the complexity is quadratic in the average andworst cases.
Obviously, if more faraway items are changed, the numberof inversions falls quickly to zero.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 32/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Shell Sort
Shell Sort, D. L. Shell, 1959
We sort items in the hi distance for the i-th iteration,i = t , t − 1, . . . , 0, hi+1 > hi , h1 = 0, t > 0
The hi value is the i-th step of the method
Result: This algorithm sorts shorted sequences for the firstiterations, it sorts longer sequences to be more sorted forthe next iterations
c©2006–2008 Michal Krátký Introduction to Programming (Java) 33/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Shell Sort
Obviously, the cost of Shell Sort depends on a sequence ofsteps, there are the following step sequences:
A: h1 = 1, hi+1 = 2 ∗ hi + 1 – Hibbard
B: h1 = 1, h2 = 3, hi+1 = 2 ∗ hi − 1 (for i > 2)
C: h1 = 1, hi+1 = 3 ∗ hi + 1 – D. Knuth, 19691, 4, 13, 40, 121, 364,1093, 3280, 9841, ...
D: h1 = 1, hi+1 = 4i+1 + 3 ∗ 2i + 1R. Sedgewick (20–30% faster than C)1, 8, 23, 77, 281, 1073, 4193, 16577, ...
The complete cost analysis is still an open issue.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 34/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Shell Sort, Example
Array Index: 0 1 2 3 4 5 6 7
Input: 44 18 12 42 94 55 6 67 h = 4, i = 644 18 6 42 94 55 12 67 h = 1, i = 118 44 6 42 94 55 12 67 h = 1, i = 26 18 44 42 94 55 12 67 h = 1, i = 36 18 42 44 94 55 12 67 h = 1, i = 56 18 42 44 55 94 12 67 h = 1, i = 66 12 18 42 44 55 94 67 h = 1, i = 7
Output: 6 12 18 42 44 55 67 94
c©2006–2008 Michal Krátký Introduction to Programming (Java) 35/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.4, shellSort() 1/2
s t a t i c void s h e l l S o r t ( i n t data [ ] ){
i n t i , j , h , value ;
for ( h = 1 ; h <= data . leng th ; h = 3∗h +1) ;h / = 3 ;
do{
c©2006–2008 Michal Krátký Introduction to Programming (Java) 36/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.4, shellSort() 2/2
for ( i = h ; i < data . leng th ; i ++){
value = data [ i ] ;j = i ;while ( ( j >= h ) & & ( data [ j−h ] > value ) ){
data [ j ] = data [ j−h ] ;j −= h ;
} / / wh i ledata [ j ] = value ;
p r i n t ( data ) ;} / / f o rh / = 3 ;
} while ( h ! = 0 ) ;} / / s h e l l S o r t
c©2006–2008 Michal Krátký Introduction to Programming (Java) 37/38
Introduction to Programming (Java) 7/12Sorting I – Associative Sorting
IntroductionInsert SortSelect SortShell Sort
Example 7.4, file Example0704.java
public class Example0704{
public s t a t i c void main ( S t r i n g [ ] args ){
f i n a l i n t SIZE = 3 0 ;f i n a l i n t MAX_VALUE = 1000 ;i n t [ ] data = new i n t [ SIZE ] ;
setRandom ( data , MAX_VALUE) ;p r i n t ( data ) ;s h e l l S o r t ( data ) ;p r i n t ( data ) ;
}
/ / methods . . .}
c©2006–2008 Michal Krátký Introduction to Programming (Java) 38/38