]> git.dkaiser.de - 42/push_swap.git/commitdiff
Change sorting direction (still not fully working)
authorDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 17 Apr 2024 12:19:37 +0000 (14:19 +0200)
committerDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 17 Apr 2024 12:19:37 +0000 (14:19 +0200)
Somehow the 0 is still not sorted and in some cases 1 as well.
TODO: Fix this

sorting.c

index c4d3644fd76350fabd5b5aab4fa384c31351efa3..dfaafe222753d1faef8e64c8ce8c8edfed91ec6f 100644 (file)
--- a/sorting.c
+++ b/sorting.c
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/13 15:04:19 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/17 12:20:26 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/17 14:17:55 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -23,10 +23,10 @@ static void presort(t_psdata *data)
     max = data->a->stack[0];
     while (size--)
     {
-        if (data->a->stack[0] < max)
+        if (data->a->stack[0] > max)
         {
             run_command(data, PB);
-            if (data->b->stack[0] > pivot)
+            if (data->b->stack[0] < pivot)
                 run_command(data, RB);
         }
         else
@@ -48,7 +48,7 @@ static int calculate_score(t_psdata *data, int pos)
     else
         moves_to_top = data->b->size - pos;
     i = 0;
-    while (i < data->a->size && data->a->stack[i] < data->b->stack[pos])
+    while (i < data->a->size && data->a->stack[i] > data->b->stack[pos])
         i++;
     if (i < (data->a->size + 1) / 2)
         moves_to_spot = 2 * i + 1;
@@ -63,7 +63,7 @@ static void move_to_top(t_psdata *data, int idx)
 {
     if (idx < (data->b->size + 1) / 2)
     {
-        while (idx--)
+        while (--idx > 0)
             run_command(data, RB);
     }
     else
@@ -74,19 +74,26 @@ static void move_to_top(t_psdata *data, int idx)
     }
 }
 
-static void move_to_spot(t_psdata *data)
+static void move_to_spot(t_psdata *data, int idx)
 {
     int pos;
     int i;
 
     pos = 0;
-    while (pos < data->a->size && data->b->stack[0] < data->a->stack[pos])
+    while (pos < data->a->size && data->b->stack[0] > data->a->stack[pos])
         pos++;
-    if (pos < (data->a->size + 1) / 2)
+    if (pos >= data->a->size)
+    {
+        move_to_top(data, idx);
+        run_command(data, PA);
+        run_command(data, RA);
+    }
+    else if (pos < (data->a->size + 1) / 2)
     {
         i = pos;
         while (i--)
             run_command(data, RA);
+        move_to_top(data, idx);
         run_command(data, PA);
         i = pos;
         while (i--)
@@ -97,6 +104,7 @@ static void move_to_spot(t_psdata *data)
         i = data->a->size - pos;
         while (i--)
             run_command(data, RRA);
+        move_to_top(data, idx);
         run_command(data, PA);
         i = data->a->size - pos;
         while (i--)
@@ -126,8 +134,7 @@ static void scoresort(t_psdata *data)
             min_score = i;
         i++;
     }
-    move_to_top(data, min_score);
-    move_to_spot(data);
+    move_to_spot(data, min_score);
 
     free(scores);
     if (data->b->size > 0)