Skip to content

Commit

Permalink
implement large sort
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Jan 22, 2024
1 parent 9d79a65 commit 0b12b9e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort/sort.c \
$(SRC_DIR)/sort/micro_sort.c \
$(SRC_DIR)/sort/nano_sort.c \
$(SRC_DIR)/sort/large_sort.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
Expand Down
3 changes: 2 additions & 1 deletion include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */
/* Updated: 2024/01/17 18:29:53 by reasuke ### ########.fr */
/* Updated: 2024/01/22 16:55:46 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,7 @@ t_list *generate_stack(int argc, char **argv);
void sort(t_list **stack_a, t_list **stack_b, int num_a);
void nano_sort(t_list **stack, int num_a);
void micro_sort(t_list **stack_a, t_list **stack_b, int num_a);
void large_sort(t_list **stack_a, t_list **stack_b, int num_a);

void operate_sa(t_list **stack_a);
void operate_sb(t_list **stack_b);
Expand Down
54 changes: 54 additions & 0 deletions src/sort/large_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* large_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/01/22 17:02:49 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

// naive implement: insertion sort
// find smallest number of a
// rotate
// push to stack_b
// push to stack_a

static void push_in_order(t_list **stack_a, t_list **stack_b, int num_a)
{
int i;

i = 1;
while (i <= num_a)
{
if (first_content(stack_a) == i)
{
operate_pb(stack_a, stack_b);
i++;
}
else
operate_ra(stack_a);
}
}

static void push_revert(t_list **stack_a, t_list **stack_b, int num_a)
{
int i;

i = 0;
while (i < num_a)
{
operate_pa(stack_b, stack_a);
i++;
}
}

void large_sort(t_list **stack_a, t_list **stack_b, int num_a)
{
push_in_order(stack_a, stack_b, num_a);
push_revert(stack_a, stack_b, num_a);
}
4 changes: 3 additions & 1 deletion src/sort/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:10:35 by reasuke #+# #+# */
/* Updated: 2024/01/17 16:08:05 by reasuke ### ########.fr */
/* Updated: 2024/01/22 16:54:56 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,4 +18,6 @@ void sort(t_list **stack_a, t_list **stack_b, int num_a)
nano_sort(stack_a, num_a);
else if (num_a <= 5)
micro_sort(stack_a, stack_b, num_a);
else
large_sort(stack_a, stack_b, num_a);
}
78 changes: 77 additions & 1 deletion test/test_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2024, reasuke

#include <vector>
#include <algorithm>
#include <random>
#include <vector>

#include "gtest/gtest.h"

Expand Down Expand Up @@ -57,6 +58,49 @@ static void sort_test(int N) {
close(stdout_copy);
}

static void random_sort_test_main(int N) {
std::vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i + 1;
}
// shuffle vector
std::cout << std::endl;
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
std::shuffle(v.begin(), v.end(), engine);
// for (int i = 0; i < N; ++i) {
// std::cout << v[i] << ' ';
// }
// std::cout << '\n';
t_list *stack_a = NULL;
t_list *stack_b = NULL;
for (int &i : v) {
ft_lstadd_back(&stack_a, ft_lstnew(new int(i)));
}
// sort
sort(&stack_a, &stack_b, N);
// check if the order is appropreate
for (int i = 1; stack_a; ++i, stack_a = stack_a->next) {
EXPECT_EQ(*(int *)stack_a->content, i);
}
}

static void random_sort_test(int N) {
// save stdout
int stdout_copy = dup(STDOUT_FILENO);
// redirect stdout to /dev/null
int dev_null = open("/dev/null", O_WRONLY);
dup2(dev_null, 1);
close(dev_null);

// execute test
random_sort_test_main(N);

// revert stdout
dup2(stdout_copy, STDOUT_FILENO);
close(stdout_copy);
}

TEST(sort, oneElement) {
sort_test(1);
}
Expand All @@ -76,3 +120,35 @@ TEST(sort, fourElements) {
TEST(sort, fiveElements) {
sort_test(5);
}

TEST(sort, sixElements) {
sort_test(6);
}

TEST(sort, sevenElements) {
sort_test(7);
}

TEST(sort, eightElementsRandom) {
random_sort_test(8);
}

TEST(sort, nineElementsRandom) {
random_sort_test(9);
}

TEST(sort, tenElementsRandom) {
random_sort_test(10);
}

TEST(sort, hundredElementsRandom) {
random_sort_test(100);
}

TEST(sort, twoHundredElementsRandom) {
random_sort_test(200);
}

TEST(sort, thousandElementsRandom) {
random_sort_test(1000);
}

0 comments on commit 0b12b9e

Please sign in to comment.