(Quicksort) You have previously seen the sorting techniques of the bucket sort and selection sort. We now present the recursive sorting technique called Quicksort. The basic algorithm for a single-subscripted array of values is as follows:
a) Partitioning Step: Take the first element of the unsorted array and determine its final location in the sorted array (i.e., all values to the left of the element in the array are less than the element, and all values to the right of the element in the array are greater than the element). We now have one element in its proper location and two unsorted subarrays.
b) Recursive Step: Perform Step 1 on each unsorted subarray.
Each time Step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are created. When a subarray consists of one element, that subarray must be sorted; therefore, that element is in its final location. The basic algorithm seems simple enough, but how do we determine the final position of the first element of each subarray? As an example, consider the following set of values (the element in bold is the partitioning element—it will be placed in its final location in the sorted array):
37 2 6 4 89 8 10 12 68 45
a) Starting from the rightmost element of the array, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 12, so 37 and 12 are swapped. The values now reside in the array as follows:
12 2 6 4 89 8 10 37 68 45
Element 12 is in italics to indicate that it was just swapped with 37.
b) Starting from the left of the array, but beginning with the element after 12, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. The first element greater than 37 is 89, so 37 and 89 are swapped. The values now reside in the array as follows:
12 2 6 4 37 8 10 89 68 45
c) Starting from the right, but beginning with the element before 89, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 10, so 37 and 10 are swapped. The values now reside in the array as follows:
12 2 6 4 10 8 37 89 68 45
d) Starting from the left, but beginning with the element after 10, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. There are no more elements greater than 37, so when we compare 37 with itself, we know that 37 has been placed in its final location of the sorted array. Once the partition has been applied to the array, there are two unsorted subarrays. The subarray with values less than 37 contains 12, 2, 6, 4, 10 and 8. The subarray with values greater than 37 contains 89, 68 and 45. The sort continues with both subarrays being partitioned in the same manner as the original array.
Based on the preceding discussion, write recursive function quickSort to sort a single-sub- scripted integer array. The function should receive as arguments an integer array, a starting sub-script and an ending subscript. Function partition should be called by quickSort to perform the partitioning step.
#include
#include
#include
#include
using namespace std;
const int SIZE = 10;
const int MAX_NUMBER = 1000;
// function prototypes
void quicksort( int * const, int, int );
int partition( int * const, int, int );
void swap( int * const, int * const );
int main()
{
int arrayToBeSorted[ SIZE ] = {};
int loop;
srand( time( 0 ) );
// randomly generate content
for ( loop = 0; loop < SIZE; loop++ )
arrayToBeSorted[ loop ] = rand() % MAX_NUMBER;
cout << "Initial array values are:\n";
// print out values of the array
for ( loop = 0; loop < SIZE; loop++ )
cout << setw( 4 ) << arrayToBeSorted[ loop ];
cout << "\n\n";
// if there is only one element
if ( SIZE == 1 )
cout << "Array is sorted: " << arrayToBeSorted[ 0 ] << '\n';
else
{
quicksort( arrayToBeSorted, 0, SIZE - 1 );
cout << "The sorted array values are:\n";
for ( loop = 0; loop < SIZE; loop++ )
cout << setw( 4 ) << arrayToBeSorted[ loop ];
cout << endl;
} // end else
} // end main
// recursive function to sort array
void quicksort( int * const array, int first, int last )
{
int currentLocation;
if ( first >= last )
return;
currentLocation = partition( array, first, last ); // place an element
quicksort( array, first, currentLocation - 1 ); // sort left side
quicksort( array, currentLocation + 1, last ); // sort right side
} // end function quicksort
// partition the array into multiple sections
int partition( int * const array, int left, int right )
{
int position = left;
// loop through the portion of the array
while ( true )
{
while ( array[ position ] <= array[ right ] && position != right )
right--;
if ( position == right )
return position;
if ( array[ position ] > array[ right ])
{
swap( &array[ position ], &array[ right ] );
position = right;
} // end if
while ( array[ left ] <= array[ position ] && left != position )
left++;
if ( position == left )
return position;
if ( array[ left ] > array[ position ] )
{
swap( &array[ position ], &array[ left ] );
position = left;
} // end if
} // end while
} // end function partition
// swap locations
void swap( int * const ptr1, int * const ptr2 )
{
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
} // end function swap
Initial array values are:
630 971 223 286 354 322 134 846 615 224
The sorted array values are:
134 223 224 286 322 354 615 630 846 971
You might also like to view...
The Function Bar displays the value or formula contained in the active cell
Indicate whether the statement is true or false
Checking to make sure users input required data is an example of data validation
Indicate whether the statement is true or false