You are on page 1of 20

UNIT II Sorting Techniques

Sorting Techniques

Sorting: - process by which a collection of items placed into order.


Operation of arranging data
Order typically based on a data field called a key

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.,

Pass 1: A [0] is sorted by itself


Pass 2: A[1] is inserted either before or after A[0], so that A[0], A[1] is sorted.
Pass 3: A[2] is inserted into proper space in A[0], A[1], i.e., before A[0], between A[0]
and A[1], or after space A[1],so that: A[0],A[1],A[2] are sorted
Pass N:A[N-1] is inserted into its proper place in A[0],A[1],A[2],.....A[N-1]. So that A
[0], A [1] ... A [N-1] are sorted.

The above example explains the working of Insertion Sort.

DS NEC, CSE Dept. Page 1


UNIT II Sorting Techniques

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

Algorithm worst case Average case

Insertion sort O (n2) O (n2)

Program:

void insertionsort (int list[],int n)


{
int i, ne, loc;
for(i=1;i<=n-1;i++)
{
ne=list[i];
loc=i-1;
while((loc>=0)&&(list[loc]>ne))
{
list[loc+1]=list[loc];
loc=loc-1;
}
list[loc+1]=ne;
}
}

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.

DS NEC, CSE Dept. Page 2


UNIT II Sorting Techniques

The following example will describe the working of bubble sort

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.....

DS NEC, CSE Dept. Page 3


UNIT II Sorting Techniques

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

Algorithm worst case Average case

Bubble sort O (n2) O(n2)

Program for bubble sort:

void Bubblesort (int list [ ], int n)


{
int swapele, temp;
swapele = 1;
while (swapele==1)
{
n=n-1;
swapele =0;
for (i=0; i<n; i++)
{
if (list [i] > list [i+1])
{
Swapele=1;
temp = list [i];
list[i] = list [i+1];
list [i+1] = temp;
}
}
}
}

DS NEC, CSE Dept. Page 4


UNIT II Sorting Techniques

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

L={34,26,1,45,18,78,12,89,27} be an ordered list of elements

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.

Since the condition is true they are exchanged.


0 1 2 3 4 5 6 7 8
Exchange 45, 27:34 26 01 27 18 78 12 89 45

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.

DS NEC, CSE Dept. Page 5


UNIT II Sorting Techniques

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

Call for partition [12 26 1 27 18] or [78 89 45]


Exchange 12 and pivot element 34

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.

DS NEC, CSE Dept. Page 6


UNIT II Sorting Techniques

Algorithm:

Quicksort (L, first, last)


L: list of unordered elements
first: starting point of list
last: ending point of list
begin:
if (first<last) then
loc=partition (L, first, last)
Quicksort (L, first, loc-1);
Quicksort (L, loc+1, last);
end of if
end of quicksort

Partition (L, first, last)


L: list to be partitioned
first: starting point of list
last: ending point of list
begin:
left=first;
right=last+1;
pivot=first;
while (left<=right)
repeat
left=left+1;
until L[left] <= L[pivot] and left<=last;
repeat
Right=right-1;
until L[right] > L[pivot] and right>=first;
if (left<right)
Swap (L[left], L[right]);
end of while
loc=right;
swap (L[first], L[right]);
end of partition.

Program:

void quicksort (int list [], int first, int last)


{
int loc;
if (first<last){
loc=partition (list, first, last)
quicksort (list, first, loc-1);
quicksort (list, loc+1, last);}
}
int partition (int L[], int first, int last)
{
int left, right, pivot, temp;
left=first;

DS NEC, CSE Dept. Page 7


UNIT II Sorting Techniques

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.

Recursive Merge Sort Procedure:

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.

DS NEC, CSE Dept. Page 8


UNIT II Sorting Techniques

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.

Algorithm for Merge Sort:

merge (a, first, last)


a: list of array elements in a.
first: Starting index of a
last: final index of a
begin
if (first < last) then
mid = (first + last)/2;
merge (a, first, mid);
merge (a, first, last);
mergesort (a, first, mid, mid+1, last);
end of if;
end of merge.

DS NEC, CSE Dept. Page 9


UNIT II Sorting Techniques

Algorithm for Merge:


mergesort (a, start1, end1, start2, end2)
a: list of array elements
start 1 : starting index of array sub-list1
end 1: ending index of array sub-list1
start 2: starting index of array sub-list2
end2: ending index of array sub-list2
begin
i, final start, final end, index=0, C [ ];
finalstart = start; finalend = end 2;
while ((start1<end1) and (start2<=end2)) do
if (a[start1]<a[start2])then
c[index] = a[start1];
index = index+1;
start1 = start1+1;
else
C[index]=a[start2];
Index = index+1;
start2 = start2+1;
end of if;
end of while;
while (start1<=end1) do
c[index] = a[start1];
start1 = start1+1;
index = index+1;
end of while;
while (start2<=end2) do
c[index]=a[start2];
start2=start2+1;
index=index+1;
end of while;
index=0;
for i=finalstart to finalend
a[i]=c[index];
index=index+1;
end of for
end of mergesort
Program:-
void merge(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge(a,first,mid);
merge(a,mid+1,last);
mergesort(a,first,mid,mid+1,last);
}
}

