-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsort_3_and_5_element.c
101 lines (92 loc) · 2.41 KB
/
sort_3_and_5_element.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_3_and_5_element.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: doberyn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/02 19:52:00 by doberyn #+# #+# */
/* Updated: 2020/01/02 19:52:01 by doberyn ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*
** ft_sort_3_element - алгоритм для сортировки 3-х
** и 2-х чисел.
** ༺༻
** Состояние: ✓
** Нормы: ✓
** ༺༻
*/
void ft_sort_3_element(t_stacks *stacks)
{
int max;
max = ft_max(stacks->a);
if (stacks->count_a == 1)
return ;
else if (stacks->count_a == 2)
{
if (stacks->a->data > stacks->a->next->data)
ft_sa(stacks->a, 1);
return ;
}
else if (stacks->count_a == 3)
{
if (stacks->a->data == max)
ft_ra(&stacks->a, 1);
if (stacks->a->next->data == max)
ft_rra(&stacks->a, 1);
if (stacks->a->data > stacks->a->next->data)
ft_sa(stacks->a, 1);
}
}
/*
** ft_sort_5_element - алгоритм для сортировки 5-ти
** и 4-х чисел.
** ༺༻
** Состояние: ✓
** Нормы: ✓
** ༺༻
*/
void ft_sort_5_element(t_stacks *stacks)
{
while (stacks->count_b < 2)
{
if (stacks->a->data == stacks->min || stacks->a->data == stacks->max)
ft_pb(stacks, 1);
else
ft_ra(&stacks->a, 1);
}
ft_sort_3_element(stacks);
ft_pa(stacks, 1);
ft_pa(stacks, 1);
if (stacks->a->data == stacks->max)
{
ft_ra(&stacks->a, 1);
}
else
{
ft_sa(stacks->a, 1);
ft_ra(&stacks->a, 1);
}
}
/*
** ft_max - функция возвращает максимальное значение
** из стека.
** ༺༻
** Состояние: ✓
** Нормы: ✓
** ༺༻
*/
int ft_max(t_stack *s)
{
int max;
max = s->data;
while (s)
{
if (s->data > max)
max = s->data;
s = s->next;
}
return (max);
}