#include <stdio.h>
int partition(int *A, int p, int r);
void quick_sort(int *A, int p, int r);
int main()
{
int data[] = {2, 8, 7, 1, 3, 5, 6, 4};
quick_sort(data, 0, 7);
int i;
for (i = 0; i < 8; i++)
{
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
//数组的划分
int partition(int *A, int p, int r)
{
int x = A[r];
int i = p - 1;
int tmp;
int j = p;
for (; j < r; j++)
{
if (A[j] <= x)
{
++i;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
tmp = A[r];
A[r] = A[i+1];
A[i+1] = tmp;
return i+1;
}
//快速排序
void quick_sort(int *A, int p, int r)
{
if (p < r)
{
int q = partition(A, p, r);
quick_sort(A, p, q-1);
quick_sort(A, q+1, r);
}
}
int partition(int *A, int p, int r);
void quick_sort(int *A, int p, int r);
int main()
{
int data[] = {2, 8, 7, 1, 3, 5, 6, 4};
quick_sort(data, 0, 7);
int i;
for (i = 0; i < 8; i++)
{
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
//数组的划分
int partition(int *A, int p, int r)
{
int x = A[r];
int i = p - 1;
int tmp;
int j = p;
for (; j < r; j++)
{
if (A[j] <= x)
{
++i;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
tmp = A[r];
A[r] = A[i+1];
A[i+1] = tmp;
return i+1;
}
//快速排序
void quick_sort(int *A, int p, int r)
{
if (p < r)
{
int q = partition(A, p, r);
quick_sort(A, p, q-1);
quick_sort(A, q+1, r);
}
}