int median(int a,int b,int c)
{
if ((a > b || a > c) && (a < c || a < b))
return 1;
else
return 0;
}
int choosePivot(int a[],int len)
{
int temp;
int head = 0,middle = len % 2 ? len / 2: len / 2 - 1 , tail = len - 1;
if (median(a[head],a[middle],a[tail]))
{
return 0;
}
else if (median(a[middle],a[head],a[tail]))
{
temp = a[middle];
a[middle] = a[head];
a[head] = temp;
}
else if (median(a[tail],a[head],a[middle]))
{
temp = a[tail];
a[tail] = a[head];
a[head] = temp;
}
return 0;
}
void quickSort(int a[],int len)
{
int pivot = 0;
int i = 1,j,temp;
int flag = 0;
if (len <= 1)
return;
else
pivot = choosePivot(a,len);
for (j = 1;j
a[pivot])
{
flag = 1;
}
else
{
if (flag == 0)
{
i++;
}
else
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
i++;
}
}
}
if (i == len)
{
temp = a[i-1];
a[i-1] = a[pivot];
a[pivot] = temp;
quickSort(a,len - 1);
}
else if (i == 1)
{
quickSort(a + 1,len - 1);
}
else
{
temp = a[i-1];
a[i-1] = a[pivot];
a[pivot] = temp;
quickSort(a,i - 1);
quickSort(a+i,len - i);
}
}
快排 c
最新推荐文章于 2024-06-18 16:17:50 发布