Monday, March 16, 2009

C Program Quick Sort

// Program for Quick Sort

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

enum Boolean { False , True } ;

//-------------------------------------------------------------------

void Quicksort ( int array[] , int LB , int UB )
{
Boolean Flag = True ;
int i , j , Keyval , temp ;

if ( LB < UB )
{
i = LB , j = UB+1 ;

Keyval = array [ LB ] ;

while ( Flag == True )
{

i++ ;
while ( array[i] < Keyval )
i++ ;

j--;
while ( array[j] > Keyval )
j--;

if ( i < j )
{
temp = array[i] ;
array[i] = array[j];
array[j] = temp ;
}
else
Flag = False ;

}

temp = array [ LB ] ;
array [ LB ] = array [ j ] ;
array [ j ] = temp ;

Quicksort ( array , LB , j-1 ) ;
Quicksort ( array , j+1 , UB ) ;

}

return ;
}


void Showarray ( int array[] , int UB )
{
gotoxy ( 1,2 ) ;
cout << "\n" ;

for ( int i = 0 ; i < UB ; i++ )
{
cout << array [i] << endl ;
}

}

//-------------------------------------------------------------------

void main ()
{
clrscr ();

int array [10] = { 42 , 23 , 74 , 11 , 65 , 58 , 94 , 36 , 99 , 87 };

Quicksort ( array , 0, 9 ) ;

Showarray ( array , 10 ) ;


getch();
}

No comments:

Post a Comment