Professional Documents
Culture Documents
Lecture 2
Lecture 2
• Input:
▪ A sequence of n numbers a1, a2, . . . , an
• Output:
▪ A permutation (reordering) a1’, a2’, . . . , an’ of the
5 2 4 6 1 3
sorted unsorted
for j ← 2 to n a1 a2 a3 a4 a5 a6 a7 a8
do key ← A[ j ]
ke y
// Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j- 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i– 1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
i←j- 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
IT3103, Dept of IT,SIT, MUJ
9
Loop Invariant for Insertion Sort
• Initialization:
▪ Just before the first iteration, j =
2:
the subarray A[1 . . j-1] = A[1],
(the element originally in A[1]) –
is sorted
c6
n
do A[i + 1] ← A[i] j=2
(t j −1)
c7
n
i←i–1 j=2
(t j −1)
A[i + 1] ← key c8 n-1
tj: # of times the while statement is executed at iteration j
T (n) = c1n + c 2 (n −1) + c 4 (n −1) + c 5 t j + c6 (t j −1)+c 7 (t j −1)+ c8 (n −1)
n n n
▪ A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
▪ tj = 1
• T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) =
(c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8)
= an + b = (n)
• Advantages
▪ Good running time for “almost sorted” arrays (n)
• Disadvantages
▪ (n2) running time in worst and average case
▪ n2/2 comparisons and exchanges
8 4 6 9 2 3 1
j
8 4 6 9 2 1 3 1 2 8 4 6 9 3
i=1 j i=3 j
8 4 6 9 1 2 3 1 2 3 8 4 6 9
i=1 j i=4 j
8 4 6 1 9 2 3 1 2 3 4 8 6 9
i=1 j i=5 j
8 4 1 6 9 2 3 1 2 3 4 6 8 9
i=1 j i=6 j
8 1 4 6 9 2 3 1 2 3 4 6 8 9
i=1 j i=7
j
1 8 4 6 9 2 3
i =IT3103,
CS11501,Dept
jDeptofofIT,SIT,
CSE,SCIT,
MUJ MUJ
49
Bubble Sort
Alg.: BUBBLESORT(A)
for i 1 to length[A]
do for j length[A] downto i + 1
do if A[j] < A[j -1]
i then exchange A[j] A[j-1]
8 4 6 9 2 3 1
i=1 j
IT3103,
CS1501,Dept
DeptofofIT,SIT, MUJ MUJ
CSE,SCIT,
50
Bubble-Sort Running Time
Alg.: BUBBLESORT(A)
for i 1 to length[A] c1
do for j length[A] downto i + 1 c2
do if A[j] < A[j -1] c3
then exchange A[j] A[j-1] c4
n
(n
n n
T(n) = c1(n+1) + c2 (n − i c3 (n − i) + c4
i=1 i=1 i=
n
= (n) + (c2 + c2 + c4) (n − i)
i=1
n n
+1)
n
n 2
n
where (n − i) = n − i = n −
2 n(n = −
i=1 i=1 i=1 2 2 2
Thus,T(n) = (n2)
IT3103, Dept of IT,SIT, MUJ
51
Selection Sort
• Idea:
▪ Find the smallest element in the array
▪ Exchange it with the element in the first position
▪ Find the second smallest element and exchange it
with the element in the second position
▪ Continue until the array is sorted
• Disadvantage:
▪ Running time depends only slightly on the amount
of order in the file
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
for i ← j + 1 to n c4 n−1
j=
(n −
then smallest ← i c6
n−1
j=1
(n − j)
CS1501
Design & Analysis of Algorithm
56
IT3103, Dept of IT,SIT, MUJ
How Fast Can We Sort?
– Radix Sort
– Bucket sort
output array
(i.e., frequencies)
(r=6)
A 2 5 3 0 2 3 0 3
0 1 2 3 4 5
Cnew 2 2 4 7 7 8
IT3103, Dept of IT,SIT, MUJ
Example
1 2 3 4 5 6 7 8 0 1 2 3 4 5
A 2 5 3 0 2 3 0 3 Cnew 2 2 4 7 7 8
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 3 B 0 3
0 1 2 3 4 5
0 1 2 3 4 5
Cnew 2 2 4 6 7 8 Cnew 1 2 4 6 7 8
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 3 3 B 0 2 3 3
0 1 2 3 4 5
0 1 2 3 4 5
Cnew 1 2 4 5 7 8 Cnew 1 2 3 5 7 8
A 2 5 3 0 2 3 0 3
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 0 2 3 3 B 0 0 2 3 3 3 5
0 1 2 3 4 5 0 1 2 3 4 5
C 0 2 3 5 7 8 C 0 2 3 4 7 7
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 0 2 3 3 3 B 0 0 2 2 3 3 3 5
0 1 2 3 4 5
C 0 2 3 4 7 8
A
Alg.: COUNTING-SORT(A, B, n, k) 0 k
1. for i ← 0 to r C
2. do C[ i ] ← 0 1 n
B
3. for j ← 1 to n
4. do C[A[ j ]] ← C[A[ j ]] + 1
5. C[i] contains the number of elements equal to i
6. for i ← 1 to r
7. do C[ i ] ← C[ i ] + C[i -1]
8. C[i] contains the number of elements ≤ i
9. for j ← n downto 1
10. do B[C[A[ j ]]] ← A[ j ]
11. C[A[ j ]] ← C[A[ j ]] - 1
IT3103, Dept of IT,SIT, MUJ
Analysis of Counting Sort
Alg.: COUNTING-SORT(A, B, n, k)
1. for i ← 0 to r
O(r)
2. do C[ i ] ← 0
3. for j ← 1 to n
O(n)
4. do C[A[ j ]] ← C[A[ j ]] + 1
5. C[i] contains the number of elements equal to i
6. for i ← 1 to r
O(r)
7. do C[ i ] ← C[ i ] + C[i -1]
8. C[i] contains the number of elements ≤ i
9. for j ← n downto 1
10. do B[C[A[ j ]]] ← A[ j ] O(n)
• Example: key=15
5 .72 4 / Distribute
6 .94 5 / Into buckets
7 .21 6 .68 /
9 .23 8 /
10 .68 9 .94 /
0 /
1 .12 .17 /
3 .39 /
Sort within each
4 /
bucket
5 /
6 .68 /
7 .72 .78 /
8 /
9 .94 /
IT3103, Dept of IT,SIT, MUJ
Example - Bucket Sort
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /
0 /
1 .12 .17 /
3 .39 /
4 /
7 .72 .78 /
8 /
9 .94 /
IT3103, Dept of IT,SIT, MUJ
Analysis of Bucket Sort
Alg.: BUCKET-SORT(A, n)
for i ← 1 to n
O(n)
do insert A[i] into list B[nA[i]]
for i ← 0 to k - 1
k O(n/k log(n/k))
do sort list B[i] with quicksort sort =O(nlog(n/k)