Professional Documents
Culture Documents
Sorting Techniques
Insertion Sort: Here the elements are inserted based on insertion. In this sorting we can
read the elements from 0 to n-1, inserting each element into its proper position.
The insertion sort algorithm scans A from A [N-1], inserting each element A[K] into its
proper position in the previously sorted sub array A[0], A[1], A[2],........ A[K-1]. i.e.,
Algorithm:
Insertionsort (list, N)
list: list is array of elements
N: N is number of elements in the list
begin:
for i=1 to N-1 do
newelement =list[i]
location =i-1
while (location>=0) and (list[location]>newelement ) do
list[location+1]=list[location];
location=location-1;
end of while
list[location+1]=newelement
end of for
end of insertion sort
Program:
Bubble sort: Bubble sort belongs to the family of sorting by exchange, where during
the sorting process pairs of elements that are out of order are interchanged until the whole list
is ordered.
In this sorting, multiple swapping takes place in single iteration. In this method, the adjacent
list of elements to be sorted.
Index: A0 A1 A2 A3 A4 A5 A6 A7
Array: 32 51 27 85 66 23 13 57
Pass 1: a) compare A0 and A1, since 32<51, the list is not altered
b) Compare A1 and A2, since 51>27, swap 51 and 27 then
Array: 32 27 51 85 66 23 13 57
c) Compare A2 and A3, since 51<85, the list is not altered
d) Compare A3 and A4, since 85>66, swap 85 and 66 then
Array: 32 27 51 66 85 23 13 57
e) Compare A4 and A5, since 85>23, swap 85 and 23 then
Array: 32 27 51 66 23 85 13 27
f) Compare A5 and A6, since 85>13, swap 85 and 13 then
Array: 32 27 51 66 23 13 85 27
g) Compare A5 and A6, since 85>27, swap 85 and 27 then
Array: 32 27 51 66 23 13 27 85
At the end of the first pass, the largest number 85 has moved to the last position. However,
the rest of the numbers are not sorted, even though some of them have changed their
positions. Now we have to do the above approach for the remaining elements.
Pass 2: At the end of pass 2, the 2nd largest element, 66 has moved to the next_to_last
position
Array: 27 33 51 23 13 57 [66, 85]
At the end of 3rd pass, the third largest element 57 moved to its position in the list
Pass 3: 27 33 23 13 51 [57 66 85]
Pass 4: 27 23 13 33 [51 57 66 85]
Pass 5: 23 13 27 [33 51 57 66 85]
Pass 6: 13 23 [27 33 51 57 66 85]
Pass 7: 13 [23 27 33 51 55 66 85]
From the example: pass 1 has taken 7 comparisons for 8 elements and pass 2 will take 6
comparisons and so on.....
Algorithm:
Bubblesort (list, N)
list: list of array elements
N: Number of elements in the list
begin:
Swapelements=1;
while Swapelements=1
N=N-1;
Swapelements=0;
for i=0 to (N)
If list[i]>list[i+1] then
Swap list[i] and list[i+1]
Swapelements=1;
end of if
end of for
end of while
end of bubble sort
Quick sort: Quick sort belong to the family of sorting by exchange where elements that
are out of order are exchanged amongst themselves to obtain the sorted list
The procedure works on the principle of partitioning the unordered list into two sub lists at
every stage of the sorting process based on what is called a pivot element. The two sub lists
occur to the left and right of the pivot element. The pivot element determines its appropriate
position in the sorted list and is therefore free from its participation in the subsequent stages
of the sorting process. Again each of the sub lists are partitioned against their respective pivot
elements until no more partitioning can be called. At this stage quick sort is done.
The procedure for quick sort is explained by using the following example
Let us choose first element as pivot element. The following example explains the portioning
the list. Here left index position moves from left to right looking for the first element that is
greater than pivot element i.e. 34 and after that the right index position moves from right to
left looking for the first element less than or equal to pivot element i.e. 34.
0 1 2 3 4 5 6 7 8
L: 34 26 01 45 18 78 12 89 27
Pivot: 0, pivot element is 34
left index stops at 45, right index stops at 27
0 1 2 3 4 5 6 7 8
45, 27 spotted: 34 26 01 45 18 78 12 89 27
Now, compare the left (3) and right (8) index positions of the spotted elements. If left<right
condition is true then swap left and right position elements.
Now, proceed from the points where the moves were last stopped, left index position stops at
78 element during its left to right move and right index position stops at 12 during its right to
left move.
As before the left < right (5<6) condition is satisfied, resulting in an exchange of 78 and 12
0 1 2 3 4 5 6 7 8
L: 34 26 01 27 18 78 12 89 45
Pivot: 0, pivot element is 34
left index stops at 78, right index stops at 12
0 1 2 3 4 5 6 7 8
78, 12 spotted: 34 26 01 27 18 12 78 89 45
In the next lap of the move, elements 78 and 12 are spotted again but this time the condition
left<right (6<5) is failed.
0 1 2 3 4 5 6 7 8
L: 34 26 01 27 18 12 78 89 45
Pivot: 0, pivot element is 34
left index stops at 78, right index stops at 12
0 1 2 3 4 5 6 7 8
12, 78 spotted: 34 26 01 27 18 12 78 89 45
If the condition left>right is satisfied, then swap the pivot element with the right element and
partition the array of elements into two sublists i.e., left part of pivot and right part of pivot.
Apply the same procedure for 2 parts until all elements are sorted.
This implies that the position index of 78 is greater than position index of 12 calling for a
partition. Now 34 exchanges position with 12 and the list is partitioned into two
In the above example all the elements less than or equal to 34 are on left part of pivot and
greater elements are on right part of pivot. Also 34, pivot element has settled down at
appropriate place.
Now, repeat the same process for the left part of the pivot and the right part of the pivot. At
last the elements will be in sorted order. Quick sort is also called as Stable Sort.
Algorithm:
Program:
right=last+1;
pivot=first;
while(left<right){
do
{
left=left+1;
} while(L[left]<=L[pivot]&&left<=last);
do
{
right=right-1;
} while(L[right]>L[pivot]&&right>=first);
if(left<right){
temp=L[left];
L[left]=L[right];
L[right]=temp;}
}
loc=right;
temp=L[first];
L[first]=L[right];
L[right]=temp;
return loc;
}
Merge Sort: Merging is a process by which two ordered lists of elements are combined
(or) merged into a single ordered list. Merge Sort makes use of the principle of merge to sort
an unordered list of elements and hence the name.
Consider an example that A & B are 2 sorted lists of elements. Let A has r elements and B
has s elements. The Merge sort combines the elements of A and B into a single sorted list C
with n= r + s elements. This is called Merging. After combining the two lists, the elements
are sorted by using the Merging Algorithm. At each step, the front element of both the lists is
compared and the smaller one is placed in the combined list.
The recursive merge sort procedure is built on the design principle of Divide and conquers.
Here, the original unordered list of elements is recursively divided roughly into two sublists
until the sublists are small enough where a merge operation is done before they are combined
to yield the final sorted list.
The following example explains the working of Merge Sort procedure for 2 sorted arrays.
Sorted1 and Sorted2 array elements are compared and the smallest element is moved to the
final array. This procedure is repeated until one array is empty among those two arrays. Then
the array with the remaining elements is moved to the final array.
Radix sort: Radix sort belongs to the family of sorting by distribution where keys are
repeatedly distributed into groups (or) classes based on the digits or the characters forming
the key until the entire list at the end of a distribution phase gets sorted. Radix sort is also
known as bin sort (or) bucket sort (or) digital sort
Radix sort method: - Given a list of L of n number of keys, radix sort undertakes sorting by
distributing the keys based on the digits forming the key. If the distribution proceeds from
least significant digit (LSD) onwards and progresses left digit after digit, then it is termed as
LSD first sort.
Let us consider the case of LSD first sort for the list of n keys. Such that the radix sort works
for number of digits present in the largest number of the list. Here r is radix of the key, where
r depends upon the number type and hence the name radix sort.
In order to understand the distribution passes of the LSD first sort procedure, assume that r
bins corresponding to the radix of the keys are present
In the 1st pass of the sort, all the keys of the List L are stored in their respective bins based on
the value of their last digit.
At the end of 1st pass, the keys are collected in order of the bins.
At this stage the elements (keys) are sorted based on their LSD.
In the second pass of the sort, follow the similar distribution of the keys for sorting them in
respective bins based on their next digit.
At this stage the elements are sorted based on their last two digits.
The distribution continues for d passes, where d is number of digits for the largest element in
the list, at the end of which the entire list L is obtained sorted.
Algorithm:-
radixsort (array, num)
array: Array of elements
num: Number of elements in array
begin
m=0,exp=1,bucket[10],b[];
for i=0 to num-1
if a[i]>m
m=a[i];/*m will be largest element of array and the next while loop will run for
number of digits in that element*/
end of if;
end of for;
while m/exp>0 do
bucket[10]={0}; /*initialising the bucket array to zero*/
for i=0 to i=n-1
bucket[a[i]/exp%10]++;/*sorting the element into bucket array as per their digit position*/
for i=1 to 9
bucket[i]+=bucket[i-1];/*this loop will store the number of elements into bucket*/
for i=n-1 to 0
b[--bucket[a[i]/exp%10]]=a[i];
for i=0 to n-1
a[i]=b[i];/*this loop will copy the array elements into temporary as per the index position*/
exp*=10; /*copying the elements from temporary array*/
end of while /*moving to array*/
end of radixsort/*the loop to next digit*/
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
int a[MAX],i,n;
clrscr();
printf(enter number of elements\n);
scanf(%d,&n);
printf(enter %d elements \n,n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
radixsort(a,n);
printf(sorted elements are \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
getch();
}
void radixsort(int a[],int n)
{
int b[MAX],m=0,exp=1,bucket[10];
for(i=0;i<n;i++)
if(a[i]>m)
m=a[i];
while(m/exp>0)
{
for(i=0;i<10;i++)
bucket[i]=0;
for(i=0;i<n;i++)
bucket[a[i]/exp%10]++;
for(i=1;i<10;i++)
bucket[i]+=bucket[i-1];
for(i=n-1;i>=0;i--)
b[--bucket[a[i]/exp%10]]=a[i];
for(i=0;i<n;i++)
a[i]=b[i];
exp*=10;
}
}
Heap sort: - Heap sort is a sorting procedure belonging to the family of sorting by
selection. At every pass of the sort, the smallest or the largest key is selected by a well
derived method and added to the output list and when all the elements have been selected the
output list is sorted.
Heap:- A heap is a complete binary tree in which each parent node u labelled by a key or
element p(u) and its respective child nodes v, w labelled p(v) and p(w) respectively are such
that p(u)>p(w) and p(u)>p(v). Since the parent node keys are greater than their respective
child node keys at each level, the key at the root node is largest among all the keys
represented as a heap.
k0 is inserted into the initially empty heap as its root. k 1 is inserted as the left child of k0. If the
property is violated then k0 and k1 swap positions to construct a heap out of them. Next k 2 is
inserted as the right child of node k 0. If k2 violates the property of heap it swaps position with
its parent k0.
In general, a key ki is inserted into the heap as the child of node (i-1)/2 following the
principles of complete binary tree (the parent of child is given by (i-1)/2 and the right and left
child of i is given by (2i+2) and (2i+1) respectively). If the property of heap is violated then it
calls for a swap between ki and k(i-1)/2 which in turn may cause further adjustments between k (i-
1)/2 and its parent and so on. 0 1 2 3 4 5 6 7
Consider an example, List L = {D, B, G, E, A, H, C, F}
Insert Element before Heap Construct after Heap Construct
D D
D =>
D D
B =>
B
B
D
G Here Root Node D is small G
Than G, So swap them =>
B B
G D
E G => G
B E
D D
E B
G
A G
E
D
E
D
B A
=>
B A
H
H Here D and H are G
Swapped because E
E G
D
D is small than H and =>
Also G is small than H. B A D
B A H
H
H
E
C G E
G
=>
B A D C B A D C
H H
E F
G G
F B A
=>
D C E A D C
F B
2. Reconstruction of heap: - After the construction of heap to the unordered list of elements
L = {k0, k1, k2, k3.....kn-1}. The root which holds the largest element of L swaps places with the
largest numbered node of the tree. The largest node is now disabled from further participation
in the heap reconstruction process.
Now the remaining tree with (n-1) elements is again reconstructed to form a heap. The root
node now holds the next largest element of the list. The swapping of the root node with the
next largest numbered node in the tree which is disabled which yields a tree with (n-2)
elements and the procedure is followed again until the tree is left with single element.
H (0)
F (1)
G (2)
B (7)
Here root node B is small than its child nodes. In F and G, G is greater, so swap B and G
elements. After swapping the node 3 (B) is less compared to node 6 (D) and 7 (c). So, we
need to swap again node 6 (B) and node 3 (D). After swapping the binary heap is as follows:
G (0)
F (1)
D (2)
F (0)
G C (0) =>
E (1) D (2)
F (1)
D (2)
DS (2) NEC, CSE Dept. Page 17
A (4)
UNIT II Sorting Techniques
X
C (3) A (4) B (5) G (6)
E (3) B (5) G (6)
(4)
B (0) E (0)
F =>
E(1) C (1)
D (2) D (2)
A (0)
E => D (0)
C (1) D (2) C (1)
A (2)
B (3) E (4)
B (3) E (4) X
B (0
D => C (0)
C (1)
A (2) B (1)
A (2)
X
D (3) D (3)
A (0)
C => B (0)
A (0) A (0)
B =>
B (1) B (1) X
At this stage, the heap sort is done and the output list contains the elements in the sorted
order, A B C D E F G H. The heap sort is implemented using arrays.
Algorithm:-
makeheap (list, n)
list: list of array elements
n: number of array elements
begin
variables: i, c, p ,val;
for i=1 to n-1
val=list[i];
c=i;
p=(c-1)/2;
while c>0 and val>list[p]
list[c]=list[p];
c=p;
p=(c-1)/2;
end of while;
list[c]=val;
end of for
end of makeheap
heapsort (list,n)
list: list of array elements
n: number of array elements
begin
variables i, ivalue, c,p;
for i=n-1 to 1
ivalue=list[i];
list[i]=list[0];
p=0;
if(i==1)
c=-1;
else
c=1;
end of if
if(i>2 and list[2]>list[1])
c=2;
while c>=0 and ivalue<list[s] do
list[p]=list[c];
p=c;
c=2*p+1;
if(c+1<=i-1 and list[c]< list[c+1])
c++;
end of if
if(c>i-1)
c=-1;
end of if
end of while
list[p]=ivalue;
end of for
end of heapsort
Program:-