All Types Of Sorting in C Programming with Example

How To Write a C Program To MERGE SORT, HEAP SORT, QUICK SORT, INSERTION SORT SELECTION SORT and BUBBLE SORT Arrays in C Programming Language ?


Solution For C Program To MERGE SORT, HEAP SORT, QUICK SORT, INSERTION SORT SELECTION SORT and BUBBLE SORT Arrays:


MERGE SORT - Write a c program to merge two sorted arrays:
#include<stdio.h>
main()
{
      int a[10],m,n,b[10],p;
      static int i;
      clrscr();
      printf("enter the size of the first array\n");
      scanf("%d",&m);
      printf("enter the elements into thae array\n");
      for(i=0;i<m;i++)
 scanf("%d",&a[i]);
      printf("enter the size of the second array\n");
      scanf("%d",&n);
      printf("enter the elements into the array\n");
      for(i=0;i<n;i++)
  scanf("%d",&b[i]);
      merge_sort(a,b,m,n);
      getch();
}
merge_sort(int a[],int b[],int m,int n)
{
      int c[25],p;
      static int i,j,k;
      i=0;
      j=0;
      while(i<m && j<n)
      {
  if(a[i]<b[j])
  {
c[k]=a[i];
k++;
i++;
  }
  else if(a[i]>b[j])
  {
c[k]=b[j];
k++;
j++;
  }
  else
  {
c[k]=a[i];
i++;
k++;
  }
      }
      if(i<m)
      {
  for(p=i;p<m;p++)
  {
c[k]=a[i];
k++;
i++;
  }
      }
      else if(j<n)
      {
  for(p=j;p<n;p++)
  {
c[k]=b[j];
k++;
j++;
  }
      }
      printf("after sorting the elements are\n");
      for(i=0;i<k;i++)
  printf("%d ",c[i]);
}
Tags:  c program to merge two sorted arrays, c program to merge two sorted arrays into one sorted array, c program to merge two sorted arrays in ascending order, merge two sorted arrays, merge two sorted arrays in place, merge two sorted arrays algorithm, merge two sorted arrays without extra space, merge two sorted arrays java, merge two sorted arrays without duplicates.

HEAP SORT - C Program to Sort an Array based on Heap Sort Algorithm.

#include<stdio.h>
main()
{
     int a[25],n,i;
     system("clear");
     printf("enter the size of the array\n");
     scanf("%d",&n);
     printf("enter %d elements into the array\n",n);
     for(i=0;i<n;i++)
     {
scanf("%d",&a[i]);
heap(a,i,a[i]);
     }
     heapsort(a,n);
     printf("sorted elements are\n");
     for(i=0;i<n;i++)
     {
printf("%d ",a[i]);
     }
}
heap(int a[],int pos,int k)
{
     int f;
     f=(pos-1)/2;
     while(pos>0 && k>a[f])
     {
a[pos]=a[f];
pos=f;
f=(pos-1)/2;
     }
     a[pos]=k;
}
heapsort(int a[],int n)
{
     int k,key,i,j;
     for(i=n-1;i>0;i--)
     {
key=a[0];
a[0]=a[i];
a[i]=key;
for(j=0;j<i;j++)
{
    heap(a,j,a[j]);
}
     }
}
Tags: c program to HEAPSORT, write a c program for heap sort, heap sort recursive program in c, c program for heap sort using recursion.

QUICK SORT - Write a c program to quick sort an array.

