Write a C program to sort a list of N elements of integer type using quick sort Algorithm

#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int lb,int ub);
void main()
{
 int i,n,a[20];
 clrscr();
 printf("\n * * * * *  * * * * *");
 printf("\n      INPUT       \n");
 printf("enter the size of the array:");
 scanf("%d",&n);
 printf("\n enter the elements of the array :");
 for(i=0;i<n;i++)
 scanf("%d",&a[i]);
 printf("\n * * * * * * * * * * *");
 printf("\n        OUTPUT ");
 printf("\n orginal list \n");
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
 quicksort(a,0,n-1);
 printf("\n * * * * * * * * * * \n");
 printf("\n sorted list\n ");
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
 getch();
 }
 void quicksort(int a[],int lb,int ub)
 {
  int up,down,temp,key;
  int flag=1 ;
  if(lb<ub)
  {
   up=lb;
   down=ub;
   key=a[lb];
   while(flag)
   {
    up++;
    while(a[up]<key)
    up++;
    while(a[down]>key)
    down--;
    if(up<down)
    {
     temp=a[up];
     a[up]=a[down];
     a[down]=temp;
    }
    else
    flag=0;
    }
    temp=a[lb];
    a[lb]=a[down];
    a[down]=temp;
    quicksort(a,lb,down-1);
    quicksort(a,down+1,ub);
    }
   }

No comments:

Post a Comment