int partition(int array[], int start, int end)
{
int i, j, tmp;
int key = array[start];
i = start;
for (j = start + 1; j <= end; ++j) {
if (array[j] <= key && ++i != j) {
// array[j] <=> array[i]
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
array[start] = array[i];
array[i] = key;
return i;
}
void printf_array(int array[], int from, int len)
{
int i;
for (i = 0; i < len; ++i)
printf("%d,", array[from + i]);
printf("\n");
}
int main(int argc, char** argv)
{
int array[10] = {3, 7, 21, 6, 2, -3, 90, 23443, -3000, 5};
printf("partition=%d\n", partition(array, 0, 9));
printf_array(array, 0, 10);
return 0;
}
2011年6月2日星期四
partition algorithm
订阅:
博文 (Atom)