C Program For Quick Sort Using Divide Conquer

Ram Pothuraju
#include< stdio.h>
#include< conio.h>
#define MAX 50

//int pivotpoint;
void display(int[MAX],int);
void Quick_sort(int[MAX],int low,int high);
int pivot(int[MAX],int,int);
void main()
{
 int i,T[MAX],n;
 clrscr();


 printf("\n Enter number of element you want:");
 scanf("%d",&n);

 for(i=0;i< n;i++)
 {
  printf("\n Enter element[%d]:",i);
  scanf("%d",&T[i]);
 }
 printf("\n------------------------------------------");
 printf("\n Elements before sorting:");
 display(T,n);
 printf("\n------------------------------------------");

Quick_sort(T,0,n-1);

 printf("\n------------------------------------------");
 printf("\n Elements after sorting:");
 display(T,n);
 printf("\n------------------------------------------");

getch();
}
void Quick_sort(int T[MAX],int low,int high)
{
    int pivotpoint;
 if(high > low)
 {
  pivotpoint=pivot(T,low,high);
  Quick_sort(T,low,pivotpoint-1);
  Quick_sort(T,pivotpoint+1,high);
 }

}
int pivot(int T[MAX],int low,int high)
{
 int i,j;
 int pivotitem,temp;

 pivotitem=T[low];
 i=low;
 j=high;

 while(i <= j)
 {
  while(T[i] <= pivotitem)
   i++;
   
  while(T[j] > pivotitem)
   j--;
  if(i < j)
  {
   temp=T[i];
   T[i]=T[j];
   T[j]=temp;
  }
 }
 temp=T[low];
 T[low]=T[j];
 T[j]=temp;
 return j;
}

void display(int T[MAX],int n)
{
 int i;
 for(i=0;i < n;i++)
 {
  printf(" %d ",T[i]);
 }
}


OUTPUT


Enter number of element you want:10

 Enter element[0]:56

 Enter element[1]:33

 Enter element[2]:2

 Enter element[3]:90

 Enter element[4]:67

 Enter element[5]:45

 Enter element[6]:43

 Enter element[7]:77

 Enter element[8]:56

 Enter element[9]:2

------------------------------------------------------------------------------------
 Elements before sorting: 56  33  2  90  67  45  43  77  56  2
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
 Elements after sorting: 2  2  33  43  45  56  56  67  77  90
------------------------------------------------------------------------------------

Post a Comment

0Comments

Post a Comment (0)