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
|
#include <iostream>
using namespace std;
void quicksort(int[], int, int);
int partitioning(int[], int, int);
int main()
{
const int NumArrays{4}, Size{9};
int a[NumArrays][Size]
{
{ 5, 4, 2, 9, 7, 1, 3, 6, 8 },
{ 1, 3, 6, 8, 5, 4, 2, 9, 7 },
{ 8, 5, 4, 2, 1, 3, 6, 9, 7 },
{ 9, 7, 1, 3, 6, 5, 4, 2, 8 },
};
/*
for (int k = 0; k < NumArrays; ++k)
{
cout << "Enter the elements of array " << char('A' + k) << ": ";
for (int i = 0; i < Size; ++i) cin >> a[k][i];
}
*/
for (int k = 0; k < NumArrays; ++k)
{
cout << "Elements of array " << char('A' + k) << " before sorting: ";
for (int i = 0; i < Size; ++i) cout << a[k][i] << ' ';
cout << '\n';
quicksort(a[k], 0, Size - 1);
cout << "Elements of array " << char('A' + k) << " after sorting : ";
for (int i = 0; i < Size; ++i) cout << a[k][i] << ' ';
cout << '\n';
}
}
void quicksort(int a[], int l, int h)
{
if (l < h)
{
int u = partitioning(a, l, h);
quicksort(a, l, u - 1);
quicksort(a, u + 1, h);
}
}
int partitioning(int a[], int i, int j)
{
int pivote = a[i], k = i;
do
{
do ++i; while (a[i] < pivote);
while (a[j] > pivote) j--;
if (i < j) swap(a[i], a[j]);
} while (i < j);
a[k] = a[j];
a[j] = pivote;
return j;
}
|