DS NEC, CSE Dept. Page 10


UNIT II Sorting Techniques

void mergesort(int a[],int start1,int end1,int start2,int end2)


{
int i,index=0,finalstart,finalend,c[20];
finalstart=start1;
finalend=end2;
while((start1<=end1)&&(start2<=end2))
{
if(a[start1]<a[start2])
{
c[index]=a[start1];
start1=start1+1;
index=index+1;
}
else
{
c[index]=a[start2];
start2=start2+1;
index=index+1;
}
}
while(start1<=end1)
{
c[index]=a[start1];
start1=start1+1;
index=index+1;
}
while(start2<=end2)
{
c[index]=a[start2];
start2=start2+1;
index=index+1;
}
index=0;
for(i=finalstart;i<=finalend;i++)
{
a[i]=c[index];
index=index+1;
}
}

Algorithm worst case average case

Quicksort O (n^2) O (nlogn)

Merge sort O (nlogn) O (nlogn)

DS NEC, CSE Dept. Page 11


UNIT II Sorting Techniques

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.

DS NEC, CSE Dept. Page 12


UNIT II Sorting Techniques

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*/

DS NEC, CSE Dept. Page 13


UNIT II Sorting Techniques

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.

DS NEC, CSE Dept. Page 14


UNIT II Sorting Techniques

Heap sort proceeds in two phases


1) Construction of a heap where the unordered list of elements to be sorted are converted
into a heap and
2) Repeated selection and inclusion of the root node key of the heap into the output list after
reconstructing the remaining tree into a heap.

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.

1. Construction of heap: - Given an unordered list of elements it is essential that a heap is 1 st


constructed before heap sort works on it to yield the sorted list. Let L = {k 0, k1, k2, k3.....kn-1}
be the unordered list. The construction of the heap proceeds by inserting keys from L one by
one into an existing 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

DS NEC, CSE Dept. Page 15


UNIT II Sorting Techniques

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

DS NEC, CSE Dept. Page 16


UNIT II Sorting Techniques

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)

E (3) ((3) A (4) D (5) C (6)


(4)

B (7)

Output before reconstruction after reconstruction of


of heap heap

H: Swap H with largest


Number node (8) and H (0) => B (0)
F (1) (2)
Largest number node F (1)
G (2)
Is discarded G (2) (2)
E (3) ((3)
E (3) ((3) A (4) D (5)
(4) C (6)
(4)
B (7) A (4) D (5) C (6) (7) H (7) X

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)

E (3) A (4) B (5) C (6)

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)

C (3) A (4) F (5) B (3) A (4) F (5) X

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)

B (1) C (2) A (1) C (2)


X

A (0) A (0)
B =>
B (1) B (1) X

A A (0) => A (0)

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:-

DS NEC, CSE Dept. Page 18


UNIT II Sorting Techniques

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

DS NEC, CSE Dept. Page 19


UNIT II Sorting Techniques

Program:-

void makeHeap(int a[],int n)


{
int i,val,c,p;
for(i=1;i<n;i++)
{
val=a[i]
c=i;
p=(c-1)/2;
while(c>0&&a[p]<val)
{
a[c]=a[p];
c=p;
p=(c-1)/2;
}
a[c]=val;
}
}
void heapsort(int a[], int n)
{
int i,lival,c,p;
for(i=n-1;i>0;i--)
{
lival=a[i];
a[i]=a[0];
p=0;
if(i==1)
c=-1;
else
c=1;
if(i>2 && arr[2]>arr[1])
c=2;
while(c>=0 &&lival<a[c])
{
a[p]=a[c];
p=c;
c=2*p+1;
if(c+1<i-1 && a[c]<a[c+1])
c++;
if(c>i-1)
c=-1;
}
a[p]=lival;
}}
Algorithm worst case average case

Radix sort O (n) O (n)

Heap sort O (nlogn) O (nlogn)

DS NEC, CSE Dept. Page 20

You might also like