readme

329 lines 16 kB

This project has been created as part of the 42 curriculum by mleitao, ohaponiu.

Table of Contents

Overview

push_swap sorts a stack of integer numbers with limited operations (see Operations).
The strategy is specified with a flag (see Flags). If --adaptive is specified, the algorithm complexity will be chosen based on disorder (see Disorder).

Operations

OperationDescription
sa (swap a)Swap the first two elements at the top of stack a
sb (swap b)Swap the first two elements at the top of stack b
sssa and sb at the same time
pa (push a)Take the first element at the top of b and put it at the top of a
pb (push b)Take the first element at the top of a and put it at the top of b
ra (rotate a)The first element of stack a becomes the last one
rb (rotate b)The first element of stack b becomes the last one
rrra and rb at the same time
rra (reverse rotate a)The last element of stack a becomes the first one
rrb (reverse rotate b)The last element of stack b becomes the first one
rrrrra and rrb at the same time

Flags

FlagDescriptionComplexity
--simpleUses Insertion sort algorithm$O(n^2)$
--mediumUses K-Distribution algorithm (a variation of Chunk sort)$O(n \sqrt{n})$
--complexUses Turk algorithm$O(n \log n)$
--adaptiveUses one of the algorithms above depending on disorder—
--benchPrints benchmark metrics (disorder, strategy, op counts) to stderr—

Disorder

Disorder is a number in the scale of $[0, 1]$. The closer it is to 1, the less sorted the list of numbers is.

DisorderAlgorithm complexity
$[0.0, 0.2)$$O(n^2)$
$[0.2, 0.5)$$O(n \sqrt{n})$
$[0.5, 1.0]$$O(n \log n)$

Instructions

Makefile

CommandDescription
make, make all, make push_swapBuild the push_swap binary
make bonus, make checkerBuild the checker binary
make cleanRemove the build/ directory and call the same command for libft
make fcleanRemove the build/ directory and the two binaries, and call the same command for libft
make reRebuild the push_swap binary (equivalent to make fclean followed by make)
make rebonusRebuilds the checker binary (equivalent to make fclean followed by make bonus)

Usage

  1. Clone the repository:

    git clone --recurse-submodules git@github.com:42lx/push_swap.git && cd push_swap
    
  2. Build it:

    make && make bonus
    
  3. Run it:

    echo '5 4 3 2 1' > args.txt
    
    ./push_swap --bench --adaptive $(<args.txt) > ops.txt 2> bench.txt
    

    Expected ops.txt file contents:

    $ cat ops.txt
    ra
    ra
    ra
    pb
    pb
    ra
    sa
    pa
    pa
    sa
    

    Expected bench.txt file contents:

    $ cat bench.txt
    [bench] disorder:   100.00%
    [bench] strategy:   Adaptive / O(n log n)
    [bench] total_ops:  10
    [bench] sa: 2  sb: 0  ss: 0  pa: 2  pb: 2
    [bench] ra: 4  rb: 0  rr: 0  rra: 0  rrb: 0  rrr: 0
    
  4. Test it:

    ./checker $(<args.txt) < ops.txt
    

    Expected output:

    OK
    

Description

Algorithms

Insertion Sort [1]

Among $O(n^2)$ algorithms, insertion sort is the natural fit for this project. Its cost scales with the number of inversions, so a nearly sorted input is cheap. Selection sort would always scan the full remaining stack; bubble sort requires multiple passes.

K-Distribution Sort [2]

Partition the value range into $k ≈ \sqrt n$ chunks, push each chunk from A to B in one pass, then pull B back to A by repeatedly extracting the maximum. Among chunking strategies, K-sort is the most performant while remaining simple to implement and reason about.

The neat thing is the shape it leaves in B: a K, sometimes called a butterfly. Each chunk gets split on push—upper-half elements land at the top via plain pb, lower-half elements sink to the bottom via pb + rb. Plot position against value in B and you get two lines diverging from a pinch point, hence the K. The max stays near the top, so the pull-back phase rarely rotates deep.