#include<stdio.h>
int n;
main()
{
     int a[20],i,l,r;
     void qsort();
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     l=0;
     r=n-1;
     qsort(a,l,r);
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
void qsort(int b[],int left,int right)
{
     int i,j,k,p,temp,finished;
     if(right>left)
     {
 i=left;
 j=right;
 p=b[left];
 printf("\n P,the partioning element is: %d\n",p);
 finished=0;
 while(!finished)
 {
      do
      {
   ++i;
      }while((b[i]<=p)&&(i<=right));
      while((b[j]>=p)&&(j>left))
      {
   --j;
      }
      if(j<i)
   finished=1;
      else
      {
   printf("\nswapping %d and %d. \n",b[i],b[j]);
   temp=b[i];
   b[i]=b[j];
   b[j]=temp;
   for(k=0;k<n;k++)
printf("%5d",b[k]);
   printf("\n");
      }
 }
 printf("\n I is greater than J,");
 printf("so b[left] and b[j] are swapped.");
 printf("Swapping %d and %d.\n\n",b[left],b[j]);
 temp=b[left];
 b[left]=b[j];
 b[j]=temp;
 for(k=0;k<n;k++)
     printf("%5d",b[k]);
 printf("\n");
 printf("\nsub file one is : elements %d to %d.\n",left,j-1);
 qsort(b,left,j-1);
 printf("\nsub file two is : elements %d to %d.\n",i,right);
 qsort(b,i,right);
     }
     else
     {
 if(right!=left)
      printf("Right (%d) is less than Left(%d).\n",right,left);
 else
 {
      printf("Right (%d) is less than Left(%d).\n",right,left);
      printf("subfile has only one element.\n");
 }
     }
     return;
}
Tags: c program to quicksort an array, c program for quicksort using arrays, c program to sort an array using quicksort, quicksort array java, c program for quicksort using divide and conquer, c program for quicksort with explanation, c program for quicksort using recursion, c program for quicksort in data structure, c program for quicksort using divide and conquer method.

INSERTION SORT - Write a c program to INSERTION SORT an array.

#include<stdio.h>
main()
{
     int a[20],i,n;
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
scanf("%d",&a[i]);
     ins_sort(a,n);
     getch();
}
ins_sort(int a[],int n)
{
     int i,j,k;
     for(j=1;j<n;j++)
     {
 k=a[j];
 for(i=j-1;i>=0&&k<a[i];i--)
 {
      a[i+1]=a[i];
 }
 a[i+1]=k;
 printf("element %d inserted in appropriate location",k);
 for(i=0;i<n;i++)
      printf("%d  ",a[i]);
 printf("\n");
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
Tags:c program to INSERTION SORT an array, c program for insertion sort using array, insertion sort array java, c program for insertion sort with explanation, c program for insertion sort using functions, c program for insertion sort using recursion, c program for insertion sort algorithm, c program for insertion sort using linked list, c program for insertion sort in descending order.

SELECTION SORT - Write a c program to SELECTION SORT an array

#include<stdio.h>
main()
{
     int i,n,a[50];
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
scanf("%d",&a[i]);
     sel_sort(a,n);
     getch();
}
sel_sort(int a[],int n)
{
     int i,j,temp,pass,min;
     for(pass=0;pass<n-1;pass++)
     {
 min=pass;         /*assume that pass is the minimum index*/
 for(j=pass+1;j<n;j++)
 {
     if(a[min]>a[j])   /*update min*/
 min=j;
 }
 if(min!=pass)     /*swaping elements*/
 {
     temp=a[pass];
     a[pass]=a[min];
     a[min]=temp;
 }
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
Tags: c program to SELECTION SORT an array, c program for selection sort using arrays, write a c program to perform selection sort on an array of n elements, c program for selection sort using functions, c program for selection sort using linked list, c program for selection sort with output, c program for selection sort with explanation, c program for selection sort using pointers, c program for selection sort in descending order.

BUBBLE SORT - Write a c program to BUBBLE SORT an array

#include<stdio.h>
main()
{
     int a[10],i,n;
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
 scanf("%d",&a[i]);
     bub_sort(a,n);
     getch();
}
bub_sort(int a[],int n)
{
     int i,j,temp,limit;
     limit=n-1;
     for(i=0;i<limit;i++)
     {
 for(j=0;j<limit-i;j++)
 {
      if(a[j]>a[j+1])
      {
   temp=a[j];
   a[j]=a[j+1];
   a[j+1]=temp;
      }
 }
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
 printf("%d  ",a[i]);
}
Tags: c program to BUBBLE SORT an array, c program for bubble sort using arrays with explanation, bubble sort array java, c program for bubble sort with output, c program for bubble sort using recursion, c program for bubble sort using pointers, c program for bubble sort using functions, c program for bubble sort algorithm, c program for bubble sort with flag.


Learn More :