Write a program to implement Quick Sort technique in C Language

 


#include<stdio.h>
#include<conio.h>
void quickSort(int arr[10],int first,int last)
{
  int pivot,i,j,temp;
  if(first < last)
  {
    pivot = first;
    i = first;
    j = last;
    while(i < j)
    {
      while(arr[i] <= arr[pivot] && i < last)
       i++;
      while(arr[j] > arr[pivot])
       j--;
      if(i <j)
      {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
    temp = arr[pivot];
    arr[pivot] = arr[j];
    arr[j] = temp;
    quickSort(arr,first,j-1);
    quickSort(arr,j+1,last);
    }
}
void main()
{
  int size,i,j,temp,arr[100];
  clrscr();
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d integer values: ",size);
  for(i=0; i<size; i++)
      scanf("%d",&arr[i]);
  printf("\n Before sorting array elements are: ");
  for(i=0; i<size; i++)
      printf(" %d ",arr[i]);
  
   quickSort(arr,0,size-1);
  printf("\n After sorting array elements are: ");
  for(i=0; i<size; i++)
      printf(" %d ",arr[i]);
  getch();
}

Post a Comment

0 Comments