Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Quick Sort

Source Code:

#include<iostream.h>
#include<conio.h>

void quicksort(int*,int,int);
int partion(int*,int,int);
void main()
{clrscr();
 cout<<"Enter ten number for quick sort by-Tarun rawat:\n\n";


 int a[10];
 for(int i=0;i<10;i++)
 {cin>>a[i];
 }
 int p=0,q=9;
 quicksort(a,p,q);
 cout<<"After applying quick sort :\n";
 for(i=0;i<10;i++)
 {cout<<a[i]<<" ";
 }
 getch();
}

void quicksort(int a[],int p,int q)
{ if(p<q)
  {int r=partion(a,p,q);
  quicksort(a,p,r-1);
  quicksort(a,r+1,q);
  }
}

int partion(int a[],int p,int q)
{ int x=a[p];
  int i=p;int temp;
  for(int j=p+1;j<=q;j++)
     { if(a[j]<= x)
     {i=i+1;
           temp=a[i];
           a[i]=a[j];
           a[j]=temp;
     }
     }
   temp=a[p];
   a[p]=a[i];
   a[i]=temp;
   return i;
}

No comments:

Post a Comment

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)