-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.c
120 lines (111 loc) · 2.44 KB
/
sorting.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dfurneau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 21:34:54 by dfurneau #+# #+# */
/* Updated: 2021/12/11 06:05:54 by dfurneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
/**
* Algo for finishing sort
* on > 100
**/
static void finish_sort(t_ops *ops)
{
int i;
if (is_sorted(ops->a))
return ;
free_sort(ops);
ops->sort = sort_array(ops->a);
if (!ops->sort)
error(ops->a, ops->b);
i = 0;
while (ops->a->stack[i] != ops->sort[0] && i < ops->a->len - 1)
i++;
while (ops->a->stack[ops->a->top] != ops->sort[0])
{
if (i <= ops->a->len / 2)
rra(ops->a);
else
ra(ops->a);
}
}
/**
* Algo for sorting 2 to 3 ints
**/
void sort3(t_stack *a)
{
if (a->stack[a->top] > a->stack[a->top - 1])
{
if (a->stack[a->top - 1] > a->stack[0])
{
sa(a);
rra(a);
}
else
{
if (a->stack[0] > a->stack[a->top])
sa(a);
else
ra(a);
}
return ;
}
if (a->stack[0] < a->stack[a->top])
rra(a);
else
{
sa(a);
ra(a);
}
}
/**
* Algo for sorting 4 to 5 ints
**/
void sort5(t_stack *a, t_stack *b)
{
rotate_num_top(a, min_search_stack);
pb(a, b);
rotate_num_top(a, min_search_stack);
pb(a, b);
if (!is_sorted(a))
sort3(a);
pa(a, b);
pa(a, b);
}
/**
* Algo for sorting 100 ints
**/
void sort100(t_stack *a, t_stack *b)
{
t_ops ops;
int i;
push_all_chunk(a, b, CHUNK_SIZE_100);
sort5(a, b);
init_ops(&ops, a, b);
i = b->top;
while (i-- > -1)
execute_least_ops(&ops);
finish_sort(&ops);
free_ops(&ops);
}
/**
* Algo for sorting 500 ints
**/
void sort500(t_stack *a, t_stack *b)
{
t_ops ops;
int i;
push_all_chunk(a, b, CHUNK_SIZE_500);
sort5(a, b);
init_ops(&ops, a, b);
i = b->top;
while (i-- > -1)
execute_least_ops(&ops);
finish_sort(&ops);
free_ops(&ops);
}