Quick_sort

时间:2022-04-13 04:50:19
typedef int ElementType;

void Quick_sort(ElementType A[], int N)
{
Quicksort(A, , N-);
} void Quicksort(ElementType A[], int left, int right)
{
if(right-left+ < )
{
//插入排序
Insert_sort(A, right-left+);
}
else
{
//调用快排
//找中位数,优化快排
pivot = Median3(A, left, right); int i = left;
int j = right; while(true)
{
while(A[++i] < pivot);
while(A[--j] > pivot);
if(i < j)
swap(&A[i], &A[j]);
else
break;
}
swap(&A[i], &A[right-]); //递归左右
Quicksort(A, left, i-);
Quicksort(A, i+, right);
}
} void Insert_sort(ElementType A[], int N)
{
for(int i = ; i < N; i++)
{
int j;
int Temp = A[i];
for(j = i-; j >= && A[j] > Temp; j--)
{
A[j] = A[j+];
}
A[j] = Temp;
}
}