forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_ins.h
48 lines (44 loc) · 1.27 KB
/
sort_ins.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void q_sort(struct list_head *head)
{
/* insertion sort */
/* Remove every node which has a node with a strictly greater value anywhere
* to the right side of it */
/*https://npes87184.github.io/2015-09-12-linkedListSort/ */
if (!head)
return;
struct list_head *temp = head;
int size = 0;
while (temp) {
size++;
temp = temp->next;
}
struct list_head *curr = head->next;
struct list_head *prev = head;
struct list_head *tail = head;
for (int i = 1; i < size; i++) {
temp = head;
prev = head;
// find inserting location
element_t *t_nod = list_entry(temp, element_t, list),
*c_nod = list_entry(curr, element_t, list);
for (int j = 0; j < i && *t_nod->value < *c_nod->value; j++) {
temp = temp->next;
if (j != 0)
prev = prev->next;
}
// insert
if (temp == head) {
tail->next = curr->next;
curr->next = head;
head = curr;
} else if (temp == curr) {
tail = tail->next;
} else {
prev->next = curr;
tail->next = curr->next;
curr->next = temp;
}
curr = tail->next;
}
return;
}