快速排序(QuikSort)
具有O(n log n)的平均运行时间,split(int*, left, right)是找出划分点i,之后再分割数组分别进行排序。
void qsort(int *data, int left, int right)
{
if(right > left)
{
int p;
p = split(data, left, right);
qsort(data, left, p – 1);
qsort(data, p + 1, right);
}
}
int split(int *data, int left, int right)//找到分割点i,把小于data[i]的元素都移到data[i]前,大于的都移到后面
{
int temp = data[left];
int i = left, j, temp2;
for(j = left + 1; j <= right; j++)
{
if(data[j] <= temp)
{
i++;
if(i != j)
{
temp2 = data[i];
data[i] = data[j];
data[j] = temp2;
}
}
}
temp2 = data[left];
data[left] = data[i];
data[i] = temp2;
return i;
}