0% found this document useful (0 votes)
44 views

Data Structures & Algorithms: Resource Person: Zafar Mehmood Khattak

The document discusses different sorting algorithms including insertion sort. Insertion sort works by iterating through an array and inserting each element into the sorted portion of the array by shifting elements of the sorted portion to the right, if necessary, to create space to insert the current element. It is an efficient algorithm for sorting small arrays but has quadratic time complexity for large arrays.

Uploaded by

Vicky Butt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Data Structures & Algorithms: Resource Person: Zafar Mehmood Khattak

The document discusses different sorting algorithms including insertion sort. Insertion sort works by iterating through an array and inserting each element into the sorted portion of the array by shifting elements of the sorted portion to the right, if necessary, to create space to insert the current element. It is an efficient algorithm for sorting small arrays but has quadratic time complexity for large arrays.

Uploaded by

Vicky Butt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Structures & Algorithms

Resource Person:

Zafar Mehmood Khattak


Sorting
 Sorting methods can be divided into two types based upon the
complexity of their algorithms.

 One type of sorting algorithms includes


 Bubble Sort
 Insertion Sort
 Selection Sort

 Other type consists is


 Merge Sort

 Choice of a method depends upon the type and size of data to


be sorted.
LB
Truth in CS Act (proof is ur job)

 NOBODY EVER USES BUBBLE SORT

 NOBODY

 NOT EVER

 BECAUSE IT IS EXTREMELY

INEFFICIENT
Insertion sort
 The insertion sort works just like its name
suggests –
 it inserts each item into its proper place in the
final list.
 The simplest implementation of this requires
two list structures –
 the source list and the list into which sorted items are
inserted.
Insertion sort
 The insertion sort is a good middle-of-the-road choice
for sorting lists of a few thousand items or less.

 Insertion sort is over twice as fast as the bubble sort and

 almost 40% faster than the selection sort. The insertion


sort shouldn't be used for sorting lists larger than a
couple thousand items or repetitive sorting of lists larger
than a couple hundred items.
Insertion sort
 Suppose an array A with n elements
A[1], A[2],……..A[N] in memory.

 The insertion sort algorithm scan A form


A[1] to A[N], inserting each elements
A[K] into its proper position in the
previously sorted sub array A[1], A[2]
……….A[K-1] that is,
Insertion sort
 Pass1: A[1] by itself is trivially sorted.
 Pass 2: A[2] in inserted either before or after A[1] so
that A[1], A[2] is sorted.

 Pass 3: A[3] is inserted into its proper position in A[1],


A[2], so that all the three elements will be sorted.
 Pass 4. A[4] is inserted into its proper place so that…..

 Pass N. A[N] is inserted into its proper place so that


A[1], A[2], …………A[N] is sorted.
Insertion Sort cont…..
• The insertion sort algorithm sorts the list by moving
each element to its proper place

Figure 6: Array list to be sorted

Figure 7: Sorted and unsorted portions of the array list


Insertion Sort Algorithm (Cont’d)

Figure 8: Move list[4] into list[2]

Figure 9: Copy list[4] into temp


Insertion Sort Algorithm (Cont’d)

Figure 10: Array list before copying list[3] into list[4], then
list[2] into list[3]

Figure 11: Array list after copying list[3] into list[4], and then
list[2] into list[3]
Insertion Sort Algorithm (Cont’d)

Figure 12: Array list after copying temp into list[2]


INSERTION SORT
 The basic idea of Insertion Sort for
the items A (1 : n) is as follows:

 A (1) is already sorted


 for j2 to n do
 place A(j) in its correct position in the
sorted set A(1 : j-1)
 repeat.
12
Insertion sort algorithm
 INSERTION ( A, N).
