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
|
#include <iostream>
using namespace std;
void quickSort (int*, int, int);
int partition(int*, int, int);
int main()
{
int a[12];
for(int i=0; i<12; i++)
{
cout<< "Number " << i+1 << " ";
cin>>a[i];
}
quickSort(a,0,11);
for (int i=0; i<12 ; i++)
{
cout << a[i] << endl;
}
}
void quickSort (int T[], int first , int last)
{
int cut;
if (first<last)
{
cut = partition(T, first,last);
quickSort(T, first,cut);
quickSort (T, cut+1, last);
}
}
int partition(int T[], int first,int last)
{
int pivot, temp;
int loop, cutPoint, bottom, top;
pivot=T[first];
bottom = first;
top = last;
loop=1;
while (loop)
{
while (T[top]>pivot)
{
top--;
}
while(T[bottom]<pivot)
{
bottom++;
}
if (bottom<top)
{
temp=T[bottom];
T[bottom]=T[top];
T[top]=temp;
}
else
{
loop=0;
cutPoint = top;
}
}
return cutPoint;
}
|