1.4 coefficient was chosen after many benchmarks comparing different coefficients.

Turk Algorithm [3]

Turk sort is the most elegant high-performance algorithm. It always chooses the element whose combined rotation cost with target is lowest. The cost-based calculation pairs perfectly with high-disorder data: when values are randomly scattered, many elements have low-cost moves at any given moment, and the greedy choice reliably finds them. The key saving is that when A and B need to rotate in the same direction, the rr and rrr double-rotation operations move both stacks for the price of one.

Elements in stack A look for targets in stack B (max below). The relationship is surjective (each element has 1 target, but the reverse isn't true).

In this project, we implemented a simple version of Turk algorithm that performs cost calculations only while pushing to B, while reusing insertion logic to push back to A. This is good enough while keeping the code simple and readable.

Graph representing surjection function

Alternatives Considered

Heap sort and Merge sort don't translate well to this project. Both require random access or pointer-based tree structures that have no natural mapping onto two restricted stacks. Simulating them would require far more operations than the purpose-built algorithms above.

Quick sort can be adapted—use pb to partition around a pivot—but the implementation complexity grows quickly: tracking pivot positions across both stacks, ensuring balanced partitions, and handling degenerate cases all add significant code for modest gain over K-sort.

Radix sort processes bits one at a time, which generates a large number of operations regardless of how ordered the input already is. It ignores all existing structure in the data and produces far more operations than needed for typical inputs.

Data structure

There are many viable options when it comes to picking a data structure to handle the stacks. We opted for one that lets us handle it in a circular way—a circular stack—meaning that after the last element comes the first and vice-versa.

The choice

We narrowed down our choice to either ring/circular buffer[4] or doubly circular linked list[5].
Ring buffer, in the context of this project, is much more promising, because of how trivial it is to operate on it—rotate and reverse rotate are simply indexing math—and how much more efficient it is (see Efficiency), so we decided to work with that one. Linked lists provide us with dynamic memory which we don't require, since we know the capacity in advance.

How it works

Differences in our implementation

The ring buffer we've implemented is a bit different than the regular ring buffer.
Instead of keeping track of both head and tail, it keeps track of the head and the buffer's current size. This is because keeping track of tail isn't useful for the scope of this project. Ring buffers are normally used for data streams, where size isn't always known prior to receiving information.

Regular ring buffers are also usually FIFO (First In First Out), but here that concept is abstracted away. The only time an element gets popped out is in the push operation, and in that case it is LIFO (Last In First Out), because we always pop the top most element.

Our head holds a different meaning than the regular ring buffer's head—our head points to the first element, instead of the last written chunk of data in a regular ring buffer.

Implementation
Elements

Our stack is composed of the following elements:

  • buf: contiguous buffer containing all elements
  • head: index in the buffer that keeps track of where to start reading/operating
  • size: amount of elements that can be read/operated in the buffer
  • capacity: maximum capacity of the buffer (number of elements)
Lifecycle functions

The functions that manage the stack lifecycle are:

  • stack_init: initializes the buffer, allocating memory to the previously calculated capacity and initializes size and head to 0 (meaning start reading/operating at the first position and can only read/operate at maximum of 0 elements)
  • stack_build: initializes the stack with the given values and sets size to its maximum value, which is capacity
  • stack_free: frees the stack, points buf to NULL and sets all other stack variables to 0
Operation functions

The magic of a ring buffer is that every operation is based on the position of $head \mod capacity$, which means that head will always be constrained between $[0, capacity-1]$.

The images that follow each operation will be based on the initial state of the image showed below.
This image shows stack A, already populated but unsorted, and stack B, empty. This would be the state of the stacks right before the sorting begins.

Image showing a general ring buffer

Push

stack_push: pushes an element from stack src to stack dst. Pushing, in a ring buffer, simply means moving the head of the source stack forward and decreasing its size by 1. The inverse happens in dst, writing to the buffer in the new head location the value that was read from src.

Image showing the push operation on the buffers

Swap

stack_swap: swaps the first two elements of a stack.

Image showing the swap operation

Rotate

stack_rotate: rotates the stack by moving head to the next element.

Image showing the rotate operation on a buffer

Reverse rotate

stack_rev_rotate: reversely rotates the stack by moving head to the previous element.

Image showing the reverse rotate operation on a buffer

Rotating after pushing

Theoretically, the rotate and reverse rotate would work as described, since the element would get pushed out of the stack. However, since stack_push only virtually removes it, we have to add an extra step to stack_rotate and stack_rev_rotate—we write the first element to the new (post rotate) last position (or new first position, in case of stack_rev_rotate).

Image showing the problem with pushing and rotating, and its fix

Querying the stack

These functions describe the queries we can make to the stack:

  • stack_size: returns the current size of the stack
  • stack_is_empty: returns whether size is 0
  • stack_at: returns the value at the index given based on the head position
  • stack_is_sorted: returns whether stack is sorted

Efficiency

Memory management

Since the size of the stack is known at runtime, using a ring buffer comes in handy because it is only necessary to allocate memory one time (meaning, only one syscall of size n).
On the other hand, the usage of a linked list would imply n syscalls of smaller size, which is way more computationally expensive the bigger n grows.

Memory overhead

Ring buffer has less overhead: three integer variables of integer type (4 bytes in 64-bit systems)—head, size and capacity (12 bytes total)—per stack.
Using a linked list, the overhead would be: two pointers (8 bytes)—prev and next (16 bytes total)—per node.

This means that for 100 numbers, ring buffer would store $1 \times 20=20$ bytes of overhead, while linked list would store $16 \times 100=1600$ bytes of overhead in total, which scales in $O(n)$. That is a very considerable amount of extra memory needed, simply for bookkeeping.

Operation complexity

Both structures have $O(1)$ time complexity for basic operations, but the constant in the ring buffer is much smaller:

  • swap is swapping two values at fixed indexes
  • rotate is incrementing the head
  • push is decrementing the head and copying a value

Using a linked list, this would be:

  • swap is swapping pointers in 4 nodes
  • rotate is updating the head to the next node
  • push is moving one node between two heads, which comes with altering a lot of pointers

Working with a linked list would add a lot of complexity that is easily avoidable.

Error handling

Errors exit via die(int status), which prints Error to stderr and calls exit(status). Exit codes are defined in include/cli.h: ERR_MALLOC (1) for allocation failure, ERR_PARSE (2) for invalid integer input, ERR_FLAG (3) for unknown flags, and ERR_INPUT (4) for general input errors.

Run echo $? immediately after the binary to inspect the exit code and identify the failure cause.

Resources

References

LLM Usage

LLMs were used for:

  • Design decisions
  • Algorithm research
  • Parts of this README
  1. Insertion sort ↩︎

  2. K-Distribution Sort applied to the push-swap problem ↩︎

  3. Push Swap — A journey to find most efficient sorting algorithm ↩︎

  4. Circular buffer ↩︎

  5. Doubly circular linked list ↩︎

  1*This project has been created as part of the 42 curriculum by mleitao, ohaponiu.*
  2
  3Table of Contents
  4=================
  5
  6* [Overview](#overview)
  7   * [Operations](#operations)
  8   * [Flags](#flags)
  9   * [Disorder](#disorder)
 10* [Instructions](#instructions)
 11   * [Makefile](#makefile)
 12   * [Usage](#usage)
 13* [Description](#description)
 14   * [Algorithms](#algorithms)
 15      * [Insertion Sort](#insertion-sort-1)
 16      * [K-Distribution Sort](#k-distribution-sort-2)
 17      * [Turk Algorithm](#turk-algorithm-3)
 18      * [Alternatives Considered](#alternatives-considered)
 19   * [Data structure](#data-structure)
 20      * [The choice](#the-choice)
 21      * [How it works](#how-it-works)
 22         * [Differences in our implementation](#differences-in-our-implementation)
 23         * [Implementation](#implementation)
 24            * [Elements](#elements)
 25            * [Lifecycle functions](#lifecycle-functions)
 26            * [Operation functions](#operation-functions)
 27               * [Push](#push)
 28               * [Swap](#swap)
 29               * [Rotate](#rotate)
 30               * [Reverse rotate](#reverse-rotate)
 31               * [Rotating after pushing](#rotating-after-pushing)
 32            * [Querying the stack](#querying-the-stack)
 33      * [Efficiency](#efficiency)
 34         * [Memory management](#memory-management)
 35         * [Memory overhead](#memory-overhead)
 36         * [Operation complexity](#operation-complexity)
 37   * [Error handling](#error-handling)
 38* [Resources](#resources)
 39   * [References](#references)
 40   * [LLM Usage](#llm-usage)
 41
 42# Overview
 43
 44**push_swap** sorts a stack of integer numbers with limited operations (see [Operations](#operations)). \
 45The strategy is specified with a flag (see [Flags](#flags)). If `--adaptive` is specified, the algorithm complexity will be chosen based on disorder (see [Disorder](#disorder)).
 46
 47## Operations
 48
 49Operation | Description
 50--|--
 51`sa (swap a)` | Swap the first two elements at the top of stack `a`
 52`sb (swap b)` | Swap the first two elements at the top of stack `b`
 53`ss` | `sa` and `sb` at the same time
 54`pa (push a)` | Take the first element at the top of `b` and put it at the top of `a`
 55`pb (push b)` | Take the first element at the top of `a` and put it at the top of `b`
 56`ra (rotate a)` | The first element of stack `a` becomes the last one
 57`rb (rotate b)` | The first element of stack `b` becomes the last one
 58`rr` | `ra` and `rb` at the same time
 59`rra (reverse rotate a)` | The last element of stack `a` becomes the first one
 60`rrb (reverse rotate b)` | The last element of stack `b` becomes the first one
 61`rrr` | `rra` and `rrb` at the same time
 62
 63## Flags
 64
 65Flag | Description | Complexity
 66--|--|:--:
 67`--simple` | Uses Insertion sort algorithm | $O(n^2)$
 68`--medium` | Uses K-Distribution algorithm (a variation of Chunk sort) | $O(n \sqrt{n})$
 69`--complex` | Uses Turk algorithm | $O(n \log n)$
 70`--adaptive` | Uses one of the algorithms above depending on disorder | &mdash;
 71`--bench` | Prints benchmark metrics (disorder, strategy, op counts) to stderr | &mdash;
 72
 73## Disorder
 74
 75Disorder is a number in the scale of $[0, 1]$. The closer it is to 1, the less sorted the list of numbers is.
 76
 77Disorder | Algorithm complexity
 78:--:|:--:
 79$[0.0, 0.2)$ | $O(n^2)$
 80$[0.2, 0.5)$ | $O(n \sqrt{n})$
 81$[0.5, 1.0]$ | $O(n \log n)$
 82
 83# Instructions
 84
 85## Makefile
 86
 87Command | Description
 88--|--
 89`make`, `make all`, `make push_swap` | Build the `push_swap` binary
 90`make bonus`, `make checker` | Build the `checker` binary
 91`make clean` |  Remove the `build/` directory and call the same command for `libft`
 92`make fclean` | Remove the `build/` directory and the two binaries, and call the same command for `libft`
 93`make re` | Rebuild the `push_swap` binary (equivalent to `make fclean` followed by `make`)
 94`make rebonus` | Rebuilds the `checker` binary (equivalent to `make fclean` followed by `make bonus`)
 95
 96## Usage
 97
 981. Clone the repository:
 99	```bash
100	git clone --recurse-submodules git@github.com:42lx/push_swap.git && cd push_swap
101	```
102
1032. Build it:
104	```bash
105	make && make bonus
106	```
107
1083. Run it:
109	```bash
110	echo '5 4 3 2 1' > args.txt
111	```
112
113	```bashhttps://en.wikipedia.org/wiki/Insertion_sort
114	./push_swap --bench --adaptive $(<args.txt) > ops.txt 2> bench.txt
115	```
116
117   Expected `ops.txt` file contents:
118	```bash
119	$ cat ops.txt
120	ra
121	ra
122	ra
123	pb
124	pb
125	ra
126	sa
127	pa
128	pa
129	sa
130	```
131
132   Expected `bench.txt` file contents:
133	```bashhttps://en.wikipedia.org/wiki/Insertion_sort
134	$ cat bench.txt
135	[bench] disorder:   100.00%
136	[bench] strategy:   Adaptive / O(n log n)
137	[bench] total_ops:  10
138	[bench] sa: 2  sb: 0  ss: 0  pa: 2  pb: 2
139	[bench] ra: 4  rb: 0  rr: 0  rra: 0  rrb: 0  rrr: 0
140	```
141
1424. Test it:
143	```bash
144	./checker $(<args.txt) < ops.txt
145	```
146
147   Expected output:
148	```bash
149	OK
150	```
151
152# Description
153
154## Algorithms
155
156### Insertion Sort [^5]
157
158Among $O(n^2)$ algorithms, insertion sort is the natural fit for this project. Its cost scales with the number of inversions, so a nearly sorted input is cheap. Selection sort would always scan the full remaining stack; bubble sort requires multiple passes.
159
160### K-Distribution Sort [^3]
161
162Partition the value range into $k ≈ \sqrt n$ chunks, push each chunk from A to B in one pass, then pull B back to A by repeatedly extracting the maximum. Among chunking strategies, K-sort is the most performant while remaining simple to implement and reason about.
163
164The neat thing is the shape it leaves in B: a K, sometimes called a butterfly. Each chunk gets split on push&mdash;upper-half elements land at the top via plain `pb`, lower-half elements sink to the bottom via `pb` + `rb`. Plot position against value in B and you get two lines diverging from a pinch point, hence the K. The max stays near the top, so the pull-back phase rarely rotates deep.
165
1661.4 coefficient was chosen after many benchmarks comparing different coefficients.
167
168### Turk Algorithm [^4]
169
170Turk sort is the most elegant high-performance algorithm. It always chooses the element whose combined rotation cost with target is lowest. The cost-based calculation pairs perfectly with high-disorder data: when values are randomly scattered, many elements have low-cost moves at any given moment, and the greedy choice reliably finds them. The key saving is that when A and B need to rotate in the same direction, the `rr` and `rrr` double-rotation operations move both stacks for the price of one.
171
172Elements in stack A look for targets in stack B (max below). The relationship is surjective (each element has 1 target, but the reverse isn't true).
173
174In this project, we implemented a simple version of Turk algorithm that performs cost calculations only while pushing to B, while reusing insertion logic to push back to A. This is good enough while keeping the code simple and readable.
175
176![Graph representing surjection function](./assets/surjection.jpg)
177
178### Alternatives Considered
179
180**Heap sort** and **Merge sort** don't translate well to this project. Both require random access or pointer-based tree structures that have no natural mapping onto two restricted stacks. Simulating them would require far more operations than the purpose-built algorithms above.
181
182**Quick sort** can be adapted&mdash;use `pb` to partition around a pivot&mdash;but the implementation complexity grows quickly: tracking pivot positions across both stacks, ensuring balanced partitions, and handling degenerate cases all add significant code for modest gain over K-sort.
183
184**Radix sort** processes bits one at a time, which generates a large number of operations regardless of how ordered the input already is. It ignores all existing structure in the data and produces far more operations than needed for typical inputs.
185
186## Data structure
187
188There are many viable options when it comes to picking a data structure to handle the stacks. We opted for one that lets us handle it in a circular way&mdash;a circular stack&mdash;meaning that after the last element comes the first and vice-versa.
189
190### The choice
191
192We narrowed down our choice to either ring/circular buffer[^1] or doubly circular linked list[^2]. \
193**Ring buffer**, in the context of this project, is much more promising, because of how trivial it is to operate on it&mdash;rotate and reverse rotate are simply indexing math&mdash;and how much more efficient it is (see [Efficiency](#efficiency)), so we decided to work with that one. Linked lists provide us with dynamic memory which we don't require, since we know the capacity in advance.
194
195### How it works
196
197#### Differences in our implementation
198
199The ring buffer we've implemented is a bit different than the regular ring buffer. \
200Instead of keeping track of both head and tail, it keeps track of the head and the buffer's current size. This is because keeping track of tail isn't useful for the scope of this project. Ring buffers are normally used for data streams, where size isn't always known prior to receiving information.
201
202Regular ring buffers are also usually FIFO (First In First Out), but here that concept is abstracted away. The only time an element gets popped out is in the push operation, and in that case it is LIFO (Last In First Out), because we always pop the top most element.
203
204Our head holds a different meaning than the regular ring buffer's head&mdash;our head points to the first element, instead of the last written chunk of data in a regular ring buffer.
205
206#### Implementation
207
208##### Elements
209
210Our stack is composed of the following elements:
211- `buf`: contiguous buffer containing all elements
212- `head`: index in the buffer that keeps track of where to start reading/operating
213- `size`: amount of elements that can be read/operated in the buffer
214- `capacity`: maximum capacity of the buffer (number of elements)
215
216##### Lifecycle functions
217
218The functions that manage the stack lifecycle are:
219- `stack_init`: initializes the buffer, allocating memory to the previously calculated `capacity` and initializes `size` and `head` to 0 (meaning start reading/operating at the first position and can only read/operate at maximum of 0 elements)
220- `stack_build`: initializes the stack with the given values and sets `size` to its maximum value, which is `capacity`
221- `stack_free`: frees the stack, points `buf` to `NULL` and sets all other stack variables to 0
222
223##### Operation functions
224
225The magic of a ring buffer is that every operation is based on the position of $head \mod capacity$, which means that head will always be constrained between $[0, capacity-1]$.
226
227The images that follow each operation will be based on the initial state of the image showed below. \
228This image shows stack A, already populated but unsorted, and stack B, empty. This would be the state of the stacks right before the sorting begins.
229
230![Image showing a general ring buffer](./assets/stack_general.png)
231
232###### Push
233
234`stack_push`: pushes an element from stack `src` to stack `dst`. Pushing, in a ring buffer, simply means moving the `head` of the source stack forward and decreasing its size by 1. The inverse happens in `dst`, writing to the buffer in the new `head` location the value that was read from `src`.
235
236![Image showing the push operation on the buffers](./assets/stack_push.png)
237
238###### Swap
239
240`stack_swap`: swaps the first two elements of a stack.
241
242![Image showing the swap operation](./assets/stack_swap.png)
243
244###### Rotate
245
246`stack_rotate`: rotates the stack by moving `head` to the next element.
247
248![Image showing the rotate operation on a buffer](./assets/stack_rotate.png)
249
250###### Reverse rotate
251
252`stack_rev_rotate`: reversely rotates the stack by moving `head` to the previous element.
253
254![Image showing the reverse rotate operation on a buffer](./assets/stack_rev_rotate.png)
255###### Rotating after pushing
256
257Theoretically, the rotate and reverse rotate would work as described, since the element would get pushed out of the stack. However, since `stack_push` only virtually removes it, we have to add an extra step to `stack_rotate` and `stack_rev_rotate`&mdash;we write the first element to the new (post rotate) last position (or new first position, in case of `stack_rev_rotate`).
258
259![Image showing the problem with pushing and rotating, and its fix](./assets/stack_push_rotate.png)
260
261##### Querying the stack
262
263These functions describe the queries we can make to the stack:
264- `stack_size`: returns the current `size` of the stack
265- `stack_is_empty`: returns whether `size` is 0
266- `stack_at`: returns the value at the index given based on the `head` position
267- `stack_is_sorted`: returns whether stack is sorted
268
269### Efficiency
270
271#### Memory management
272
273Since the size of the stack is known at runtime, using a ring buffer comes in handy because it is only necessary to allocate memory one time (meaning, only one syscall of size `n`). \
274On the other hand, the usage of a linked list would imply `n` syscalls of smaller size, which is way more computationally expensive the bigger `n` grows.
275
276#### Memory overhead
277
278Ring buffer has less overhead: three integer variables of integer type (4 bytes in 64-bit systems)&mdash;head, size and capacity (12 bytes total)&mdash;per stack. \
279Using a linked list, the overhead would be: two pointers (8 bytes)&mdash;`prev` and `next` (16 bytes total)&mdash;***per node***.
280
281This means that for 100 numbers, ring buffer would store $1 \times 20=20$ bytes of overhead, while linked list would store $16 \times 100=1600$ bytes of overhead in total, which scales in $O(n)$. That is a very considerable amount of extra memory needed, simply for bookkeeping.
282
283#### Operation complexity
284
285Both structures have $O(1)$ time complexity for basic operations, but the constant in the ring buffer is much smaller:
286- swap is swapping two values at fixed indexes
287- rotate is incrementing the head
288- push is decrementing the head and copying a value
289
290Using a linked list, this would be:
291- swap is swapping pointers in 4 nodes
292- rotate is updating the head to the next node
293- push is moving one node between two heads, which comes with altering a lot of pointers
294
295Working with a linked list would add a lot of complexity that is easily avoidable.
296
297## Error handling
298Errors exit via `die(int status)`, which prints `Error` to `stderr` and calls `exit(status)`. Exit codes are defined in `include/cli.h`:
299  `ERR_MALLOC` (1) for allocation failure, `ERR_PARSE` (2) for invalid integer input, `ERR_FLAG` (3) for unknown flags, and `ERR_INPUT` (4) for general input errors.
300
301  Run `echo $?` immediately after the binary to inspect
302  the exit code and identify the failure cause.
303
304# Resources
305
306
307## References
308- [Turk Algorithm explained in 6 steps](https://pure-forest.medium.com/push-swap-turk-algorithm-explained-in-6-steps-4c6650a458c0)
309- [Push swap — a deep dive](https://dynamicdispat.ch/posts/2024/01/push-swap/)
310- [Push swap in less than 4200 operations (K-sort)](https://medium.com/@ulysse.gks/push-swap-in-less-than-4200-operations-c292f034f6c0)
311- [My implementation of the 42 push_swap project](https://dansylvain.com/blog/algorithm/my-implementation-of-the-42-push-swap-project)
312- [Stack Overflow: most efficient way to sort with two stacks](https://stackoverflow.com/questions/75100698/push-swap-what-is-the-most-efficient-way-to-sort-given-values-using-a-limited-s)
313- [How to over-optimize push_swap](https://medium.com/@kilfenbaridon/how-to-over-optimize-an-algorithm-push-swap-42-school-project-d01a50e5fd78)
314- [The least amount of moves with two stacks](https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a)
315- [Push-Swap-Tester](https://github.com/gemartin99/Push-Swap-Tester)
316- [push_swap visualizer](https://github.com/lmarquar/push_swap_visualizer)
317
318## LLM Usage
319
320LLMs were used for:
321- Design decisions
322- Algorithm research
323- Parts of this README
324
325[^1]: [Circular buffer](https://en.wikipedia.org/wiki/Circular_buffer)
326[^2]: [Doubly circular linked list](https://en.wikipedia.org/wiki/Doubly_linked_list)
327[^3]: [K-Distribution Sort applied to the push-swap problem](https://medium.com/@brakebein42/k-distribution-sort-applied-to-the-push-swap-problem-ae2d96d68376)
328[^4]: [Push Swap — A journey to find most efficient sorting algorithm](https://medium.com/@ayogun/push-swap-c1f5d2d41e97)
329[^5]: [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort)