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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include<iostream>
#include<iomanip>
using namespace std;
int a[10];
int* aptr = a;
void quickSort(int arr[], int start, int end)
{
int o = start;
int p = end;
int midValue = arr[o];
int midPosition = o;
int oo = start;
int pp = end;
while (oo < pp)
{
for (int i = p ; arr[i] > o ; i--)
{
if (arr[i] < midValue)
{
swap(arr[i], arr[o]);
midPosition = i;
pp = i;
break;
}
}
for (int i = o ; arr[i] < p ; i++)
{
if (arr[i] > midValue)
{
swap(arr[o], arr[midPosition]);
midPosition = i;
oo = i;
break;
}
}
}
if (midPosition != 0 && midPosition != 9)
{
quickSort(arr, start, midPosition - 1);
quickSort(arr, midPosition + 1, end);
}
else if (midPosition == 0)
{
quickSort(arr, midPosition + 1, end);
}
else
{
quickSort(arr, 0, midPosition - start);
}
}
int main()
{
int values, origin;
cout << "Initial array values are:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> values;
a[i] = values;
}
quickSort(a, 0, 9);
cout << "\nThe sorted array values are:" << endl;
cout << setw(6) << a[0] << setw(6) << a[1] << setw(6) << a[2] << setw(6) << a[3] << setw(6) << a[4];
cout << setw(6) << a[5] << setw(6) << a[6] << setw(6) << a[7] << setw(6) << a[8] << setw(6) << a[9] << endl;
return 0;
}
|