Q:

Write A Program To Quick Sort Using Dynamic Array

0

Write A Program To Quick Sort Using Dynamic Array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<stdlib.h>
using namespace std;

void quicksort(int *,int ,int );

int main()
{
  int *a,s,i;

  cout<<"ENTER THE SIZE OF ARRAY: \n\n";
    cin>>s;

    a=(int*)malloc(s*sizeof(int));

    cout<<"ENTER THE NUMBER: ";
    for(i=0;i<s;i++)
    {
   cout<<"ENTER THE ELEMENT "<<i+1<<" :";
     cin>>a[i];
  }
  quicksort(a,0,s-1);
    cout<<"\nSORTED ARRAY IN ACCENDING ORDER: \n\n";
    for(i=0;i<s;i++)
    cout<<a[i]<<" ";
}

void quicksort(int a[],int first,int last)
{
  int pivot,j,temp,i;

      if(first<last)
   {
         pivot=first;
         i=first;
         j=last;

         while(i<j)
   {
             while(a[i]<=a[pivot]&&i<last)
             i++;
             while(a[j]>a[pivot])
             j--;
               
             if(i<j)
    {
                 temp=a[i];
                  a[i]=a[j];
                  a[j]=temp;
             }
         }

         temp=a[pivot];
         a[pivot]=a[j];
         a[j]=temp;
       
         quicksort(a,first,j-1);
         quicksort(a,j+1,last);

    }
}

 

Output:

ENTER THE SIZE OF ARRAY: 

 

5

ENTER THE NUMBER: ENTER THE ELEMENT 1 :414

ENTER THE ELEMENT 2 :5535

ENTER THE ELEMENT 3 :3639

ENTER THE ELEMENT 4 :7878

ENTER THE ELEMENT 5 :552

 

SORTED ARRAY IN ACCENDING ORDER: 

414 552 3639 5535 7878 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now