fix sort 3 and start sortmore

This commit is contained in:
whaffman 2024-11-17 18:14:53 +01:00
parent 816b02eac1
commit f28600d8a6
6 changed files with 86 additions and 4 deletions

2
.vscode/launch.json vendored
View File

@ -6,7 +6,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/push_swap",
"args": ["20", "86", "63", "13", "84"],
"args": ["89", "35", "82", "97", "46"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],

View File

@ -70,6 +70,7 @@ int main(int argc, char *argv[])
return (1);
}
}
print_state(state);
sort5(state);
print_state(state);
return (0);

View File

@ -35,7 +35,7 @@ int sort3(t_state *state)
rra(state);
sa(state);
}
else if (!(a > c && b < c))
else if ((a > b && a < c) || (a > b && b > c))
sa(state);
return (1);
}

View File

@ -36,8 +36,12 @@ int sort5(t_state *state)
if (*(int *)state->b->content > *(int *)state->b->next->content)
sb(state);
sort3(state);
print_state(state);
merge_to_a(state);
print_state(state);
merge_to_a(state);
print_state(state);
rotate_a_to_top(state);
print_state(state);
return (1);
}

64
src/sort/sortmore.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* sortmore.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/17 17:25:40 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/17 17:39:12 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <limits.h>
int count_moves_merge_a(int idx_b, t_state *state)
{
const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
const int idx_a = put_at_index_asc(number_b, state->a);
const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a));
const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b));
if (shortest_a * shortest_b > 0)
return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1);
return (ft_abs(shortest_a + shortest_b) + 1);
}
int best_merge_a(t_state *state)
{
const int size = ft_lstsize(state->b);
int moves;
int best_b;
int min_moves;
int i;
min_moves = INT_MAX;
best_b = 0;
i = 0;
while (i < size)
{
moves = count_moves_merge_a(i, state);
if (moves < min_moves)
{
min_moves = moves;
best_b = i;
}
i++;
}
return (best_b);
}
void b_merge_a(int idx_b, t_state *state)
{
rotate_b_to(idx_b, state);
merge_to_a(state);
}
void sortmore(t_state *state)
{
while (state->b)
b_merge_a(best_merge_a(state))
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/10 14:44:51 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/10 16:35:59 by whaffman ### ### ### ### / \ */
/* Updated: 2024/11/17 17:41:19 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -68,6 +68,19 @@ int index_of(int number, t_list *stack)
}
return (-1);
}
int ft_abs(int a)
{
if (a >= 0)
return (a);
return (-a);
}
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}
int ft_min(int a, int b)
{
@ -83,7 +96,7 @@ int count_gt(int n, t_list *stack)
count = 0;
while (stack)
{
if (*(int *) stack->content > n)
if (*(int *) stack->content < n)
count++;
stack = stack->next;
}