Algorithm - pin3dev/42_PushSwap GitHub Wiki
Sorts a stack of three elements in ascending order:
This function sorts exactly three elements in a stack by identifying the maximum and minimum values, then performing the necessary swaps and rotations to order the elements.
| Library | None |
|---|---|
| Signature | void sort_3(t_stack **stack_a); |
| Parameters |
stack_a: A pointer to stack A, which contains exactly three elements. |
| Return | None |
// Initial stack A: 5 -> 3 -> 8 -> NULL
sort_3(&stack_a);
// After sorting: 3 -> 5 -> 8 -> NULL
- Uses comparisons to determine the maximum and minimum elements in the stack.
- Applies
sa(),ra(), andrra()operations to rearrange the elements in ascending order.
Sorts a stack with more than three elements using an advanced sorting algorithm:
This function handles large stacks by moving elements between two stacks (A and B), adjusting their positions based on calculated movements, and reordering the stacks until all elements are sorted.
| Library | None |
|---|---|
| Signature | void big_sort(t_stack **stack_a, t_stack **stack_b); |
| Parameters |
stack_a: A pointer to stack A, which contains the elements to be sorted. |
stack_b: A pointer to stack B, which serves as a temporary holding stack during sorting. |
|
| Return | None |
// Initial stack A: 9 -> 4 -> 2 -> 8 -> 6 -> 5 -> NULL
// Initial stack B: (empty)
big_sort(&stack_a, &stack_b);
// After sorting stack A: 2 -> 4 -> 5 -> 6 -> 8 -> 9 -> NULL
// Stack B: (empty)
- Applies the Turk Algorithm, transferring elements between stacks A and B using efficient movements.
- Uses a combination of simultaneous rotations and independent movements to minimize the number of operations.
Sorts a stack based on its size using appropriate sorting strategies:
This function selects the appropriate sorting strategy depending on the size of stack A, using a mix of sorting algorithms tailored for different cases.
| Library | None |
|---|---|
| Signature | void sort_cases(t_stack **stack_a); |
| Parameters |
stack_a: A pointer to stack A, which contains the elements to be sorted. |
| Return | None |
// Initial stack A: 7 -> 3 -> 4 -> 1 -> 6 -> 2 -> NULL
sort_cases(&stack_a);
// After sorting: 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> NULL
- For small stacks (2 or 3 elements), it uses simple swaps or
sort_3().- For larger stacks (4 or more elements), it moves some elements to stack B, applies
big_sort(), and then returns the sorted elements to stack A.
Finds the appropriate destination place for a source node within the destination stack:
This function determines the best position to insert a node (src) from the source stack into the destination stack (dest). It scans the destination stack to find a node that fits the correct ordering, considering boundaries and ordering criteria.
| Library | None |
|---|---|
| Signature | t_stack *return_dest_place(t_stack *src, t_stack **dest); |
| Parameters |
src: The source node that needs to be placed in the destination stack. |
dest: A pointer to the destination stack. |
|
| Return | A pointer to the node in the destination stack representing the optimal insertion place. |
// Initial source node: content = 3
// Destination stack: 1 -> 5 -> 9 -> NULL
t_stack *place = return_dest_place(src_node, &stack_b);
// The function identifies node with content = 5 as the best place for insertion.
- It checks for boundary conditions using
ck_new_min_max().- Iterates through the destination stack to find the most suitable insertion point for the source node.
Finds the node with the fewest total movements in the source stack:
This function searches the source stack (src) and identifies the node that requires the least number of movements to be properly placed in the destination stack.
| Library | None |
|---|---|
| Signature | t_stack *find_best_case(t_stack **src); |
| Parameters |
src: A pointer to the source stack. |
| Return | A pointer to the node with the smallest total_mov value, representing the best candidate for the next operation. |
// Initial source stack: 4(total_mov=3) -> 2(total_mov=1) -> 6(total_mov=2) -> NULL
t_stack *best_case = find_best_case(&stack_a);
// The function selects node with content = 2 as it has the fewest movements.
- Compares the
total_movvalue for each node and selects the one with the minimum value.
Moves a node to the top of stack A:
This function moves a specified node from stack A to the top, based on its movement orientation (mov_orientation). It uses either rotation (ra()) or reverse rotation (rra()) depending on the required direction.
| Library | None |
|---|---|
| Signature | void mv_node_to_top_a(t_stack *node_a, t_stack **stack_a); |
| Parameters |
node_a: The node from stack A to be moved to the top. |
stack_a: A pointer to stack A. |
|
| Return | None |
// Initial stack A: 5 -> 3(mov=1, orientation=0) -> 8 -> 1 -> NULL
mv_node_to_top_a(node_a, &stack_a);
// After 2 rotations (ra):
// stack A becomes: 3 -> 8 -> 1 -> 5 -> NULL
- Repeatedly rotates or reverse rotates the stack until the node reaches the top, decreasing the
movvalue with each operation.
Moves two nodes simultaneously to the top of stack A and stack B:
This function moves two nodes (node_aandnode_b) from stack A and stack B to the top of their respective stacks, using either double rotation (rr()) or double reverse rotation (rrr()), depending on their movement orientation.
| Library | None |
|---|---|
| Signature | void mv_node_to_top_ab(t_stack *node_a, t_stack *node_b, t_stack **stack_a, t_stack **stack_b); |
| Parameters |
node_a: The node from stack A to be moved to the top. |
node_b: The node from stack B to be moved to the top. |
|
stack_a: A pointer to stack A. |
|
stack_b: A pointer to stack B. |
|
| Return | None |
// Initial stack A: 4 -> 6 -> 2(mov=2, orientation=1) -> NULL
// Initial stack B: 9 -> 7 -> 3(mov=2, orientation=1) -> NULL
mv_node_to_top_ab(node_a, node_b, &stack_a, &stack_b);
// After 2 simultaneous reverse rotations (rrr):
// stack A becomes: 2 -> 4 -> 6 -> NULL
// stack B becomes: 3 -> 9 -> 7 -> NULL
- If both nodes share the same movement orientation, the function performs simultaneous rotations (
rr()for upward,rrr()for downward) to move them to the top of their respective stacks.
Moves a node to the top of stack B:
This function moves a specified node from stack B to the top, based on its movement orientation (mov_orientation). It uses either rotation (rb()) or reverse rotation (rrb()) depending on the required direction.
| Library | None |
|---|---|
| Signature | void mv_node_to_top_b(t_stack *node_b, t_stack **stack_b); |
| Parameters |
node_b: The node in stack B to be moved to the top. |
stack_b: A pointer to stack B. |
|
| Return | None |
// Initial stack B: 9 -> 5 -> 7 -> 2(mov=1, orientation=1) -> NULL
mv_node_to_top_b(node_b, &stack_b);
// After 1 reverse rotation (rrb):
// stack B becomes: 2 -> 9 -> 5 -> 7 -> NULL
- Repeatedly rotates or reverse rotates the stack until the node reaches the top, decreasing the
movvalue with each operation.
Finds the right position to insert a node into the destination stack:
This function determines the optimal position for a node (src) to be inserted into the destination stack (dest), ensuring that the stack remains ordered.
| Library | None |
|---|---|
| Signature | t_stack *fit_between(t_stack *src, t_stack **dest); |
| Parameters |
src: The node from the source stack to be inserted. |
dest: A pointer to the destination stack. |
|
| Return | A pointer to the node in the destination stack that represents the ideal insertion point. |
// Initial source node: content = 4
// Destination stack: 2 -> 5 -> 8 -> NULL
t_stack *place = fit_between(src_node, &stack_b);
// The function identifies node with content = 5 as the best place for insertion.
- The function first checks for boundary conditions (min/max values) and iterates through the destination stack to find the most suitable insertion point.
Rotates stack A to bring the smallest node to the top, completing the sorting process:
This function rotates stack A to move the node with the smallest value to the top, finalizing the sorting process after all other elements have been moved to stack A.
| Library | None |
|---|---|
| Signature | void rotate_to_finish(t_stack **stack_a, t_stack **stack_b); |
| Parameters |
stack_a: A pointer to stack A. |
stack_b: A pointer to stack B. |
|
| Return | None |
// Initial stack A: 5 -> 8 -> 1(mov=2, orientation=0) -> 3 -> NULL
rotate_to_finish(&stack_a, &stack_b);
// After 2 rotations (ra):
// stack A becomes: 1 -> 3 -> 5 -> 8 -> NULL
- Identifies the smallest node using
ft_min()and rotates it to the top usingmv_node_to_top_a().
Moves all nodes from stack B back to stack A, ensuring correct placement:
This function moves all nodes from stack B back to stack A in their correct order, usingfit_between()to determine the proper place for each node and finalizing the sorting process.
| Library | None |
|---|---|
| Signature | void back_to_a(t_stack **stack_a, t_stack **stack_b); |
| Parameters |
stack_a: A pointer to stack A. |
stack_b: A pointer to stack B. |
|
| Return | None |
// Initial stack A: 1 -> 9 -> 7 -> NULL
// Initial stack B: 8 -> 5 -> NULL
back_to_a(&stack_a, &stack_b);
// After moving all elements using fit_between():
// stack A: 5 -> 7 -> 8 -> 9 -> 1 -> NULL
// The stack is semi-ordered, and by applying one rotation (rra), the stack will become fully ordered.
- Moves nodes from stack B to stack A by placing them in their correct positions and then rotating stack A to finish the sorting process.
Previous ⬅️ • Top ⬆️ • Next ➡️