Skip to main content

QUICK SORT

CODING:
#include<stdio.h>
#include<conio.h>
int partition(int list[],int beg,int end);
void quick_sort(int list[],int beg,int end);
void main()
{
int arry[50],i,n;
clrscr();
printf("How many enter the element:");
scanf("%d",&n);
for(i=0;i<n;i++)
 {
  printf("Enter the number:");
  scanf("%d",&arry[i]);
 }
quick_sort(arry,0,n-1);
printf("YOUR SORTED LIST:\n");
 for(i=0;i<n;i++)
printf("%d\n",arry[i]);
getch();
}
void quick_sort(int list[],int beg,int end)
{
int loc;
if(beg<end)
 {
 loc=partition(list,beg,end);
 quick_sort(list,beg,loc-1);
 quick_sort(list,loc+1,end);
 }
}
int partition(int list[],int beg,int end)
{
int left,right,temp,loc,flag;
loc=left=beg;
right=end;
flag=0;
while(flag!=1)
{
 while((list[loc]<=list[right]) && (loc!=right))
    right--;
  if(loc==right)
    flag=1;
  else if(list[loc]>list[right])
   {
    temp=list[loc];
    list[loc]=list[right];
    list[right]=temp;
    loc=right;
   }
  if(flag!=1)
  {
   while((list[loc]>=list[left]) && (loc!=left))
     left++;
    if(loc==left)
     flag=1;
    else if(list[loc]<list[left])
    {
     temp=list[loc];
     list[loc]=list[left];
     list[left]=temp;
     loc=left;
    }

  }
}
return loc;
}

Comments