aboutsummaryrefslogtreecommitdiff
path: root/src/push_swap
diff options
context:
space:
mode:
Diffstat (limited to 'src/push_swap')
-rw-r--r--src/push_swap/main.c8
-rw-r--r--src/push_swap/sort.c24
2 files changed, 21 insertions, 11 deletions
diff --git a/src/push_swap/main.c b/src/push_swap/main.c
index fa34b35..0d25ace 100644
--- a/src/push_swap/main.c
+++ b/src/push_swap/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 09:09:59 by cacharle #+# #+# */
-/* Updated: 2020/01/22 10:36:31 by cacharle ### ########.fr */
+/* Updated: 2021/09/09 10:02:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,11 +19,13 @@ int main(int argc, char **argv)
if (argc == 1)
return (0);
- if ((a = stack_new(argc - 1)) == NULL)
+ a = stack_new(argc - 1);
+ if (a == NULL)
return (1);
if (parse(argc, argv, a) != STATUS_SUCCESS)
return (1);
- if ((b = stack_new(stack_length(a))) == NULL)
+ b = stack_new(stack_length(a));
+ if (b == NULL)
{
stack_destroy(a);
return (1);
diff --git a/src/push_swap/sort.c b/src/push_swap/sort.c
index b6113e9..588dc54 100644
--- a/src/push_swap/sort.c
+++ b/src/push_swap/sort.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 09:12:02 by cacharle #+# #+# */
-/* Updated: 2020/07/21 11:43:00 by charles ### ########.fr */
+/* Updated: 2021/09/10 10:48:01 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,6 +47,14 @@ static int frame_length(t_stack *st, int frame_index)
/* stack_swap_print(main); */
/* } */
+static int not_a_ternary(bool cond, int x, int y)
+{
+ if (cond)
+ return (x);
+ else
+ return (y);
+}
+
/*
** main : stack to sort
** tmp : temporary stack used to store pivot > values
@@ -70,7 +78,7 @@ static void push_swap_qsort_partition(t_stack *main, t_stack *tmp,
int main_frame)
{
int pivot;
- int hidden_count;
+ int hidden_count;
int frame_len;
pivot = stack_peek(main);
@@ -79,8 +87,8 @@ static void push_swap_qsort_partition(t_stack *main, t_stack *tmp,
frame_len = frame_length(main, main_frame) - 1;
while (frame_len > 0)
{
- if (main->tag == STACK_A ?
- (stack_peek(main) < pivot) : (stack_peek(main) > pivot))
+ if (not_a_ternary(main->tag == STACK_A,
+ stack_peek(main) < pivot, stack_peek(main) > pivot))
stack_push_to_print(main, tmp);
else
{
@@ -118,9 +126,9 @@ static void push_swap_qsort_rec(t_stack *main, t_stack *tmp,
return ;
if (frame_length(main, main_frame) == 2)
{
- if (main->tag == STACK_A ?
- main->elements[main->top] > main->elements[main->top - 1]
- : main->elements[main->top] < main->elements[main->top - 1])
+ if (not_a_ternary(main->tag == STACK_A,
+ main->elements[main->top] > main->elements[main->top - 1],
+ main->elements[main->top] < main->elements[main->top - 1]))
stack_swap_print(main);
return ;
}
@@ -140,7 +148,7 @@ static void push_swap_qsort_rec(t_stack *main, t_stack *tmp,
** wrapper for push_swap_qsort_rec
*/
-void push_swap_qsort(t_stack *a, t_stack *b)
+void push_swap_qsort(t_stack *a, t_stack *b)
{
push_swap_qsort_rec(a, b, 0, 0);
}