Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update readme #45

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CXXFLAGS = -std=c++17 -Wall -Wextra -Werror
PROD_FLAGS = -O3
DEV_FLAGS = -O0 -g -fsanitize=address,integer,undefined -D DEV
LEAK_FLAGS = -D LEAK
DEPFLAGS = -MMD -MP
DEP_FLAGS = -MMD -MP
INCLUDE = -I $(INC_DIR)

# flag options
Expand Down Expand Up @@ -156,9 +156,11 @@ _checker_build: $(BONUS_OBJ)
# util
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(@D)
@$(CC) $(CFLAGS) $(INCLUDE) $(DEPFLAGS) -c $< -o $@
@$(CC) $(CFLAGS) $(INCLUDE) $(DEP_FLAGS) -c $< -o $@
@printf "$(GREEN)─$(RESET)"

-include $(DEP)

$(LIBFT): $(LIBFT_DIR)
@make -C $(@D)

Expand Down Expand Up @@ -226,5 +228,3 @@ $(GTEST_DIR):
.PHONY: norm
norm:
norminette $(INC_DIR) $(SRC_DIR) $(LIBFT_DIR)

-include $(DEP)
28 changes: 28 additions & 0 deletions README-ja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
### アルゴリズム
2段階に分けてソートを行う。
1. stack_aからstack_bへ要素の大きさに従ってセグメントに分けをしながらpushする
2. stack_bからstack_aへセグメントが大きいいものから貪欲的にpushしてもどす

1回目にざっくりとソートして、2回目にきっちりソートを行う。

#### 1.セグメントに分けてstack_bへpushする
以下のテーブルに示すようにセグメント数を決定する。

| スタックの要素数 | セグメント数 |
| ---- | ---- |
| ~ 99 | 1 |
| 100 ~ 249 | 3 |
| 250 ~ 499 | 5 |
| 500 ~ 749 | 7 |
| ... | ... |

セグメントは大まかなグループわけであり、スタック要素の番号が小さければセグメント番号も小さい。

この過程ではstack_aをrotateしながら、セグメント番号が小さな要素から順番にstack_bへpushを行う。

#### 2.貪欲的にstack_aへpushする
stack_bのうち、セグメント番号が大きな要素から順番にpushして戻す。
この時、stack_aが常に広義のソート状態を保つようにする。
広義のソート状態とは、stackのtopとbottomを繋げて円環とみなしたときに、最小値と最大値が隣り合っていて、その他の要素はソートされている状態をさす。
セグメント番号が同一な要素が複数ある場合は、広義のソート状態を保ちながらpushするのに必要な手数が最小な要素を選んでpushを行う。
この時、必要な手数が最小であるとは各要素に対してpushの仕方4通りを全て計算し、かつ全ての要素のなかで最小な手数のことである。
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
<h1 align="center">Push Swap</h1>
<p>This repository contains the implementation of the push_swap project, which focuses on sorting a stack of numbers using a limited set of actions.</p>
<img src="https://img.shields.io/badge/norminette-passing-success"/>
<a href="https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html"><img src="https://img.shields.io/badge/leaks-none-success" /></a>
<img src="https://img.shields.io/badge/bonus-included-success"/>
<img src="https://img.shields.io/badge/norminette-passing-success"/> <img src="https://img.shields.io/badge/leaks-none-success" /> <img src="https://img.shields.io/badge/bonus-included-success"/>

### Algorithm
[日本語版](/README-ja.md)

The sorting is done in two stages:

1. Push from stack_a to stack_b, dividing into segments according to the value of the elements.
2. Greedily push from stack_b back to stack_a, starting with the largest segments

First, a rough sort is performed, followed by a more precise sort in the second stage.

#### 1. Pushing to stack_b in Segments
Determine the number of segments as shown in the table below.

| Number of Elements in Stack | Number of Segments |
| ---- | ---- |
| ~ 99 | 1 |
| 100 ~ 249 | 3 |
| 250 ~ 499 | 5 |
| 500 ~ 749 | 7 |
| ... | ... |

Segments are rough groupings, where smaller element numbers in the stack correspond to smaller segment numbers.

In this process, rotate stack_a and sequentially push elements with smaller segment numbers to stack_b.

#### 2. Greedily Pushing Back to stack_a
From stack_b, push elements back to stack_a, starting with those in the largest segments. Ensure that stack_a always maintains a broadly sorted state during this process. A broadly sorted state means that when the stack's top and bottom are connected to form a ring, the minimum and maximum values are adjacent, and all other elements are sorted.

If there are multiple elements with the same segment number, choose the one that minimizes the number of moves needed to maintain a broadly sorted state and push it. The minimum number of moves refers to calculating all four possible ways of pushing for each element and selecting the one with the smallest number among all elements.
23 changes: 23 additions & 0 deletions include/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/19 16:23:21 by reasuke #+# #+# */
/* Updated: 2024/02/19 16:25:20 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef MAIN_H
# define MAIN_H

# include "push_swap.h"

int check_args(int argc, char **argv);
t_stack *generate_stack(int argc, char **argv);

void sort(t_stack **p_a, t_stack **p_b);

#endif