Write a Program to perform recursive Quick Sort. - Data Structure using C

Translate

Thursday, December 10, 2020

Write a Program to perform recursive Quick Sort.

 Write a Program to perform recursive Quick Sort.

Input Format:

The first line consists of an integer N denoting the size of the array. The next line contains N space-separated integers denoting the elements of the array.

Output Format

Print N space-separated integers in ascending order

code👇👇👇👇👇👇👇👇👇

#include<stdio.h>

void quicksort(int number[25],int first,int last){

   int i, j, pivot, temp;


   if(first<last){

      pivot=first;

      i=first;

      j=last;


      while(i<j){

         while(number[i]<=number[pivot]&&i<last)

            i++;

         while(number[j]>number[pivot])

            j--;

         if(i<j){

            temp=number[i];

            number[i]=number[j];

            number[j]=temp;

         }

      }


      temp=number[pivot];

      number[pivot]=number[j];

      number[j]=temp;

      quicksort(number,first,j-1);

      quicksort(number,j+1,last);


   }

}


int main(){

   int i, count, number[25];


   printf("How many elements are u going to enter?: ");

   scanf("%d",&count);


   printf("Enter %d elements: ", count);

   for(i=0;i<count;i++)

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


   quicksort(number,0,count-1);


   printf("Order of Sorted elements: ");

   for(i=0;i<count;i++)

      printf(" %d",number[i]);


   return 0;

}





No comments:

Post a Comment

Introduction to Arrays

  Introduction to Arrays An array is a data structure that allows you to store a collection of elements of the same type. Each element in th...