135 lines
3.6 kB
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* sort_turk.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: ohaponiu <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/05/01 19:03:09 by ohaponiu #+# #+# */
9/* Updated: 2026/05/04 12:51:33 by ohaponiu ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "operations.h"
14#include "stack.h"
15#include "strategies.h"
16#include <limits.h>
17
18/* find position in B of largest element < val, or max if none */
19static int find_target_b(t_stack *b, int val)
20{
21 int i;
22 int best_pos;
23 int best_val;
24 int max_pos;
25
26 best_pos = 0;
27 best_val = -1;
28 max_pos = 0;
29 i = -1;
30 while (++i < stack_size(b))
31 {
32 if (stack_at(b, i) > stack_at(b, max_pos))
33 max_pos = i;
34 if (stack_at(b, i) < val && (best_val < 0 || stack_at(b, i) > best_val))
35 {
36 best_val = stack_at(b, i);
37 best_pos = i;
38 }
39 }
40 if (best_val >= 0)
41 return (best_pos);
42 return (max_pos);
43}
44
45/* compute combined rotation cost to bring positions pa and pb to top */
46static int move_cost(int pa, int sa, int pb, int sb)
47{
48 int ca;
49 int cb;
50
51 ca = pa;
52 if (pa > sa / 2)
53 ca = sa - pa;
54 cb = pb;
55 if (pb > sb / 2)
56 cb = sb - pb;
57 if ((pa <= sa / 2) == (pb <= sb / 2))
58 return ((ca > cb) * ca + (ca <= cb) * cb);
59 return (ca + cb);
60}
61
62/* execute combined rotations, using rr/rrr when directions align */
63static void apply_rots(t_stack *a, t_stack *b, int best_pos[2], t_ops *ops)
64{
65 if (best_pos[0] > stack_size(a) / 2)
66 best_pos[0] -= stack_size(a);
67 if (best_pos[1] > stack_size(b) / 2)
68 best_pos[1] -= stack_size(b);
69 while (best_pos[0] > 0 && best_pos[1] > 0)
70 {
71 op_rr(a, b, ops);
72 best_pos[0]--;
73 best_pos[1]--;
74 }
75 while (best_pos[0] < 0 && best_pos[1] < 0)
76 {
77 op_rrr(a, b, ops);
78 best_pos[0]++;
79 best_pos[1]++;
80 }
81 while (best_pos[0] > 0 && best_pos[0]--)
82 op_ra(a, ops);
83 while (best_pos[0] < 0 && best_pos[0]++)
84 op_rra(a, ops);
85 while (best_pos[1] > 0 && best_pos[1]--)
86 op_rb(b, ops);
87 while (best_pos[1] < 0 && best_pos[1]++)
88 op_rrb(b, ops);
89}
90
91/* find element in A with cheapest move cost to B, rotate both, push */
92static void push_cheapest(t_stack *a, t_stack *b, t_ops *ops)
93{
94 int pos_a;
95 int best;
96 int best_pos[2];
97 int pos_b;
98 int cost;
99
100 best = INT_MAX;
101 pos_a = -1;
102 while (++pos_a < stack_size(a))
103 {
104 pos_b = find_target_b(b, stack_at(a, pos_a));
105 cost = move_cost(pos_a, stack_size(a), pos_b, stack_size(b));
106 if (cost < best)
107 {
108 best = cost;
109 best_pos[0] = pos_a;
110 best_pos[1] = pos_b;
111 }
112 }
113 apply_rots(a, b, best_pos, ops);
114 op_pb(a, b, ops);
115}
116
117/* turk sort: greedily push cheapest to B, sort_three remainder, insert back */
118void sort_turk(t_stack *a, t_stack *b, t_ops *ops)
119{
120 if (stack_is_sorted(a))
121 return ;
122 if (stack_size(a) <= 5)
123 return (sort_five(a, b, ops));
124 op_pb(a, b, ops);
125 op_pb(a, b, ops);
126 while (stack_size(a) > 3)
127 push_cheapest(a, b, ops);
128 sort_three(a, ops);
129 while (!stack_is_empty(b))
130 {
131 rotate_a_to_top(a, find_insert_pos(a, stack_at(b, 0)), ops);
132 op_pa(a, b, ops);
133 }
134 rotate_a_to_top(a, find_extremum_pos(a, 0), ops);
135}