Upload
abdul-waqar
View
301
Download
0
Embed Size (px)
DESCRIPTION
Courtesy: Sir Nabeel Sabir's Share Folder
Citation preview
Adnan Shahzada
• Input:• sequence a1, a2, …, an of numbers.
• Output:
…• permutation a'1, a'2, …, a'n such that a'1 a'2
… a'n .
• Example:
• Input: 8 2 4 9 3 6
• Output: 2 3 4 6 8 9
INSERTION-SORT (A, n) A[1 . . n]for j ← 2 to n
do key ← A[ j]i ← j – 1while i > 0 and A[i] > key
do A[i+1] ← A[i]do A[i+1] ← A[i]i ← i – 1
A[i+1] = key
i j
keysorted
1 n
8 2 4 9 3 6
8 2 4 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
T(n) = (n)T(n) = (n)
T(n)=(n2)
- Expected No of Key Comparisons to insert key j is:
- Total expected number of KC is:- Total expected number of KC is:
- T(n)= (n2)