-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
68 lines (61 loc) · 2.21 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: susajid <susajid@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 12:16:49 by susajid #+# #+# */
/* Updated: 2024/01/03 16:32:35 by susajid ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void push_swap(t_sorting *sorting);
static void sort_3(t_sorting *sorting);
int main(int argc, char **argv)
{
t_sorting *sorting;
sorting = set_sorting(--argc, &argv[1]);
if (!sorting)
return (1);
push_swap(sorting);
return (clear_sorting(sorting), 0);
}
static void push_swap(t_sorting *sorting)
{
if (!sorting || sorting->total_len < 2 || is_sorted(sorting->stack_a))
return ;
if (sorting->total_len == 2)
return (swap(sorting, 'a', true));
if (!circle_sorted(sorting->stack_a, sorting->len_a))
{
while (sorting->len_a > 3)
{
if (sorting->len_b >= 2)
do_cheap(sorting);
push(sorting, 'b', true);
}
sort_3(sorting);
while (sorting->stack_b)
{
do_move(sorting, find_move(sorting->stack_a, sorting->len_a,
sorting->stack_b->value, false), 0, false);
push(sorting, 'a', true);
}
}
do_move(sorting, find_move(sorting->stack_a, sorting->len_a,
find_min(sorting->stack_a), false), 0, false);
}
static void sort_3(t_sorting *sorting)
{
int max_value;
if (sorting->len_a != 3)
return ;
max_value = find_max(sorting->stack_a);
if (sorting->stack_a->value == max_value)
rotate(sorting, 'a', true);
else if (sorting->stack_a->next->value == max_value)
reverse_rotate(sorting, 'a', true);
if (sorting->stack_a->value > sorting->stack_a->next->value)
swap(sorting, 'a', true);
}