Professional Documents
Culture Documents
Data Structure Lec44 Handout
Data Structure Lec44 Handout
Lecture No. 44
___________________________________________________________________
Data Structures
Lecture No. 44
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 7
7.1, 7.2
Summary
Selection Sort
o Selection Sort analysis
Insertion Sort
o Insertion Sort Analysis
Bubble Sort
o Bubble Sort analysis
Summary
N log2 (N) Algorithms
This is the sequel of the previous lecture in which we talked about the sort algorithms.
There are three elementary sorting methods being discussed in this hand out. These
are- selection sort, insertion sort and bubble sort. To begin with, we will talk about the
selection sort algorithm.
Selection Sort
Suppose we have an array with different numbers. For sorting it in an ascending order,
selection sorting will be applied on it in the following manner. We find the smallest
number in the array and bring it to the position 1. We do the same process with the
remaining part of the array and bring the smallest number among the remaining array
to the next position. This process continues till the time all the positions of the array
are filled with the elements. Thus the main idea of selection sort is that
find the smallest element
put it in the first position
find the next smallest element in the remaining elements
put it in the second position
And so on, until we get to the end of the array
Lets understand this algorithm by considering an example with the help of figures.
Consider an array that has four numbers i.e. 19, 5, 7 and 12 respectively. Now we want
to sort this array in ascending order. To sort the array, selection algorithm will be
applied on it.
Page 1 of 10
a
:
19
12
a
:
19
12
a
:
19
12
a
:
12
19
a
:
12
19
Page 2 of 10
Page 3 of 10
20
10
20
10
20
10
10
20
10
20
Insertion Sort
The main idea of insertion sort is
Start by considering the first two elements of the array data. If found
out of order, swap them
Consider the third element; insert it into the proper position among the
first three elements.
Consider the fourth element; insert it into the proper position among
the first four elements.
This algorithm is not something uncommon to the persons who know card playing. In
the game of cards, a player gets 13 cards. He keeps them in the sorted order in his
hand for his ease. A player looks at the first two cards, sorts them and keeps the
smaller card first and then the second. Suppose that two cards were 9 and 8, the player
swap them and keep 8 before 9. Now he takes the third card. Suppose, it is 10, then it
is in its position. If this card is of number 2, the player will pick it up and put it on the
start of the cards. Then he looks at the fourth card and inserts it in the first three cards
(that he has sorted) at a proper place. He repeats the same process with all the cards
and finally gets the cards in a sorted order. Thus in this algorithm, we keep the left part
of the array sorted and take element from the right and insert it in the left part at its
proper place. Due to this process of insertion, it is called insertion sorting.
Lets consider the array that we have used in the selection sort and sort it now with the
insertion sorting. The following figure shows the insertion sort of the array pictorially.
a
:
19
12
a
:
12
19
a
:
12
19
a
:
12
19
Page 6 of 10
Bubble Sort
The third sorting algorithm is bubble sort. The basic idea of this algorithm is that we
bring the smaller elements upward in the array step by step and as a result, the larger
elements go downward. If we think about array as a vertical one, we do bubble sort.
The smaller elements come upward and the larger elements go downward in the array.
Thus it seems a bubbling phenomenon. Due to this bubbling nature, this is called the
bubble sort. Thus the basic idea is that the lighter bubbles (smaller numbers) rise to the
top. This is for the sorting in ascending order. We can do this in the reverse order for
the descending order.
The steps in the bubble sort can be described as below
Exchange neighboring items until the largest item reaches the end of the
array
Repeat the above step for the rest of the array
In this sort algorithm, we do not search the array for the smallest number like in the
other two algorithms. Also we do not insert the element by shifting the other elements.
In this algorithm, we do pair-wise swapping. We will take first the elements and swap
the smaller with the larger number. Then we do the swap between the next pair. By
repeating this process, the larger number will be going to the end of the array and
smaller elements come to the start of the array.
Lets try to understand this phenomenon with the help of figures how bubble sort
works. Consider the same previous array that has elements 19, 12, 5 and 7.
Page 7 of 10
a:
a:
19
12
19
12
12
19
12
19
5
0
a:
a:
a:
a:
a:
a:
a:
a:
5
12
19
12
19
12
19
12
19
12
19
12
19
First of all, we compare the first pair i.e. 19 and 5. As 5 is less than 19, we swap these
elements. Now 5 is at its place and we take the next pair. This pair is 19, 12 and not
12, 7. In this pair 12 is less than 19, we swap 12 and 19. After this, the next pair is 19,
7. Here 7 is less than 19 so we swap it. Now 7 is at its place as compared to 19 but it
is not at its final position. The element 19 is at its final position. Now we repeat the
pair wise swapping on the array from index 0 to 2 as the value at index 3 is at its
position. So we compare 5 and 12. As 5 is less than 12 so it is at its place (that is
before 12) and we need not to swap them. Now we take the next pair that is 12 and 7.
In this pair, 7 is less than 12 so we swap these elements. Now 7 is at its position with
respect to the pair 12 and 7. Thus we have sorted the array up to index 2 as 12 is now
at its final position. The element 19 is already at its final position. Note that here in the
bubble sort, we are not using additional storage (array). Rather, we are replacing the
elements in the same array. Thus bubble sort is also an in place algorithm. Now as
index 2 and 3 have their final values, we do the swap process up to the index 1. Here,
the first pair is 5 and 7 and in this pair, we need no swapping as 5 is less than 7 and is
at its position (i.e. before 7). Thus 7 is also at its final position and the array is sorted.
Following is the code of bubble sort algorithm in C++.
void bubbleSort(int *arr, int N)
{
int i, temp, bound = N-1;
int swapped = 1;
while (swapped > 0 )
{
swapped = 0;
for(i=0; i < bound; i++)
if ( arr[i] > arr[i+1] )
{
Page 8 of 10
Summary
Now considering the above three algorithms, we see that these algorithms are easy to
understand. Coding for these algorithms is also easy. These three algorithms are in
place algorithms. There is no need of extra storage for sorting an array by these
algorithms. With respect to the time complexity, these algorithms are proportional to
N2. Here N is the number of elements. So we can see that as the value of N increases,
the performance time of these algorithms increases considerably as it is proportional to
N2. Thus these algorithms are expensive with respect to time performance. There are
algorithms that have the time complexity proportional to N log 2 (N). The following
table shows the respective values of N2 and N log2(N) for some values of N.
Page 9 of 10
N2
N Log2 (N)
33.21
664.38
9965.78
132877.12
1660964.04
19931568.57
10
100
100
10000
1000
1000000
10000
100000000
100000 10000000000
1000000
1E+12
From this table we can see that for a particular value of N, the value of N2 is very large
as compared to the value of N log2 (N). Thus we see that the algorithms whose time
complexity is proportional to N2 are much time consuming as compared to the
algorithms the time complexity of which is proportional to N log 2 (N). Thus we see
that the N log2 (N) algorithms are better than the N2 algorithms.
12
11
Now we split this array into two parts shown in the following figure.
10
12
11
Now we have two parts of the array. We sort these parts separately. Suppose we sort
these parts with an elementary sort algorithm. These parts may be sorted in the
following manner.
4
10
12
11
After this we merge these two parts and get the sorted array as shown below.
2
10
11
12
Page 10 of 10