















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Sorting Algorithms
2
4
To insert 12, we need to make room for it by moving first 36 and then 24.
5
7
input array
left sub-array (^) right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
8
10
Alg.: INSERTION-SORT (A)
for j ← 2 to n do key ← A[ j ] 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
Invariant : at the start of the for loop the elements in A[1.. j-1] are in sorted order
11
c 1 n c 2 n- 0 n- c 4 n- c 5 c 6 c 7 c 8 n-
(^)
n j 2 tj (^)
n j 2 ( tj^1 ) (^)
n j 2 ( tj^1 )
( ) ( 1 ) ( 1 ) ^1 ^ ^1 ^8 ( 1 ) 2
7 2
6 2
1 2 4 5
T n cn c n c n c t c t c t c n
n
j
j
n
j
j
n
j
j
for j ← 2 to n do key ← A[ j ] 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 tj: # of times the while statement is executed at iteration j
13
a quadratic function of n
1 2 2
( 1) ( 1) (^1) ( 1) ( 1) 2 2 2
n n n j j j j n n^ j n n^ j n n ^ ^ ^ ^ ^ ^ ^ ^
( ) 1 2 ( 1 ) 4 ( 1 ) 5 ( 2 1 ) (^1) 6 ( 2 ^1 ) 7 ( 2 ^1 ) 8 ( 1 )
T n cn c n c n c ^ n n c n n c n n c n
“while i > 0 and A[i] > key”
( ) ( 1 ) ( 1 ) 1 1 8 ( 1 ) 2
7 2
6 2
1 2 4 5
T n cn c n c n c t c t c t c n
n j
j
n j
j
n j
j
using (^) we have:
14
Comparisons and Exchanges in Insertion Sort
for j ← 2 to n
do key ← A[ j ] 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
c 1 n c 2 n- 0 n- c 4 n- c 5
c 6 c 7 c 8 n-
(^)
n j 2 tj (^)
n j 2 ( tj^1 ) (^)
n j 2 ( tj^1 )
16
1 2 3 n
i
8 4 6 9 2 3 1 j
17
8 4 6 9 2 3 1 i = 1 j
8 4 6 9 2 1 3 i = 1 j
8 4 6 9 1 2 3 i = 1 j
8 4 6 1 9 2 3 i = 1 j
8 4 1 6 9 2 3 i = 1 j
8 1 4 6 9 2 3 i = 1 j
1 8 4 6 9 2 3 i = 1 j
1 8 4 6 9 2 3 i = 2 j 1 2 8 4 6 9 3 i = 3 j 1 2 3 8 4 6 9 i = 4 j 1 2 3 4 8 6 9 i = 5 j 1 2 3 4 6 8 9 i = 6 j 1 2 3 4 6 8 9 i = 7 j Docsity.com
19
Thus,T(n) = (n^2 )
2 2 1 1 1
n n n
i i i
Alg.: BUBBLESORT(A) for i 1 to length[A] do for j length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1]
n
i
n i 1
n
i
n i 1
n
i
n i 1
( )
n
i
n i 1
( )
Comparisons: n^2 /
Exchanges: n^2 /
c 1 c 2 c 3 c 4
20