#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
#define OK 1
#define ERROR -1
#define MAXNUM 11
typedef int Status;
// use define a basic struct shape
typedef struct {
int key; // key field
char *otherInfo;
}ElemType;
typedef struct {
ElemType *data;
int length;
}sqList;
// in this program we will reverse the all sequence
// or other says sort from low to high
// Create basic Sort
void CreateSort(sqList &l) {
// Allocate size
l.data = new ElemType[MAXNUM + 1];
l.length = MAXNUM;
int i = 1;
for (int j = MAXNUM; j >= 1; j--) {
// allocate
l.data[i++].key = j;
}
}
void Print_result(sqList l) {
for (int i = 1; i < l.length; i++) {
cout << l.data[i].key << " ";
}
}
Status Partition(sqList &l, int low, int high) {
//Partition means spread
int pivotkey;
l.data[0] = l.data[low]; // as low means rotate
pivotkey = l.data[low].key;
while (low < high) {
while (low < high && l.data[high].key >= pivotkey)high--;
// search exc
l.data[low] = l.data[high];
while (low < high && l.data[low].key <= pivotkey)low++;
//exchange number all
l.data[high] = l.data[low];
}
l.data[low] = l.data[0]; // 枢轴记录到位
return low;
}
void QSort(sqList &l, int low, int high) {
int pivoteloc;
if (low < high) {
//recursive
pivoteloc = Partition(l, low, high);
QSort(l, low, pivoteloc - 1);
QSort(l, pivoteloc + 1, high);
}
}
void main() {
sqList l;
CreateSort(l);
Print_result(l);
QSort(l, 1, MAXNUM);
Print_result(l);
char c;
cin >> c;
}
Quick Sort
最新推荐文章于 2021-07-20 22:16:03 发布