#include <stdio.h>
#include <stdlib.h>
void* quickSort(void * p, size_t width,
int (* cmp)(void*, void*), void (* swap)(void*, void*),
int start, int end);
//----------------------------------------------------------------
int myCmp(int (*a) [2], int (*b) [2]);
void mySwap(int (*a) [2], int (*b) [2]);
void showMyArray(int (*a) [2], int n);
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
int a[5][2] = {{4,1},{6,8},{3,2},{1,5},{10,3}};
quickSort(a, sizeof(int [2]), (void*)myCmp, (void*)mySwap, 0, 4);
showMyArray(a, 5);
return 0;
}
//------------------------------------------------------------------------------
// Function //
//----------//
void* quickSort(void * p, size_t width,
int (* cmp)(void*, void*), void (* swap)(void*, void*),
int start, int end)
{
if (start >= end)
return p;
int i = start, j = end - 1;
do {
while (cmp(p+i*width, p+end*width) < 0) ++i;
while (cmp(p+j*width, p+end*width) >= 0 && j >= start) --j;
if (i < j) swap(p+i*width, p+j*width);
else if (i ^ end) swap(p+i*width, p+end*width);
} while (i < j);
quickSort(p, width, cmp, swap, start, i - 1);
quickSort(p, width, cmp, swap, i + 1, end);
return p;
}
int myCmp(int (*a) [2], int (*b) [2])
{
if (a[0][0] < b[0][0])
return -1;
else if (a[0][0] > b[0][0])
return 1;
return 0;
}
void mySwap(int (*a) [2], int (*b) [2])
{
int temp = a[0][0];
a[0][0] = b[0][0];
b[0][0] = temp;
}
void showMyArray(int (*a) [2], int n)
{
int i = 0;
while (i < n)
{
printf("%d %d\n", a[i][0], a[i][1]);
++i;
}
}
#include <stdlib.h>
void* quickSort(void * p, size_t width,
int (* cmp)(void*, void*), void (* swap)(void*, void*),
int start, int end);
//----------------------------------------------------------------
int myCmp(int (*a) [2], int (*b) [2]);
void mySwap(int (*a) [2], int (*b) [2]);
void showMyArray(int (*a) [2], int n);
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
int a[5][2] = {{4,1},{6,8},{3,2},{1,5},{10,3}};
quickSort(a, sizeof(int [2]), (void*)myCmp, (void*)mySwap, 0, 4);
showMyArray(a, 5);
return 0;
}
//------------------------------------------------------------------------------
// Function //
//----------//
void* quickSort(void * p, size_t width,
int (* cmp)(void*, void*), void (* swap)(void*, void*),
int start, int end)
{
if (start >= end)
return p;
int i = start, j = end - 1;
do {
while (cmp(p+i*width, p+end*width) < 0) ++i;
while (cmp(p+j*width, p+end*width) >= 0 && j >= start) --j;
if (i < j) swap(p+i*width, p+j*width);
else if (i ^ end) swap(p+i*width, p+end*width);
} while (i < j);
quickSort(p, width, cmp, swap, start, i - 1);
quickSort(p, width, cmp, swap, i + 1, end);
return p;
}
int myCmp(int (*a) [2], int (*b) [2])
{
if (a[0][0] < b[0][0])
return -1;
else if (a[0][0] > b[0][0])
return 1;
return 0;
}
void mySwap(int (*a) [2], int (*b) [2])
{
int temp = a[0][0];
a[0][0] = b[0][0];
b[0][0] = temp;
}
void showMyArray(int (*a) [2], int n)
{
int i = 0;
while (i < n)
{
printf("%d %d\n", a[i][0], a[i][1]);
++i;
}
}
—>〉Sun〈<—