You are on page 1of 9

Data Structures

SHIFFAN f A THIMA S
19UW30243 ; 19urr108
lli - B.SC (IT)
Contents:-

1. About Insertion sort


2. Way of working
3. Example of insertion sort
4. Algorithm
5. Ask questions If any doubt

.~ -__ - -
·-- - ~

I II

~·2· /"·9
11 i :
I' • .· .'
Insertion sort :-
• Insertion sort is a simple sorting algorithm that worbs similar to the way
you sort playing cards in your hands.
• Insertion sort is the sorting mechanism where the
sorted array is built having one item at a time.

• The array elements are compared with each other


sequentially and then arranged simultaneously in
some particular order.
Way of working ?•
1. Select. II
2. Compare
3. Shift
4. Insert

lj' ', ' '

~s'.- / ,9
11:
11
Example of Insertion sort

I I I I I I I
5 1 6 2 4 3 Lets t.a.ke th.is Array.

Aa we can • • • here. i n
in.eertion eort . we pick u p •

'"'s© 6 2 4 3
key. and compar·• • ii with
elemnt• ■ head of ,t.
and
puts the key In th• n ghl
1 5 @ 2 4 3 place
1
~ 4 3
S has nothing before iL
1 3
2 ~ 1 is compared to 5 and ia
@ ineerted b•fore 5 .
~
1 2 5 6
6 '• gr♦.-ter than 5 and 1

2 is smaller than 6 and 5 .


( Always we start with the second but greater than 1 . so its is
e l ement • • key .) i n.serted after 1 .

And this goes on


Algorithm ·-•
For(l=t ; K n;I++)

{
temp=a(i);
j=l-1;

\Vldk(l>=o && •UI > temp)


{

•IJ+tJ,...UI
J- ;
}

■U+ll-ttmp

l
Ask questions

VVlJen
in doubt
ask
questions•
t/,~ a.ttelftlolf
9 / 9·

You might also like