Implementation of quick sort using C in dataStructure developerIndian.com

Updated:01/02/2023 by Computer Hope

Go Back



The sorting method known as quick sort is incredibly effective and works by dividing a large array of data into smaller arrays. A huge array is divided into two arrays: one array contains values larger than the pivot value, and the second array contains values smaller than the pivot value, which is the basis for the partition.
Want to learn how we can implement quick sort using C in dataStructure in c


            #include<stdio.h>
#include<conio.h>
int quick(int *p,int l, int u)
   { int pos=l,t;
     while(1)
      {
      while(p[u]>p[pos] && pos != u)
	  u--;
      if(pos==u)
	return pos;
      else
        {
          t=p[pos];
          p[pos]=p[u];
          p[u]=t;
          pos=u;   }
      while(p[l]<=p[pos] && pos != l)
	  l++;
       if(pos==l)
	 return pos;
       else
         {
          t=p[pos];
          p[pos]=p[l];
          p[l]=t;
          pos=l;  
         }  
      }   
   }
void quicksort(int *p,int n)
   {
   int lb=0,ub=n-1,pos;
   int lower[10],upper[10],top=-1;
   lower[++top]=lb;
   upper[top]=ub;
   while(top != -1)
      {
      lb=lower[top];
      ub=upper[top--];
      pos=quick(p,lb,ub);
      if(pos+1<ub)
        {
        lower[++top]=pos+1;
        upper[top]=ub;
        }
      if(pos-1>lb)
        {
        lower[++top]=lb;
        upper[top]=pos-1;
        }
      }
   }

 void main()
   {  int x[10],i;
      clrscr();
      printf("\n Enter Array X ");
      for(i=0;i<10;i++)
           scanf("%d",&x[i]);
       quicksort(x,10); //calling fn 
       printf("\n After Sortig ");
       for(i=0;i<10;i++)
            printf("  %d",x[i]);
   }

          

Conclusion

In this article , we see quick sort is the recursive function implementing the Quick Sort algorithm. It partitions the array around the pivot, and recursively sorts the subarrays before and after the partition. Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst-case complexity are O(n^2), respectively.