Lecture 03[Insertion Sort]
-
Upload
zahraa-tishreen -
Category
Documents
-
view
220 -
download
0
Transcript of Lecture 03[Insertion Sort]
-
8/12/2019 Lecture 03[Insertion Sort]
1/20
Design & Analysis of Algorithms
Anwar Ghani
Semester: Spring 2013
Department of Computer Science & Software Engineering,International Islamic University, Islamabad.
-
8/12/2019 Lecture 03[Insertion Sort]
2/20
The Sorting Problem
Input:
A sequence of n numbers a 1, a 2 , . . . , a n
Output:
A permutation (reordering) a 1, a2 , . . . , a n of the
input sequence such that a 1 a2 a n
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 2
-
8/12/2019 Lecture 03[Insertion Sort]
3/20
Insertion Sort like sorting a hand of playing cards
Start with an empty left hand and the cards facingdown on the table.
Remove one card at a time from the table, and insert
compare it with each of the cards already in the hand, fromright to left
The cards held in the left hand are sorted
these cards were originally the top cards of the pile on thetable
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 3
-
8/12/2019 Lecture 03[Insertion Sort]
4/20
To insert 9, we need to
make room for it by movingfirst 22 and then 15.
Insertion Sort
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 4
-
8/12/2019 Lecture 03[Insertion Sort]
5/20
Insertion Sort
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 5
-
8/12/2019 Lecture 03[Insertion Sort]
6/20
Insertion Sort
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 6
-
8/12/2019 Lecture 03[Insertion Sort]
7/20
Insertion Sort
5 2 4 6 1 3
input array
at each iteration, the array is divided in two sub-arrays :
e su -array r g t su -array
sorted unsorted
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 7
-
8/12/2019 Lecture 03[Insertion Sort]
8/20
Insertion Sort
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 8
-
8/12/2019 Lecture 03[Insertion Sort]
9/20
INSERTION-SORTINSERTION-SORT (A)
for j 2 to ndo key A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
a 8a 7a 6a 5a 4a 3a 2a 1
1 2 3 4 5 6 7 8
key
-while i > 0 and A[i] > key
do A[i + 1] A[i]
i i 1A[i + 1] key
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 9
-
8/12/2019 Lecture 03[Insertion Sort]
10/20
Loop Invariant for Insertion SortINSERTION-SORT (A)
for j 2 to n
do key
A[ j ]Insert A[ j ] into the sorted sequence A[1 . . j -1]
i - 1 while i > 0 and A[i] > key
do A[i + 1] A[i]
i
i 1A[i + 1] keyInvariant : at the start of the for loop the elements in A[1 . . j-1]are sorted
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 10
-
8/12/2019 Lecture 03[Insertion Sort]
11/20
Proof Proving loop invariants works like induction
Initialization (base case): It is true prior to the first iteration of the loop
Maintenance (inductive step): ,
the next iteration
Termination: When the loop terminates, the invariant gives us a useful
property that helps show that the algorithm is correct Stop the induction when the loop terminates
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 11
-
8/12/2019 Lecture 03[Insertion Sort]
12/20
Loop Invariant for Insertion Sort Initialization:
Just before the first iteration,
= 2:
The sub array A[1 . . j-1] = A[1],
The element originally in A[1] is sorted
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 12
-
8/12/2019 Lecture 03[Insertion Sort]
13/20
Loop Invariant for Insertion Sort Maintenance: the while inner loop moves A[j -1], A[j -2], A[j -
3], and so on, by one position to the right untilthe proper position for key is found
,position.
So, the sub array A[1..j] is sorted after jth iteration
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 13
-
8/12/2019 Lecture 03[Insertion Sort]
14/20
Loop Invariant for Insertion Sort Termination: The outer for loop ends when j = n + 1 j-1 = n
Replace n with j-1 in the loop invariant: the sub array A[1 . . n] consists of the elements originally in
A[1 . . n], but in sorted order
The entire array is sorted
-
Invariant : at the start of the for loop the elements in A[1 . . j-1]are in sorted order
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 14
-
8/12/2019 Lecture 03[Insertion Sort]
15/20
Analysis
cost timesc1 n
c2 n-10 n-1c4 n-1
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
=n
j jt 2 =
n
j jt
2)1(
c5c6c7
c8 n-1
while i > 0 and A[i] > keydo A[i + 1] A[i]
i i 1
A[i + 1]
key
= n
j jt
2)1(
( ) ( ) )1(11)1()1()( 82
72
62
5421 ++++++=
===
nct ct ct cncncncnT n
j j
n
j j
n
j j
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 15
-
8/12/2019 Lecture 03[Insertion Sort]
16/20
Best Case Analysis Pre-Sorted Array
A[i] keyupon the first time the while loop test is run
(when i = j -1)
t = 1
while i > 0 and A[i] > key
T(n) = c 1n + c 2(n -1) + c 4(n -1) + c 5(n -1) + c 8(n-1)
= (c 1 + c 2 + c 4 + c 5 + c 8)n + (-c 2 - c4 - c5 - c8)
= An + B( ) ( ) )1(11)1()1()( 8
27
26
25421
++++++= ===
nct ct ct cncncncnT n
j j
n
j j
n
j j
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 16
-
8/12/2019 Lecture 03[Insertion Sort]
17/20
Worst Case Analysis Reverse sorted
Always A[i] > key in while loop test
Have to compare key with all elements to the left of the j th position compare with j-1 elements t j = j
( 1) ( 1) ( 1)1 ( 1)
n n nn n n n n n j j j
+ + = => = => =using we have:
a quadratic function of nWhere
1 2 2 j j j= = =
)1(2
)1(
2
)1(1
2
)1()1()1()( 8765421 +
+
+
+
+++= ncnn
cnn
cnn
cncncncnT
)( 8542 C C C C C +++=
( 2 / )765 C C C A ++=( 2 / )7658421 C C C C C C C B ++++=
C Bn An ++= 2
( 2 / )765 C C C A ++=( 2 / )7658421 C C C C C C C B ++++=
( 2 / )765 C C C A ++=
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 17
-
8/12/2019 Lecture 03[Insertion Sort]
18/20
Average Case Analysis
INSERTION-SORT (A)
for j
2 to ndo key A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
cost times
c1 nc2 n-1
0 n-1
i j - 1
while i > 0 and A[i] > key
do A[i + 1] A[i]i i 1
A[i + 1] key
c4 n-1c5
c6c7
c8 n-1
2 / 2
jn
j =
=
n
j j
2)12 / (
= n
j j
2)12 / (
j/2 comparisons
j/2-1 exchanges
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 18
-
8/12/2019 Lecture 03[Insertion Sort]
19/20
Average Case Analysis (Cont..) In average case scenario t j = j/2
12 / 122
= == jt n
j
n
j j2 /
22 jt
n
j
n
j j == = and
)1(2 / 2
)1(2 / 2
)1(2 / 12
)1()1()1()( 8765421 +
+
+
++++= ncnncnncnncncncncnT
C Bn An ++= 2 a quadratic function of n
)2 / ( 5842 C C C C C +++=
( 4 / )7658421 C C C C C C C B ++++=( 4 / )765 C C C A ++=Where
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 19
-
8/12/2019 Lecture 03[Insertion Sort]
20/20
Insertion Sort Advantages
Good running time for Pre-sorted arrays n Disadvantages
n runn ng t me n worst an average case
08-Mar-12 InsertionSort/AOA/Spring 12/Shehzad 20