Sorting
Sorting
If the elements are numbers, then they can be sorted in ascending or descending
order.
In Case the elements are characters or Strings then you can sort them in
alphabetical order.
Bubble Sort
Bubble sort is the simplest sorting algorithm that sorts an array of items in multiple
passes.
In the first pass, first and second item are compared and swapped(exchanged) if
they are in the wrong order. Then the second and third items are compared and
swapped if they are in the wrong order.
This process continues until the last two items of an array are compared and
swapped if they are in the wrong order.
In the first pass, n-1 comparisons occur. After the first pass, the first largest item is
placed at the first last position.
In the second pass, n-2 comparisons occur. After the second pass, the second
largest item is placed at the second last position.
After n-1 passes, entire array is sorted and hence no more items are left for
comparison.
Illustrates how the following numbers, get sorted in ascending order using
bubble sort:- {8,7,9,2}
First Pass
Second Pass
After 2nd pass second largest number 8 bubbles to second last position.
Third pass
This array is sorted after three passes.
Bubble Sort
Algorithm
BUBBLE_SORT(a,n)
if a[j]>a[j+1] then
[End of if structure]
#include<stdio.h>
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
int i;
int main()
{
int a[15],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nEnter value:");
scanf("%d",&a[i]);
BUBBLE_SORT(a,n);
DISPLAY(a,n);
return 0;
Selection Sort
If the array is to be arranged in ascending order, the smallest element need to be
selected and exchange/interchange with the first position element in the array.
Again, the smallest element from the remaining elements is selected and
exchange/interchange with the second array element. The process continues till all
elements are arranged.
If the array needs to be arranged in descending order, select the largest element.
Q. Illustrates how the following array, get sorted in ascending
Sol.
1st pass
2nd pass
3rd pass
4th pass
5th pass
Selection Sort
Algorithm
SELECTION_SORT(a,n)
set pos=i
if min>a[j] then
set min=a[j]
set pos=j
[End of if structure]
set temp=a[pos]
set a[pos]=a[i]
set a[i]=temp
[End of if structure]
#include <stdio.h>
int i,j,temp,min,pos;
for(i=0;i<n-1;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<n;j++)
if(a[j]<min)
min=a[j];
pos=j;
if(pos!=i)
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
int i;
int main()
int a[15],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nEnter value:");
scanf("%d",&a[i]);
SELECTION_SORT(a,n);
DISPLAY(a,n);
return 0;
Insertion Sort
Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part.
Insertion Sort
Algorithm
INSERTION_SORT(a,n)
set j = i-1
set j = j-1
#include <stdio.h>
{
int i, j,key;
for (i=1;i<n;i++)
key = a[i];
j = i - 1;
a[j + 1] = a[j];
j = j - 1;
a[j + 1] = key;
int i;
int main()
int a[15],n,i;
printf("How many values to sort?:");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nEnter value:");
scanf("%d",&a[i]);
INSERTION_SORT(a,n);
DISPLAY(a,n);
return 0;
Merging
Merging means combining the elements of two similar data structure into a third
data structure.
MERGING
Algorithm
MERGE(a,n1,b,n2,c)
#include<stdio.h>
int i,j,k;
i=j=k=0;
while(i<n1&&j<n2)
if(a[i]<=b[j])
c[k]=a[i];
i++;
}
else
c[k]=b[j];
j++;
k++;
while(i<n1)
c[k]=a[i];
i++;
k++;
while(j<n2)
c[k]=b[j];
j++;
k++;
return k;
}
void DISPLAY(int a[],int n)
int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
int main()
int a[10],b[10],c[20],n1,n2,n,i,j;
scanf("%d",&n1);
for(i=0;i<n1;i++)
printf("\nEnter value:");
scanf("%d",&a[i]);
scanf("%d",&n2);
for(j=0;j<n2;j++)
printf("\nEnter value:");
scanf("%d",&b[j]);
n=MERGE(a,n1,b,n2,c);
DISPLAY(c,n);
return 0;
Merge Sort
Merge sort is also based on divide and conquer approach. It divides the input array
into two sub-arrays and then recursively divide each of the two sub-arrays until we
have sub-arrays of length one. Finally, these sub-arrays are merged into one sorted
array.
In Merge sort the unsorted array is divided into n sub-arrays, each having one
element.
Merge Sort
Algorithm
Merge_Sort(a,beg,end)
call Merge_Sort(a,beg,mid)
call Merge_Sort(a,mid+1,end)
call Merge(a,beg,mid,end)
[End of if structure]
Step 2: return
Merge
Algorithm
Merge(a,beg,mid,end)
#include <stdio.h>
int i,j,k;
int n1=mid-beg+1,n2=end-mid;
int temp[n1+n2];
i=beg;
j=mid+1;
k=0;
while(i<=mid&&j<=end)
if(a[i]<=a[j])
temp[k]=a[i];
i++;
}
else
temp[k]=a[j];
j++;
k++;
while(i<=mid)
temp[k]=a[i];
i++;
k++;
while(j<=end)
temp[k]=a[j];
j++;
k++;
k=0;
for(i=beg;i<=end;i++)
a[i]=temp[k++];
{
int mid;
if(beg<end)
mid=(beg+end)/2;
MERGE_SORT(a,beg,mid);
MERGE_SORT(a,mid+1,end);
MERGE(a,beg,mid,end);
int i;
int main()
int a[15],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter value:");
scanf("%d",&a[i]);
MERGE_SORT(a,0,n-1);
DISPLAY(a,n);
return 0;
Quick Sort
Quick sort is based on divide and conquer approach. On the basis of a selected
element (pivot) from the list, it partitions the rest of the list into two parts—a sub-
list that contains elements less than the pivot and other sub-list containing
elements greater than the pivot. The pivot is inserted between the two sub-lists.
The algorithm is recursively applied to the sub-lists until the size of each sub-list
becomes 1, indicating that the whole list has become sorted.
Quick Sort
Algorithm
QUICK_SORT(a,beg,end)
call Quick_Sort(a,beg,mid-1)
call Quick_Sort(a,mid+1,end)
[End of if structure]
Step 2: return
PARTITION
Algorithm
PARTITION(a,beg,end)
set i=beg-1
If a[j]<=pivot then
set i=i+1
set temp=a[i]
set a[i]=a[j]
set a[j]=temp
[End of if structure]
set a[i+1]=a[end]
set a[end]=temp
#include <stdio.h>
int pivot,i,j,temp;
pivot=a[end];
i=beg-1;
for(j=beg;j<=end-1;j++)
if(a[j]<=pivot)
i++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=a[i+1];
a[i+1]=a[end];
a[end]=temp;
return i+1;
}
int mid;
if(beg<end)
mid=PARTITION(a,beg,end);
QUICK_SORT(a,beg,mid-1);
QUICK_SORT(a,mid+1,end);
int i;
int main()
int a[15],n,i;
for(i=0;i<n;i++)
printf("\nEnter value:");
scanf("%d",&a[i]);
QUICK_SORT(a,0,n-1);
DISPLAY(a,n);
return 0;