#include<stdio.h>
void swap(int* x, int* y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int Getmidnum(int shu[], int left, int right)
{
int mid = (left + right) / 2;
if (shu[left] > shu[right])
{
if (shu[mid] > shu[left]) return left;
else if (shu[mid] < shu[right]) return right;
else return mid;
}
else
{
if (shu[mid] < shu[left]) return left;
else if (shu[mid] > shu[right]) return right;
else return mid;
}
}
int Partsort(int a[], int left, int right)
{
int key, temp, mid;
mid = Getmidnum( a, left, right);
swap(&a[left], &a[mid]);
key = left;
while (left < right)
{
while (a[right] > a[key]) right--;
while (a[left] < a[key]) left++;
if(left < right) swap(&a[left], &a[right]);
}
swap(&a[left], &a[key]);
return left;
} //霍尔法
/*int Partsort(int a[], int left, int right)
{
int key, temp, hole, mid;
mid = Getmidnum( a, left, right);
swap(&a[left], &a[mid]);
key = a[left];
hole = left;
while (left < right)
{
while (a[right] > key) right--;
a[hole] = a[right];
hole = right;
while (a[left] < key && left < right) left++;
a[hole] = a[left];
hole = left;
}
a[hole] = key;
return hole;
} */
//挖坑法
void Quicksort(int a[], int begin, int end)
{
int key;
if (begin >= end) return;
key = Partsort(a, begin, end);
Quicksort(a, begin, key);
Quicksort(a, key + 1, end);
}
int main()
{
int i,a[11] = { 10,3,2,6,9,1,4,7,5,8,11 };
int left = 0, right = 10;
Quicksort(a, left, right);
for (i = 0; i < 11; i++)
{
printf("%d ", a[i]);
}
return 0;
}