this algorithm sorts the array A with N elements.
1. Set A[0]:= -infinity.
2. Repeat step 3 to 5 for K=: 2 to N
3. Set temp:= A[K] and PTR:= K-1
4. Repeat while TEMP < A[PTR]
a. Set A[PTR+1]:= A[PTR] [moves elements forward]
b. Set PTR:= PTR-1
[end of loop]
5. Set A[PTR + 1]:=TEMP. [insert elements in proper
position]
[end of step 2 loop]
6. exit
INSERTION SORT (Contd..)
Procedure Insertion sort (A, n)

A(0)  -infinity // create a smallest //


// value to exit while loop //

for j2 to n do // A(1 : j-1) is sorted //


Item A(j) ;
ij-1

while item < A (i) do // 0<=i<j //


A (i+1)A(i) ; ii-1
Repeat

A (i+1)item
Repeat
End Insertion sort 14
INSERTION SORT (Contd..)
Example: Sort A = ( 15, 10, 5, 6, 8 )
1, 2, 3, 4, 5
n=5 A(0) = - 
15 is already sorted

Now j2 itemA(2) = 10

10 is to be inserted in (15)

ij-1 = 1 item = 10 < A(i) = 15


so A(i+1 = 2)A(i) i1-1=0

Now item>A(1) = -  so while loop is exited and


A(0+1)=A(1)item
15
INSERTION SORT (Contd..)
So, the current sorted list is (10, 15)
Now j3 itemA(j) =5

5 is to be inserted in (10 15)

i2, item=5<A(1)=15 so
A(i+1)=A(3)A(i)=15, i2-1=1

item=5<A(i)=10 so A(2)10
i0, item = 5 >A(0) = - 
so A(1)5

Similarly 6 is inserted at 2nd place and 8 at 3rd place


So, final sorted list is (5, 6, 8, 10, 15)

16
Algorithm: INSERTIONSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. for i  2 to n
2. x  A[i]
3. j  i - 1
4. while (j >0) and (A[j] > x)
5. A[j + 1]  A[j]
6. j  j - 1
7. end while
8. A[j + 1]  x
9. end for

Example sort : 34 8 64 51 32 21
An Example: Insertion Sort
30 10 40 20 i =  j =  key = 
A[j] =  A[j+1] = 
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 10 40 20 i=2 j=1 key = 10
A[j] = 30 A[j+1] = 10
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20 i=2 j=1 key = 10
A[j] = 30 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20 i=2 j=1 key = 10
A[j] = 30 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20 i=2 j=0 key = 10
A[j] =  A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20 i=2 j=0 key = 10
A[j] =  A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=2 j=0 key = 10
A[j] =  A[j+1] = 10
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=0 key = 10
A[j] =  A[j+1] = 10
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=0 key = 40
A[j] =  A[j+1] = 10
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=0 key = 40
A[j] =  A[j+1] = 10
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=2 key = 40
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=2 key = 40
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=3 j=2 key = 40
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=4 j=2 key = 40
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=4 j=3 key = 20
A[j] = 40 A[j+1] = 20
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20 i=4 j=3 key = 20
A[j] = 40 A[j+1] = 20
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40 i=4 j=3 key = 20
A[j] = 40 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40 i=4 j=3 key = 20
A[j] = 40 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40 i=4 j=3 key = 20
A[j] = 40 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 40
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40 i=4 j=2 key = 20
A[j] = 30 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40 i=4 j=1 key = 20
A[j] = 10 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40 i=4 j=1 key = 20
A[j] = 10 A[j+1] = 30
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40 i=4 j=1 key = 20
A[j] = 10 A[j+1] = 20
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40 i=4 j=1 key = 20
A[j] = 10 A[j+1] = 20
1 2 3 4

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
Done!
Comparison: Assignment of
next class
 Take an array of 1000 items,
compare both the bubble sort and
insertion sort,
 How many comparison will be made each
sorting algorithm
 How many swaps will be made each
sorting algorithm.
 Write a general formula for comparison
and swaps for both the algorithm.

You might also like