#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void display(int *list)
{
for(int x = 0; x < 10; x ++){
cout << list[x] << " ";
}
cout << endl;
}
void iqsort(int *list, int m, int n)
{
int k, t, i, j;
if(m < n){
i = m;
j = n + 1;
k = list[m];
while(i < j){
for(i ++; i < n; i++){
if(list[i] >= k){
break;
}
}
for(j--; j > m; j--){
if(list[j] <= k){
break;
}
}
if(i < j){
cout << "swap list[" << i << "]--" << list[i] << " and list[" << j << "]--" << list[j] << endl;
t = list[i];
list[i] = list[j];
list[j] = t;
}
display(list);
}
cout << "swap list[" << m << "]--" << list[m] << " and list[" << j << "]--" << list[j] << endl;
t = list[m];
list[m] = list[j];
list[j] = t;
display(list);
cout << "next round " << j << endl;
iqsort(list, m, j-1);
iqsort(list, j+1, n);
}
}
int main(int argc, char* argv[])
{
int i, j, n;
int list[100] = {9, 3, 2, 7, 5, 4, 6, 1, 8};
srand((unsigned)time(NULL));
cout << "before sort " << endl;
for(i = 0; i < 10; i ++){
n = rand()%100;
}
display(list);
cout << endl << "in sort process " << endl;
iqsort(list, 0, 9);
cout << "after sort" << endl;
display(list);
return 0;
}
Output
before sort
9 3 2 7 5 4 6 1 8 0
in sort process
9 3 2 7 5 4 6 1 8 0
swap list[0]--9 and list[9]--0
0 3 2 7 5 4 6 1 8 9
next round 9
0 3 2 7 5 4 6 1 8 9
swap list[0]--0 and list[0]--0
0 3 2 7 5 4 6 1 8 9
next round 0
swap list[3]--7 and list[7]--1
0 3 2 1 5 4 6 7 8 9
0 3 2 1 5 4 6 7 8 9
swap list[1]--3 and list[3]--1
0 1 2 3 5 4 6 7 8 9
next round 3
0 1 2 3 5 4 6 7 8 9
swap list[1]--1 and list[1]--1
0 1 2 3 5 4 6 7 8 9
next round 1
0 1 2 3 5 4 6 7 8 9
swap list[4]--5 and list[5]--4
0 1 2 3 4 5 6 7 8 9
next round 5
0 1 2 3 4 5 6 7 8 9
swap list[6]--6 and list[6]--6
0 1 2 3 4 5 6 7 8 9
next round 6
0 1 2 3 4 5 6 7 8 9
swap list[7]--7 and list[7]--7
0 1 2 3 4 5 6 7 8 9
next round 7
after sort
0 1 2 3 4 5 6 7 8 9