forked from embedded2016/unit-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.c
49 lines (40 loc) · 1.23 KB
/
bubble.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* FIXME : introduce elegant ways to reuse */
#include "swap.c"
void bubble_sort(List **head)
{
if (head == NULL || (*head == NULL))
return;
int num_list = 0;
int i = 0;
int sub_i;
int sub_for_times;
int sub_for_Max;
List **pre_sub_head = head;
List *sub_head;
sub_head = *head;
for (; sub_head && sub_head->next;) {
if (sub_head->value < sub_head->next->value) {
sub_head = swap(sub_head,sub_head,sub_head->next);
*pre_sub_head = sub_head;
}
num_list = num_list + 1;
pre_sub_head = &((*pre_sub_head)->next);
sub_head = sub_head->next;
}
sub_for_times = num_list - 1;
for (*head && (*head)->next; i < sub_for_times; i++) {
sub_head = *head;
sub_for_Max = num_list - (i + 1);
pre_sub_head = head;
for (sub_i = 0; sub_head && sub_head->next && (sub_i < sub_for_Max);
sub_i++) {
if (sub_head->value < sub_head->next->value) {
sub_head = swap(sub_head,sub_head,sub_head->next);
*pre_sub_head = sub_head;
}
pre_sub_head = &((*pre_sub_head)->next);
sub_head = sub_head->next;
}
}
return;
}