1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
using namespace std;
void swap(int a[], int i, int j)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void find(int array[], int left, int right, int pivotIndex)
{
int pivotValue = array[pivotIndex];
swap(array, pivotIndex, right); //Move pivot to end
int storeIndex = left;
int i;
for (i = left; i < right; i++)
{
if (array[i] <= pivotValue)
{
swap(array, i, storeIndex);
storeIndex++;
}
}
swap(array, storeIndex, right); //Move pivot to its final place
}
int main()
{
int test[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int b, pivot;
int size = sizeof( test ) / sizeof( test[0] );
cout << "What position in the array should be the Pivot?" << endl;
cin >> pivot;
cout << "The Pivot value is: " << test[pivot] << endl;
find(test, 0, size, pivot);
while(b < size)
{
cout << test[b] << " ";
b++;
}
return 0;
}
|