0% found this document useful (0 votes)
30 views

#Include #Include: Const Int Class Private Int Int Public

The document describes an implementation of quicksort in C++. It defines a class called array that holds integers and implements functions for adding elements, getting the count, sorting using quicksort, and displaying the array. The quicksort function recursively calls itself to sort subsections of the array by picking a pivot element and partitioning the array around it. The main function demonstrates adding random numbers to an array object, sorting it using quicksort, and displaying the array before and after sorting.

Uploaded by

princeji
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

#Include #Include: Const Int Class Private Int Int Public

The document describes an implementation of quicksort in C++. It defines a class called array that holds integers and implements functions for adding elements, getting the count, sorting using quicksort, and displaying the array. The quicksort function recursively calls itself to sort subsections of the array by picking a pivot element and partitioning the array around it. The main function demonstrates adding random numbers to an array object, sorting it using quicksort, and displaying the array before and after sorting.

Uploaded by

princeji
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUICK SORTING

INTRODUCTION:
ALGORITHM:
PROGRAM:
#include <iostream.h>
#include<conio.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 ;
}
int 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( );
getch();
}
SAMPLE OUTPUT:

Quick sort:
Array before sorting
11 2 9 13 57 25 17 1 90 3
Array after quick sorting
1 2 3 9 11 13 17 25 57 90

You might also like