You are on page 1of 14

Q1.

Describe insertion sort with algorithm


and example ,assume suitable data?
(W-8,9,12,16)
Q2.Describe the principal of insertion sort
along with example.
Q3.Write the program of insertion sort.
(S-8)
• Insertion sort is a simple sorting algorithm that works similar to the way
you sort playing cards in your hands. The array is virtually split into a
sorted and an unsorted part. Values from the unsorted part are picked
and placed at the correct position in the sorted part.
• Imagine that you are playing a card game. You're holding the cards in
your hand, and these cards are sorted.
• The dealer hands you exactly one new card. You have to put it into the
correct place so that the cards you're holding are still sorted.
• But in our card example, the new card could be smaller than some of the
cards you're already holding, and so you go down the line, comparing
the new card against each card in your hand, until you find the place to
put it.
• You insert the new card in the right place, and once again, your hand
holds fully sorted cards.
• Then the dealer gives you another card, and you repeat the same
procedure.
• Then another card, and another card, and so on, until the dealer stops
giving you cards.
• This is an in-place comparison-based sorting algorithm.
• Here, a sub-list is maintained which is always sorted. For
example, the lower part of an array is maintained to be
sorted.
• An element which is to be 'inserted in this sorted sub-list,
has to find its appropriate place and then it has to be
inserted there. Hence the name, insertion sort.
• The array is searched sequentially and unsorted items are
moved and inserted into the sorted sub-list (in the same
array).
• This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n2), where n is
the number of items.
• Step 1 − If it is the first element, it is already sorted.
• Step 2 − Pick next element
• Step 3 − Compare with all elements in the sorted sub-
list
• Step 4 − Shift all the elements in the sorted sub-list
that is greater than the value to be sorted
• Step 5 − Insert the value
• Step 6 − Repeat until list is sorted
Q= 5, 0,1,9,2,6,4
A= 0,1,2,4,5,6,9
TEMP 4
0 1 2 3 4 5 6
5 0 1 9 2 6 4
0 5 1 9 2 6 4
0 1 5 9 2 6 4 0 1 2 3 4 5 6
0 1 5 9 2 6 4 5 0 1 9 2 6 4
0 1 2 5 9 6 4 0 5 1 9 2 6 4
0 1 2 5 6 9 4 0 1 5 9 2 6 4
0 1 2 4 5 6 9 0 1 5 9 2 6 4
0 1 2 5 9 6 4
0 1 2 5 6 9 4
0 1 2 4 5 6 9
Home Work
Ex.1 Q= 5,2,6,7,1,0,3 A= 0,1,2,3,5,6,7
Ex.2 Q= 16,23,13,9,7,5 A= 5,7,9,13,16,23
Ex.3 Q= 42,23,74,11,65 A= 11,23,42,65,74
Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ; 0 1 2 3 4 5 6
for( i = 1; i < n ; i++)
{ 5 0 1 9 2 6 4
temp =a [i] ;
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--) 0 5 1 9 2 6 4
{
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i =1 i<n
} N=7 1 1<7
temp =a[ i ] temp = 0

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 0
j = 0 0 >= 0 5 > 0 a [ 1] = 5

j = -1 -1 >= 0 a[ 0]= 0
Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ; 0 1 2 3 4 5 6
for( i = 1; i < n ; i++)
{
0 5 1 9 2 6 4
temp =a [i] ;
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
0 1 5 9 2 6 4
{
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=2 i<n
} N=7 2 2<7
temp =a[ i ] temp = 1

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 1
j = 1 1 >= 0 5 > 1 a [ 2] = 5

j = 0 0 >= 0 0 > 1 a[ 1]= 1


Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ;
for( i = 1; i < n ; i++) 0 1 2 3 4 5 6
{
temp =a [i] ; 0 1 5 9 2 6 4
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
{
0 1 5 9 2 6 4
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=3 i<n
} N=7 3 3<7
temp =a[ i ] temp = 9

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 9
J=2 2>=0 5>9 a[ 3]=9
Void insertion _sort (int a[ ] , int n)
{ 0 1 2 3 4 5 6
int i , j, temp ;
for( i = 1; i < n ; i++) 0 1 5 9 2 6 4
{
temp =a [i] ; 0 1 2 5 9 6 4
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
{
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=4 i<n
} N=7 4 4<7
temp =a[ i ] temp = 2

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 2
j = 3 3 >= 0 9 > 2 a [ 4] = 9

j=2 2>=0 5>2 a[3] = 5

J=1 1>=0 1>2 a[2] =2


Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ; 0 1 2 3 4 5 6
for( i = 1; i < n ; i++)
{ 0 1 2 5 9 6 4
temp =a [i] ;
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
0 1 2 5 6 9 4
{
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=5 i<n
} N=7 5 5<7
temp =a[ i ] temp = 6

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 6
J=4 4>=0 9>6 a[5]=9

J=3 3>=0 5>6 a[4]=6


Void insertion _sort (int a[ ] , int n)
{
int i , j, temp ;
for( i = 1; i < n ; i++) 0 1 2 3 4 5 6
{
temp =a [i] ; 0 1 2 5 6 9 4
for ( j = i – 1 ; j >= 0 && a[j] > temp; j--)
{ 0 1 2 4 5 6 9
a[j+1] = a[j] ;
}
a[j+1] = temp;
} Outer for loop i=6 i<n
} N=7 6 6<7
temp =a[ i ] temp = 4

Inner for loop


j = i-1 j >=0 a[ j ] > temp a[j+1]= a[j] a[j+1] =temp
TEMP 4
J=5 5>=0 9>4 a[5]=9

J=4 4>=0 6>4 a[5]=6


J=3 3>=0 5>4 a[4]=5
J=2 2>=0 2>4 a[3]=4

Outer for loop i=7 i<n


N=7 7 7<7

You might also like