-
Notifications
You must be signed in to change notification settings - Fork 1
/
q1.c
32 lines (26 loc) · 811 Bytes
/
q1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdint.h>
#include "list.h"
#include "list_sort.h"
#include "q1q2.h"
void q1_sort(void *priv, struct list_head *head, list_cmp_func_t cmp)
{
if (list_empty(head) || list_is_singular(head))
return;
struct list_head list_less, list_greater;
INIT_LIST_HEAD(&list_less);
INIT_LIST_HEAD(&list_greater);
struct list_head *pivot = head->next;
list_del(pivot);
struct list_head *itm = NULL, *is = NULL;
list_for_each_safe (itm, is, head) {
if (cmp(NULL, itm, pivot) < 0)
list_move(itm, &list_less);
else
list_move(itm, &list_greater);
}
q1_sort(NULL, &list_less, cmp);
q1_sort(NULL, &list_greater, cmp);
list_add(pivot, head);
list_splice(&list_less, head);
list_splice_tail(&list_greater, head);
}