拍个QuickSort
-
void Swap(int *a, int *b)
-
{
-
int tmp = *a;
-
*a = *b;
-
*b = tmp;
-
}
-
-
int partition(int *a, int p, int r)
-
{
-
int x = a[r];
-
int i = p - 1;
-
int j = p;
-
-
for (; j <= (r - 1); j++)
-
if (a[j] < x)
-
{
-
i++;
-
Swap(&a[i], &a[j]);
-
}
-
Swap(&a[i+1], &a[r]);
-
return i+1;
-
}
-
-
-
void quickSort(int *a, int p, int r)
-
{
-
if (p < r) {
-
int q = partition(a, p, r);
-
quickSort(a, p, q - 1);
-
quickSort(a, q + 1, r);
-
}
-
}
-
int main(int argc, char *argv[])
-
{
-
int a[] = {0, 8, 7, 1, 3, 5, 6, 4};
-
int sorted[8] = {0,1,3,4,5,6,7,8};
-
int ii = 0;
-
-
quickSort(a, 2, 7);
-
for(; ii < 8; ii++)
-
//if(sorted[ii] != a[ii])
-
-
// printf("Success\n");
-
-
// int k1 = 3, k2 = 4;
-
// int a[2] ={0,1};
-
-
-
/* Swap(&a[0], &a[1]); */
-
/* printf("k1=%d, k2=%d", a[0], a[1]); */
-
-
return 0;
-
}
-
-