Click to See Complete Forum and Search --> : quicksort


mhaycee
September 20th, 2005, 02:48 AM
good day. can you give me a system using quicksort or at least a site where i can see one? i badly need it. thank you

humptydumpty
September 20th, 2005, 03:02 AM
good day. can you give me a system using quicksort or at least a site where i can see one? i badly need it. thank you

Just go through MSDN.
or try to find any data structure book.there you can find qsort method and it's algo.
Here is one Example in C++ may be this one can help u

* @param a an integer array
* @param lo0 left boundary of array partition
* @param hi0 right boundary of array partition
*/
void QuickSort(int a[], int lo0, int hi0) throws Exception
{
int lo = lo0;
int hi = hi0;
int mid;

// pause for redraw
pause(lo, hi);
if ( hi0 > lo0)
{

/* Arbitrarily establishing partition element as the midpoint of
* the array.
*/
mid = a[ ( lo0 + hi0 ) / 2 ];

// loop through the array until indices cross
while( lo <= hi )
{
/* find the first element that is greater than or equal to
* the partition element starting from the left Index.
*/
while( ( lo < hi0 ) && ( a[lo] < mid ) )
++lo;

/* find an element that is smaller than or equal to
* the partition element starting from the right Index.
*/
while( ( hi > lo0 ) && ( a[hi] > mid ) )
--hi;

// if the indexes have not crossed, swap
if( lo <= hi )
{
swap(a, lo, hi);
// pause
pause();

++lo;
--hi;
}
}

/* If the right index has not reached the left side of array
* must now sort the left partition.
*/
if( lo0 < hi )
QuickSort( a, lo0, hi );

/* If the left index has not reached the right side of array
* must now sort the right partition.
*/
if( lo < hi0 )
QuickSort( a, lo, hi0 );

}
}

void swap(int a[], int i, int j)
{
int T;
T = a[i];
a[i] = a[j];
a[j] = T;

}

void sort(int a[]) throws Exception
{
QuickSort(a, 0, a.length - 1);
}
}