-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_simple_sort.c
78 lines (72 loc) · 1.8 KB
/
ft_simple_sort.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "ft_push_swap.h"
// 기능: 노드 2개 정렬, 리턴: void
void sort_two_node_a(t_list *stack)
{
if (stack->head->next->data > stack->head->next->next->data)
sa(stack);
}
static void sort_three_node_a2(t_list *stack)
{
// 3 1 2
if (stack->head->next->next->next->data > stack->head->next->next->data &&
stack->head->next->next->next->data < stack->head->next->data)
ra(stack);
// 3 2 1
else if (stack->head->next->next->data > stack->head->next->next->next->data &&
stack->head->next->next->data < stack->head->next->data)
{
sa(stack);
rra(stack);
}
}
// 기능: 노드가 3개 이하 일 때 3개 노드 정렬, 리턴: void
void sort_three_node_a(t_list *stack)
{
if (check_ascending(*stack))
return ;
// 1 3 2
if (stack->head->next->next->next->data > stack->head->next->data &&
stack->head->next->next->next->data < stack->head->next->next->data)
{
sa(stack);
ra(stack);
}
//2 1 3
else if (stack->head->next->data > stack->head->next->next->data &&
stack->head->next->data < stack->head->next->next->next->data)
{
sa(stack);
}
// 2 3 1
else if (stack->head->next->data > stack->head->next->next->next->data &&
stack->head->next->data < stack->head->next->next->data)
rra(stack);
sort_three_node_a2(stack);
}
static void sort_two_node_b(t_list *stack)
{
if (stack->head->next->data < stack->head->next->next->data)
sb(stack);
}
// 기능: 노드가 5개 있을 때 정렬, 리턴: void
void sort_five_node_a(t_list *stack_a, t_list *stack_b, int *sorted_node)
{
int i;
int mid;
i = 0;
mid = sorted_node[stack_a->count / 2];
while (i < 2)
{
if (mid > stack_a->head->next->data)
{
pb(stack_a, stack_b);
i++;
}
else
ra(stack_a);
}
sort_three_node_a(stack_a);
sort_two_node_b(stack_b);
pa(stack_a, stack_b);
pa(stack_a, stack_b);
}