QUICK SORT using divide and conquer

 
QUICK SORT

#include <stdio.h>


int partition(int array[], int low, int high) {


    int pivot = array[high];



  int i = (low - 1);

  int temp;



  for (int j = low; j < high; j++) {

    if (array[j] <= pivot) {



      i++;

      temp=array[i];

      array[i]=array[j];

      array[j]=temp;



    }

  }


      temp=array[i+1];

      array[i+1]=array[high];

      array[high]=temp;


  return (i + 1);

}

/*gujju computervalo*/

void quickSort(int array[], int low, int high) {

  if (low < high) {



    int pi = partition(array, low, high);



    quickSort(array, low, pi - 1);



    quickSort(array, pi + 1, high);

  }

}



// main function

int main() {

  printf("enter length of your  array :- ");

    printf("\n");

    int n;

    scanf("%d",&n);


    int array[n];


    printf("Enter %d element in array:- ",n);

    printf("\n");

    int temp;



    for(int i=0;i<n;i++)

    {

        scanf("%d",&array[i]);

    }



  quickSort(array, 0, n - 1);


  printf("Sorted array in ascending order: \n");

  for(int i=0;i<n;i++)

    {

        printf("%d\t",array[i]);

    }


}


output:-- 







Comments