03 DS and Algorithm Session 04
-
Upload
mohit-gorhe -
Category
Documents
-
view
226 -
download
2
Transcript of 03 DS and Algorithm Session 04
-
8/9/2019 03 DS and Algorithm Session 04
1/120
Data Structures and Algorithms
Session 4Ver. 1.0
Objectives
In this session, you will learn to:
Identify the algorithms that can be used to sort data
Sort data by using bubble sort
Sort data by using selection sort
Sort data by using insertion sortSort data by using shell sort
-
8/9/2019 03 DS and Algorithm Session 04
2/120
Data Structures and Algorithms
Session 4Ver. 1.0
Sorting Data
Suppose, you have to invite your friends and relatives for
your birthday party. For this, you need to give them a call.
You are looking for the telephone number of a friend named
Steve in a telephone directory with 1, records.
!owever, the records are stored randomly and not in anyparticular order.
-
8/9/2019 03 DS and Algorithm Session 04
3/120
Data Structures and Algorithms
Session 4Ver. 1.0
Sorting Data (Contd.)
"o search for the telephone number of your friend in such a
directory, you would need to scan each entry in the directory
in a se#uential manner.
"his would be very time consuming.
!ow can you solve this problem$
-
8/9/2019 03 DS and Algorithm Session 04
4/120
Data Structures and Algorithms
Session 4Ver. 1.0
% simple solution to save time, and search records
efficiently is sorting.
Sorting is the process of arranging data in some pre&defined
order or se#uence. "he order can be either ascending or
descending.If the data is ordered, you can directly go to the section that
stores the names starting with 'S(, thereby reducing the
number of records to be traversed.
Sorting Data (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
5/120
Data Structures and Algorithms
Session 4Ver. 1.0
Sorting is implemented in a program by using an algorithm.
Some sorting algorithms are:
)ubble sort
Selection sort
Insertion sortShell sort
*erge sort
+uick sort
!eap sort
"o select an appropriate algorithm, you need to consider thefollowing:
-ecution time
Storage space
rogramming effort
Selecting a Sorting Algorithm
-
8/9/2019 03 DS and Algorithm Session 04
6/120
Data Structures and Algorithms
Session 4Ver. 1.0
)ubble sort algorithm:
Is one of the simplest sorting algorithms
!as a #uadratic order of growth and is therefore suitable for
sorting small lists only
/orks by repeatedly scanning through the list, comparingad0acent elements, and swapping them if they are in the wrong
order
Sorting Data by Using Bubble Sort
-
8/9/2019 03 DS and Algorithm Session 04
7/120
Data Structures and Algorithms
Session 4Ver. 1.0
"o understand the implementation of bubble sort algorithm,
consider an unsorted list of numbers stored in an array.
Imlementing Bubble Sort Algorithm
arr
210 43
2 6 75 3
-
8/9/2019 03 DS and Algorithm Session 04
8/120
Data Structures and Algorithms
Session 4Ver. 1.0
et us sort this unsorted list.
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
2 6 75 3
-
8/9/2019 03 DS and Algorithm Session 04
9/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
4ompare the element stored at inde- with the element
stored at inde- 1.
arr
210 43
2 6 75 3
Imlementing Bubble Sort Algorithm (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
10/120
-
8/9/2019 03 DS and Algorithm Session 04
11/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
4ompare the element stored at inde- 1 with the element
stored at inde- 5 and swap the values if the value at inde- 1
is greater than the value at inde- 5.
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 7 32 5
No Change
-
8/9/2019 03 DS and Algorithm Session 04
12/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
4ompare the element stored at inde- 5 with the element
stored at inde- 6 and swap the values if the value at inde- 5
is greater than the value at inde- 6.No Change
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 7 32 5
-
8/9/2019 03 DS and Algorithm Session 04
13/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
4ompare the element stored at inde- 6 with the element
stored at inde- 7 and swap the values if the value at inde- 6
is greater than the value at inde- 7.Swap
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 7 32 5 3 7
-
8/9/2019 03 DS and Algorithm Session 04
14/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
4ompare the element stored at inde- 6 with the element
stored at inde- 7 and swap the values if the value at inde- 6
is greater than the value at inde- 7.
Largest element is placed at its correct position ater !ass 1
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 3 72 5
-
8/9/2019 03 DS and Algorithm Session 04
15/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
4ompare the element stored at inde- with the element
stored at inde- 1 and swap the values if the value at inde-
is greater than the value at inde- 1.No Change
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 3 72 5
-
8/9/2019 03 DS and Algorithm Session 04
16/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
4ompare the element stored at inde- 1 with the element
stored at inde- 5 and swap the values if the value at inde- 1
is greater than the value at inde- 5.No Change
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 3 72 5
-
8/9/2019 03 DS and Algorithm Session 04
17/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
4ompare the element stored at inde- 5 with the element
stored at inde- 6 and swap the values if the value at inde- 5
is greater than the value at inde- 6.Swap
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
6 3 72 5 3 6
-
8/9/2019 03 DS and Algorithm Session 04
18/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
4ompare the element stored at inde- 5 with the element
stored at inde- 6 and swap the values if the value at inde- 5
is greater than the value at inde- 6.
Second largest element is placed at its correct position ater !ass 2
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
3 6 72 5
-
8/9/2019 03 DS and Algorithm Session 04
19/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 6
n 2 3
4ompare the element stored at inde- with the element
stored at inde- 1 and swap the values if the value at inde-
is greater than the value at inde- 1.No Change
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
3 6 72 5
-
8/9/2019 03 DS and Algorithm Session 04
20/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 6
n 2 3
4ompare the element stored at inde- 1 with the element
stored at inde- 5 and swap the values if the value at inde- 1
is greater than the value at inde- 5.Swap
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
3 6 72 53 5
-
8/9/2019 03 DS and Algorithm Session 04
21/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 6
n 2 3
4ompare the element stored at inde- 5 with the element
stored at inde- 6 and swap the values if the value at inde- 5
is greater than the value at inde- 6.
"hird largest element is placed at its correct position ater !ass 3
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
5 6 72 3
-
8/9/2019 03 DS and Algorithm Session 04
22/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
4ompare the element stored at inde- with the element
stored at inde- 1 and swap the values if the value at inde-
is greater than the value at inde- 1.No Change
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
5 6 72 3
-
8/9/2019 03 DS and Algorithm Session 04
23/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
4ompare the element stored at inde- with the element
stored at inde- 1 and swap the values if the value at inde-
is greater than the value at inde- 1.
#o$rth largest element is placed at its correct position ater !ass 4
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
5 6 72 3
-
8/9/2019 03 DS and Algorithm Session 04
24/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
%t the end of ass 7, the elements are sorted.
Imlementing Bubble Sort Algorithm (Contd.)
arr
210 43
5 6 72 3
-
8/9/2019 03 DS and Algorithm Session 04
25/120
Data Structures and Algorithms
Session 4Ver. 1.0
/rite an algorithm to implement bubble sort.
%lgorithm for bubble sort:
1. Set pass 2 1.
5. 8epeat step 6 varying 0 from to n 9 1 9 pass.
6. If the element at inde- 0 is greater than the element at inde-0 1, swap the two elements.
7. Increment passby 1.
3. If pass;2 n 9 1, go to step 5.
Imlementing Bubble Sort Algorithm (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
26/120
Data Structures and Algorithms
Session 4Ver. 1.0
"he efficiency of a sorting algorithm is measured in terms of
number of comparisons.
In bubble sort, there are n91 comparisons in ass 1, n 95
comparisons in ass 5, and so on.
"otal number of comparisons 2
-
8/9/2019 03 DS and Algorithm Session 04
27/120
Data Structures and Algorithms
Session 4Ver. 1.0
/hat is the order of growth of the bubble sort algorithm$
$ust a minute
%nswer:
"he bubble sort algorithm has a #uadratic order of growth.
-
8/9/2019 03 DS and Algorithm Session 04
28/120
Data Structures and Algorithms
Session 4Ver. 1.0
/hile implementing bubble sort algorithm, how many
comparisons will be performed in ass 1.
$ust a minute
%nswer:
n 91 comparisons
-
8/9/2019 03 DS and Algorithm Session 04
29/120
Data Structures and Algorithms
Session 4Ver. 1.0
roblem Statement:
/rite a program to store the marks of 1 students in an array.
Include a function to sort the elements of the array by using
the bubble sort algorithm. %fter sorting the array, display the
sorted list.
Activity% Sorting Data by Using Bubble Sort Algorithm
-
8/9/2019 03 DS and Algorithm Session 04
30/120
Data Structures and Algorithms
Session 4Ver. 1.0
Selection sort algorithm:
!as a #uadratic order of growth and is therefore suitable for
sorting small lists only
Scans through the list iteratively, selects one item in each
scan, and moves the item to its correct position in the list
Sorting Data by Using Selection Sort
-
8/9/2019 03 DS and Algorithm Session 04
31/120
Data Structures and Algorithms
Session 4Ver. 1.0
"o understand the implementation of selection sort
algorithm, consider an unsorted list of numbers stored in an
array.
Imlementing Selection Sort Algorithm
arr
210 43
120 10 200105 20
-
8/9/2019 03 DS and Algorithm Session 04
32/120
Data Structures and Algorithms
Session 4Ver. 1.0
et us sort this unsorted list.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 10 200105 20
-
8/9/2019 03 DS and Algorithm Session 04
33/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
Search the minimum value in the array, arrAB to arrAn 9 1B.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 10 200105 20
min
-
8/9/2019 03 DS and Algorithm Session 04
34/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
Search the minimum value in the array, arrAB to arrAn 9 1B.
Swap the minimum value with the value at inde- .
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 10 200105 20
min
Swap
10510
S d l i h
-
8/9/2019 03 DS and Algorithm Session 04
35/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
n 2 3
Search the minimum value in the array, arrAB to arrAn 9 1B.
Swap the minimum value with the value at inde- .
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 105 20010 20
"he smallest %al$e is placed at its correct location ater !ass 1
D S d Al i h
-
8/9/2019 03 DS and Algorithm Session 04
36/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
Search the minimum value in the array, arrA1B to arrAn 9 1B.
Swap the minimum value with the value at inde- 1.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 105 20010 20
min
20 120
Swap
D t St t d Al ith
-
8/9/2019 03 DS and Algorithm Session 04
37/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 5
n 2 3
Search the minimum value in the array, arrA1B to arrAn 9 1B.
Swap the minimum value with the value at inde- 1.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200 120
"he second smallest %al$e is placed at its correct location ater
!ass 2
10510
D t St t d Al ith
-
8/9/2019 03 DS and Algorithm Session 04
38/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 6
n 2 3
Search the minimum value in the array, arrA5B to arrAn91B.
Swap the minimum value with the value at inde- 5.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200
min
10 120105
D t St t d Al ith
-
8/9/2019 03 DS and Algorithm Session 04
39/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 6
n 2 3
Search the minimum value in the array, arrA5B to arrAn91B.
Swap the minimum value with the value at inde- 5.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200
min"he third smallest %al$e is placed at its correct location ater !ass 3
12010510
D t St t d Al ith
-
8/9/2019 03 DS and Algorithm Session 04
40/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
Search the minimum value in the array, arrA6B to arrAn 9 1B.
Swap the minimum value with the value at inde- 6.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200 12010 105
min
120 200
Swap
D t St t d Al ith
-
8/9/2019 03 DS and Algorithm Session 04
41/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
Search the minimum value in the array, arrA6B to arrAn91B.
Swap the minimum value with the value at inde- 6.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20
"he o$rth smallest %al$e is placed at its correct location ater !ass 4
120 20010 105
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
42/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 7
n 2 3
"he list is now sorted.
Imlementing Selection Sort Algorithm(Contd.)
arr
210 43
120 200 2010510arr
210 43
20 20010 105 120
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
43/120
Data Structures and Algorithms
Session 4Ver. 1.0
/rite an algorithm to implement selection sort.
%lgorithm for selection sort:
1. 8epeat steps 5 and 6 varying 0 from to n 9 5
5. Find the minimum value in arrA0B to arrAn 91B:
a. Set minCinde- 2 0
b. 8epeat step c varying i from 0 1 to n 9 1
c. If arrAiB ; arrAminCinde-B:
i. minCinde- 2 i
6. Swap arrA0B with arrAminCinde-B
Imlementing Selection Sort Algorithm(Contd.)
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
44/120
Data Structures and Algorithms
Session 4Ver. 1.0
In selection sort, there are n 9 1 comparisons during ass 1
to find the smallest element, n 9 5 comparisons during ass
5 to find the second smallest element, and so on.
"otal number of comparisons 2
-
8/9/2019 03 DS and Algorithm Session 04
45/120
Data Structures and Algorithms
Session 4Ver. 1.0
8ead the following statement and specify whether it is true
or false:
"he efficiency of selection sort algorithm is same as that of the
bubble sort algorithm.
$ust a minute
%nswer:
"rue
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
46/120
Data Structures and Algorithms
Session 4Ver. 1.0
!ow many comparisons are performed in the second pass
of the selection sort algorithm$
$ust a minute
%nswer:
n 95
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
47/120
Data Structures and Algorithms
Session 4Ver. 1.0
Insertion sort algorithm:
!as a #uadratic order of growth and is therefore suitable for
sorting small lists only
Is much more efficient than bubble sort, and selection sort, if
the list that needs to be sorted is nearly sorted
Sorting Data by Using Insertion Sort
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
48/120
Data Structures and Algorithms
Session 4Ver. 1.0
"o understand the implementation of insertion sort
algorithm, consider an unsorted list of numbers stored in an
array.
Imlementing Insertion Sort Algorithm
210 43
70 1030&0 20arr
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
49/120
Data Structures and Algorithms
Session 4Ver. 1.0
"o sort this list by using the insertion sort algorithm:
You need to divide the list into two sublists, sorted and
unsorted.
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
50/120
Data Structures and Algorithms
Session 4Ver. 1.0
"o sort this list by using the insertion sort algorithm:
You need to divide the list into two sublists, sorted and
unsorted.
Initially, the sorted list has the first element and the unsorted
list has the remaining 7 elements.
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
0
70 1030&0 20
21 43
-
8/9/2019 03 DS and Algorithm Session 04
51/120
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
52/120
Data Structures and Algorithms
Session 4Ver. 1.0
ass 1
lace the first element from the unsorted list at its correct
position in the sorted list.
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
10
70 &0
32
30 10 20
4
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
53/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
10
70 &0
32
30 10 20
4
ass 5
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
54/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
30 70 &0 2010
ass 5
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
55/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
30 70 &0 2010
ass 6
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
56/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
10 10
30 70 &0
2
10
3
20
4
ass 6
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
57/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
10 10
30 70 &0
2
10
3
20
4
ass 7
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
58/120
Data Structures and Algorithms
Session 4Ver. 1.0
Imlementing Insertion Sort Algorithm(Contd.)
Sorted ist Dnsorted ist
210 43
10 703020 &0
ass 7
lace the first element from the unsorted list at its correct
position in the sorted list.
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
59/120
Data Structures and Algorithms
Session 4Ver. 1.0
et us now write an algorithm to
implement insertion sort algorithm.
Imlementing Insertion Sort Algorithm(Contd.)
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
210 43
70 1030&0 20arr
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
60/120
Data Structures and Algorithms
Session 4Ver. 1.0
n 2 3
i 2 1
Imlementing Insertion Sort Algorithm(Contd.)
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
210 43
70 1030&0 20arr
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
61/120
a a S uc u es a d go s
Session 4Ver. 1.0
n 2 3
i 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
62/120
g
Session 4Ver. 1.0
n 2 3
i 2 1
temp 2
Imlementing Insertion Sort Algorithm(Contd.)
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
210 43
70 1030&0 20arr
i
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
63/120
g
Session 4Ver. 1.0
n 2 3
i 2 1
temp 2
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
64/120
g
Session 4Ver. 1.0
n 2 3
i 2 1
temp 2
arrA0B ; temp
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
65/120
g
Session 4Ver. 1.0
n 2 3
i 2 1
temp 2
arrA0B ; temp
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
Val$e temp is stored at its correct
position in the sorted list
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
66/120
g
Session 4Ver. 1.0
n 2 3
i 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
67/120
g
Session 4Ver. 1.0
n 2 3
i 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
68/120
g
Session 4Ver. 1.0
n 2 3
i 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
69/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
70/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
71/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
72/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 1030&0 20arr
i'
&0
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes lessthan or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
73/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 10&0 20arr
i''
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
74/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
70 10&0 20arr
i'
70
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
75/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
0 2 91
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr
i'
70
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
76/120
Session 4Ver. 1.0
n 2 3
i 2 5
temp 2 6
0 2 91
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr
i
7030
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
77/120
Session 4Ver. 1.0
n 2 3
i 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
78/120
Session 4Ver. 1.0
n 2 3
i 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
79/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
80/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
81/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
82/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
10&0 20arr 7030
i'
&0
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
83/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 7030
i''
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
84/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 7030
i'
70
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
85/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 30
i'
70
'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
86/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 30
i
70
'
30
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
87/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
0 2 91
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr
i
70
'
30
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
88/120
Session 4Ver. 1.0
n 2 3
i 2 6
temp 2 1
0 2 91
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr
i
703010
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
89/120
Session 4Ver. 1.0
n 2 3
i 2 6
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr
i
703010
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
90/120
Session 4Ver. 1.0
n 2 3
i 2 7
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr
i
703010
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
91/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 703010
i
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
92/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 703010
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
93/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 703010
i'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
94/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0 20arr 703010
i'
&0
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
95/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 703010
i''
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
-
8/9/2019 03 DS and Algorithm Session 04
96/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 703010
i'
70
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
l i i l i h
-
8/9/2019 03 DS and Algorithm Session 04
97/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 3010
i'
70
'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes less
than or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
I l i I i S Al i h
-
8/9/2019 03 DS and Algorithm Session 04
98/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 3010
i
70
'
30
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes lessthan or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
I l ti I ti S t Al ith
-
8/9/2019 03 DS and Algorithm Session 04
99/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 10
i
70
'
30
'
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes lessthan or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
Data Structures and Algorithms
I l ti I ti S t Al ith
-
8/9/2019 03 DS and Algorithm Session 04
100/120
Session 4Ver. 1.0
n 2 3i 2 7
temp 2 5
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 10
i
703020
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes lessthan or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
'
Data Structures and Algorithms
I l ti I ti S t Al ith
-
8/9/2019 03 DS and Algorithm Session 04
101/120
Session 4Ver. 1.0
n 2 3i 2 7
Imlementing Insertion Sort Algorithm(Contd.)
210 43
&0arr 10
i
703020
"he list is now sorted
1. 8epeat steps 5, 6, 7, and 3varying i from 1 to n 9 1
5. Set temp 2 arrAiB
6. Set 0 2 i 9 1
7. 8epeat until 0 becomes less
than or arrA0B becomes lessthan or e#ual to temp:
a. Shift the value at inde-
0 to inde- 0 1
b. Eecrement 0 by 1
3. Store temp at inde- 0 1
'
Data Structures and Algorithms
D t i i th !" i # I ti S t Al ith
-
8/9/2019 03 DS and Algorithm Session 04
102/120
Session 4Ver. 1.0
"o sort a list of siGe n by using insertion sort, you need toperform
-
8/9/2019 03 DS and Algorithm Session 04
103/120
Session 4Ver. 1.0
% sales manager has to do a research on best seller colddrinks in the market for the year 57&5H. Eavid, the
software developer, has a list of all the cold drink brands
along with their sales figures stored in a file. Eavid has to
provide the sorted data to the sales manager. "he data in
the file is more or less sorted. /hich sorting algorithm willbe most efficient for sorting this data and why$
$ust a minute
%nswer:
Insertion sort provides better efficiency than bubble sort andselection sort when the list is partially sorted. "herefore, Eavid
should use the insertion sort algorithm.
Data Structures and Algorithms
Sorting Data by Using Shell Sort
-
8/9/2019 03 DS and Algorithm Session 04
104/120
Session 4Ver. 1.0
Shell sort algorithm:Insertion sort is an efficient algorithm only if the list is already
partially sorted and results in an inefficient solution in an
average case.
"o overcome this limitation, a computer scientist, E.. Shell
proposed an improvement over the insertion sort algorithm."he new algorithm was called shell sort after the name of its
proposer.
Sorting Data by Using Shell Sort
Data Structures and Algorithms
Imlementing Shell Sort Algorithm
-
8/9/2019 03 DS and Algorithm Session 04
105/120
Session 4Ver. 1.0
Shell sort algorithm:Improves insertion sort by comparing the elements separated
by a distance of several positions to form multiple sublists
%pplies insertion sort on each sublist to move the elements
towards their correct positions
!elps an element to take a bigger step towards its correctposition, thereby reducing the number of comparisons
Imlementing Shell Sort Algorithm
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
106/120
Session 4Ver. 1.0
"o understand the implementation of shell sort algorithm,consider an unsorted list of numbers stored in an array.
Imlementing Shell Sort Algorithm (Contd.)
210 43
70 104030 &0arr 20
5 6 7 & ( 10
(0 110 75 60 45
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
107/120
Session 4Ver. 1.0
"o apply shell sort on this array, you need to:Select the distance by which the elements in a group will be
separated to form multiple sublists.
%pply insertion sort on each sublist to move the elements
towards their correct positions.
Imlementing Shell Sort Algorithm (Contd.)
210 43
70 104030 &0arr 20
5 6 7 & ( 10
(0 110 75 60 45
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
108/120
Session 4Ver. 1.0
ist 1 2
Imlementing Shell Sort Algorithm (Contd.)
210 43
arr
5 6 7 & ( 10
Increment 2 6
ass 2 1
630 (
70 60(010
741 10
30 45110&0ist 5 2
&52
40 7520ist 6 2
70 104030 &0 20 (0 110 75 60 45
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
109/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
630 (
70 60(010
741 10
30 45110&0ist 5 2
&52
40 7520ist 6 2
%pply insertion sort to sort the
three lists"he lists are sorted
10 (07060 30 110&045
20 7540
ist 1 2
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
110/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
210 43
10 602030 45arr 40
5 6 7 & ( 10
70 &0 75 (0 110
630 ( 741 10
30ist 5 2
&52
75ist 6 2
30
75
10 60 70 (0 45 &0 110
20 40
ist 1 2
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
111/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
210 43
10 602030 45arr 40
5 6 7 & ( 10
70 &0 75 (0 110
Increment 2 5ass 2 5ist 1 2
420 6
10 704520
531 7
30 &04060ist 5 2
75 110
& 10
(0
(
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
112/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
ist 1 2
420 6
10 704520
531 7
30 &04060ist 5 2
75 110
& 10
(0
(
%pply insertion sort on each sublist
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd )
-
8/9/2019 03 DS and Algorithm Session 04
113/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
ist 1 2
420 6
10 704520
531 7
30 &06040ist 5 2
75 110
& 10
(0
(
"he lists are now sorted
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
114/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
ist 1 2
420 6
10 704520
531 7
30 &06040ist 5 2
75 110
& 10
(0
(
210 43
10 402030 45arr 60
5 6 7 & ( 10
70 &0 75 (0 110
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
115/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
210 43
10 402030 45arr 60
5 6 7 & ( 10
70 &0 75 (0 110
Increment 2 1ass 2 6
%pply insertion sort to sort the list
Data Structures and Algorithms
Imlementing Shell Sort Algorithm (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
116/120
Session 4Ver. 1.0
Imlementing Shell Sort Algorithm (Contd.)
210 43
10 403020 45arr 60
5 6 7 & ( 10
70 75 &0 (0 110
Increment 2 1ass 2 6
"he list is now sorted
Data Structures and Algorithms
$ust a minute
-
8/9/2019 03 DS and Algorithm Session 04
117/120
Session 4Ver. 1.0
/hich of the following sorting algorithms compares theelements separated by a distance of several positions to
sort the data$ "he options are:
1. Insertion sort
5. Selection sort
6. )ubble sort7. Shell sort
$ust a minute
%nswer:
7. Shell sort
Data Structures and Algorithms
Summary
-
8/9/2019 03 DS and Algorithm Session 04
118/120
Session 4Ver. 1.0
In this session, you learned that:Sorting is a process of arranging data in some pre&defined
order of a key value. "he order can be either ascending or
descending.
"here are various sorting algorithms that are used to sort data.
Some of them are:
)ubble sort
Selection sort
Insertion sort
Shell sort
*erge sort
+uick sort
!eap sort
Summary
Data Structures and Algorithms
Summary (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
119/120
Session 4Ver. 1.0
"o select an appropriate algorithm, you need to consider thefollowing:
-ecution time
Storage space
rogramming effort
)ubble sort and selection algorithms have a #uadratic order of
growth, and are therefore suitable for sorting small lists only.
Insertion sort performs different number of comparisons
depending on the initial ordering of elements. /hen the
elements are already in the sorted order, insertion sort needs
to make very few comparisons.
Insertion sort is an efficient algorithm than bubble sort andselection sort if the list that needs to be sorted is nearly sorted.
Summary (Contd.)
Data Structures and Algorithms
Summary (Contd.)
-
8/9/2019 03 DS and Algorithm Session 04
120/120
Shell sort improves insertion sort by comparing the elementsseparated by a distance of several positions. "his helps an
element to take a bigger step towards its correct position,
thereby reducing the number of comparisons.
y ( )