自己的第一个快速排序法,终于克服了心里的恐惧
void quicksort(int *head_arg, int *tail_arg)
{
if (head_arg >= tail_arg) return;
/* step1: set flag'value */
int flag = *head_arg;
int *head = head_arg;
int *tail = tail_arg;
int swap;
while (head < tail) {
/* 找大数
* 找到了或检索完毕,均有可能结束循环,下同
*/
for(; head < tail; ++head) {
if (*head > flag) break; //找到了合适的数
if (head + 1 == tail) break; //即将碰面
}
/* 找小数
*/
for(; tail > head; --tail) {
if (*tail <= flag) break;
if (tail - 1 == head) break;
}
if (head + 1 == tail) {
if (*head == flag) {
if (*tail <= flag) {
swap = *head;
*head = *tail;
*tail = swap;
}
} else if (*head < flag) {
if (*tail <= flag) {
swap = *tail;
*tail = flag;
*head_arg = swap;
}
} else if (*head > flag) {
if (*tail <= flag) {
int swap = *head;
*head = *tail;
*tail = swap;
} else if (*tail > flag) {
head -= 1;
tail -= 1;
}
}
break;
} else { //两个checker中间还有数据需比对
//交换a,b之间的数
swap = *head;
*head = *tail;
*tail = swap;
}
}
quicksort(head_arg, head);
quicksort(tail, tail_arg);
}