fix sort 3 and start sortmore
This commit is contained in:
parent
816b02eac1
commit
f28600d8a6
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -6,7 +6,7 @@
|
|||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/push_swap",
|
"program": "${workspaceFolder}/push_swap",
|
||||||
"args": ["20", "86", "63", "13", "84"],
|
"args": ["89", "35", "82", "97", "46"],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"environment": [],
|
"environment": [],
|
||||||
|
|||||||
@ -70,6 +70,7 @@ int main(int argc, char *argv[])
|
|||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
print_state(state);
|
||||||
sort5(state);
|
sort5(state);
|
||||||
print_state(state);
|
print_state(state);
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -35,7 +35,7 @@ int sort3(t_state *state)
|
|||||||
rra(state);
|
rra(state);
|
||||||
sa(state);
|
sa(state);
|
||||||
}
|
}
|
||||||
else if (!(a > c && b < c))
|
else if ((a > b && a < c) || (a > b && b > c))
|
||||||
sa(state);
|
sa(state);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,8 +36,12 @@ int sort5(t_state *state)
|
|||||||
if (*(int *)state->b->content > *(int *)state->b->next->content)
|
if (*(int *)state->b->content > *(int *)state->b->next->content)
|
||||||
sb(state);
|
sb(state);
|
||||||
sort3(state);
|
sort3(state);
|
||||||
|
print_state(state);
|
||||||
merge_to_a(state);
|
merge_to_a(state);
|
||||||
|
print_state(state);
|
||||||
merge_to_a(state);
|
merge_to_a(state);
|
||||||
|
print_state(state);
|
||||||
rotate_a_to_top(state);
|
rotate_a_to_top(state);
|
||||||
|
print_state(state);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|||||||
64
src/sort/sortmore.c
Normal file
64
src/sort/sortmore.c
Normal 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||||
/* Created: 2024/11/10 14:44:51 by whaffman #+#+# #+#+# #+# #+# | */
|
/* 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);
|
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)
|
int ft_min(int a, int b)
|
||||||
{
|
{
|
||||||
@ -83,7 +96,7 @@ int count_gt(int n, t_list *stack)
|
|||||||
count = 0;
|
count = 0;
|
||||||
while (stack)
|
while (stack)
|
||||||
{
|
{
|
||||||
if (*(int *) stack->content > n)
|
if (*(int *) stack->content < n)
|
||||||
count++;
|
count++;
|
||||||
stack = stack->next;
|
stack = stack->next;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user