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
|
#include "stdafx.h"
using namespace std;
typedef struct
{
int first;
int last;
int splitPt1;
int splitPt2;
}values ;
typedef values ItemType;
void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2);
void QuickSort (ItemType values[], int first, int last);
int _tmain(int argc, _TCHAR* argv[])
{
int list[10];
//code: get data to fill the collection
cout << "Enter 10 positive integers for the list\n";
for (int i = 0; i < 10; i++)
cin >> list[i];
return 0;
}
void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2)
{
ItemType splitVal = values[(first+last)/2],t;
int i = first, j = last;
while (values[i] < splitVal) i++;
while (values[j] > splitVal) j--;
while (i < j)
{
t = values[i]; values[i] = values[j];
values[j] = t;
i++;
j--;
while (values[i] < splitVal) i++;
while (values[j] > splitVal) j--;
}
if (i == j)
{
i++; j--;
}
splitPt1 = i;
splitPt2 = j;
}
void QuickSort (ItemType values[], int first, int last)
{
int splitPt1, splitPt2;
if (first < last)
{
Split(values, first, last, splitPt1, splitPt2);
QuickSort(values, first, splitPt2);
QuickSort(values, splitPt1, last);
}
|