C++ program to implement Quick sort Algorithm using class
#include <iostream.h>
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quiksort ( int lower, int upper ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
int array :: getcount( )
{
return count ;
}
void array :: quiksort ( int lower, int upper )
{
if ( upper > lower )
{
int i = split ( arr, lower, upper ) ;
quiksort ( lower, i - 1 ) ;
quiksort ( i + 1, upper ) ;
}
}
int array :: split ( int *a, int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
void main( )
{
array a ;
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
cout << "\nQuik sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quiksort ( 0, c - 1 ) ;
cout << "\nArray after quick sorting:" << endl ;
a.display( ) ;
} |
Hi,
This was really helpful.
Thank you.
java programing language
pls send some easy coding … the coding is too lengthy,,,,,,,,,,,,,,,,,,,
This is not too much good for learner of c++ language, so try to make as much as easy…….
C++ PROGRAM TO IMPLEMENT QUICK SORT ALGORITHM, Solution
http://programscpp.blogspot.com/2012/09/c-program-to-implement-quick-sort-algorithm-template-class.html
This is not gonna work if numbers are repetitive, it would just be stuck infinitely.
hey, i have 2 complete C programs that are perfectly correct and checked.
both of these programs are for recursive implementation of quick sort in C.
for source code visit
http://codingloverlavi.blogspot.in/2013/04/quick-sort.html
hope u find them useful…
hi, tanks a lot.
good look
we need a simple and easy program….it is too lengthy……
we need simple and easy program… it is too lengthy……
Yes… This is very helpful….http://www.codingclasses.org
.