一、数组的快速排序
二、单链表的快速排序
1.遍历交换数据
void quicksort(Linklist head, Linklist end){
if(head == NULL || head == end) //如果头指针为空或者链表为空,直接返回
return ;
int t;
//1.将head作为作为枢轴,small用来记录比枢轴小的值
Linklist p = head -> next; //用来遍历的指针
Linklist small = head;
while( p != end){
if( p -> data < head -> data){ //对于小于轴的元素放在左边
small = small -> next;
t = small -> data;
small -> data = p -> data;
p -> data = t;
}
p = p -> next;
}
//2、将枢轴与最后一个比枢轴小的数交换,这样枢轴左边的数都比枢轴小了
t = head -> data; //遍历完后,对左轴元素与small指向的元素交换
head -> data = small -> data;
small -> data = t;
quicksort(head, small); //对左右进行递归
quicksort(small -> next, end);
}
2、应该可以交换节点