{"id":26202449,"url":"https://github.com/f-corvaro/push_swap","last_synced_at":"2025-11-01T07:03:53.533Z","repository":{"id":222000489,"uuid":"755983679","full_name":"f-corvaro/PUSH_SWAP","owner":"f-corvaro","description":"\"Algorithm that sorts data using two stacks.\"","archived":false,"fork":false,"pushed_at":"2024-07-27T17:22:56.000Z","size":13068,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-28T13:04:58.532Z","etag":null,"topics":["42","42network","42roma","42romaluiss","42school","bonus","guide","mandatory","mechanicalturk","pushswap","pushswap-42","pushswap42","sorting","sorting-algorithms","stack","student-project"],"latest_commit_sha":null,"homepage":"https://github.com/f-corvaro","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/f-corvaro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"f-corvaro","ko_fi":"fcorvaro"}},"created_at":"2024-02-11T16:50:38.000Z","updated_at":"2024-07-27T17:23:00.000Z","dependencies_parsed_at":"2024-02-11T18:23:18.662Z","dependency_job_id":"2389c0e2-370b-453e-bc78-b7ce8ce39c13","html_url":"https://github.com/f-corvaro/PUSH_SWAP","commit_stats":null,"previous_names":["f-corvaro/push_swap"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f-corvaro%2FPUSH_SWAP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f-corvaro%2FPUSH_SWAP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f-corvaro%2FPUSH_SWAP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f-corvaro%2FPUSH_SWAP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f-corvaro","download_url":"https://codeload.github.com/f-corvaro/PUSH_SWAP/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243152861,"owners_count":20244656,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["42","42network","42roma","42romaluiss","42school","bonus","guide","mandatory","mechanicalturk","pushswap","pushswap-42","pushswap42","sorting","sorting-algorithms","stack","student-project"],"created_at":"2025-03-12T03:37:12.050Z","updated_at":"2025-11-01T07:03:53.527Z","avatar_url":"https://github.com/f-corvaro.png","language":"C","funding_links":["https://github.com/sponsors/f-corvaro","https://ko-fi.com/fcorvaro"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP/tree/main\"\u003e\n\t\u003cimg src=\"https://github.com/f-corvaro/PUSH_SWAP/blob/main/.extra/ps.png\" alt=\"PUSH_SWAP\"\u003e\n  \u003c/a\u003e\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003cb\u003e\u003ci\u003e\"Algorithm that sorts data using two stacks.\"\u003c/i\u003e\u003c/b\u003e\u003cbr\u003e\n\u003c/p\u003e\n\u003cp align=\"center\" style=\"text-decoration: none;\"\u003e\n    \u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg alt=\"GitHub code size in bytes\" src=\"https://img.shields.io/github/languages/code-size/f-corvaro/PUSH_SWAP?color=blueviolet\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg alt=\"Code language count\" src=\"https://img.shields.io/github/languages/count/f-corvaro/PUSH_SWAP?color=yellow\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg alt=\"GitHub top language\" src=\"https://img.shields.io/github/languages/top/f-corvaro/PUSH_SWAP?color=blueviolet\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg alt=\"GitHub last commit\" src=\"https://img.shields.io/github/last-commit/f-corvaro/PUSH_SWAP?color=yellow\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003ch3 align=\"center\"\u003eIndex\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\n \u003ca href=\"#introduction\"\u003eIntroduction\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#overview\"\u003eOverview\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#folder-structure\"\u003eFolder Structure\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#project-requirements---mandatory-part\"\u003eProject Requirements - Mandatory Part\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#important-rules-and-guidelines\"\u003eImportant Rules and Guidelines\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#program-name\"\u003eProgram name\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#files-to-submit\"\u003eFiles to Submit\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#allowed-external-functions\"\u003eAllowed External Functions\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#project-objective\"\u003eProject Objective\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#sorting-rules\"\u003eSorting Rules\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#example\"\u003eExample\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#requirements\"\u003eRequirements\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#provided-resources\"\u003eProvided Resources\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#usage-examples\"\u003eUsage Examples\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#performance-benchmarks\"\u003ePerformance Benchmarks\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#project-requirements---bonus-part\"\u003eProject Requirements - Bonus Part\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#program-name-1\"\u003eProgram Name\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#files-to-submit-1\"\u003eFiles to Submit\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#external-functs-allowed\"\u003eExternal functs. allowed\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#expected-behavior\"\u003eExpected Behavior\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#usage-examples-1\"\u003eUsage Examples\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#theoretical-background\"\u003eTheoretical Background\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#data-structures\"\u003eData Structures\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#algorithms\"\u003eAlgorithms\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#sorting-algorithms\"\u003eSorting Algorithms\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#algorithm-complexity-and-efficiency\"\u003eAlgorithm Complexity and Efficiency\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#stable-sorting-algorithms\"\u003eStable Sorting Algorithms\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#unstable-sorting-algorithms\"\u003eUnstable Sorting Algorithms\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#additional-resources\"\u003eAdditional Resources\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#organizing-your-first-major-project\"\u003eOrganizing Your First Major Project\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#running-tests\"\u003eRunning Tests\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#memory-leak-detection\"\u003eMemory Leak Detection\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#usage-ubuntu\"\u003eUsage Ubuntu\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#usage-macos\"\u003eUsage macOS\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#graphical-user-interface-gui-for-testing\"\u003eGraphical User Interface (GUI) for Testing\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#installation-steps\"\u003eInstallation Steps\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#how-to-use-the-visualizer\"\u003eHow to Use the Visualizer\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#additional-resources-1\"\u003eAdditional Resources\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#evaluation-process\"\u003eEvaluation Process\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#testing-your-project\"\u003eTesting Your Project\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#developed-skills\"\u003eDeveloped Skills\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#references\"\u003eReferences\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#support-and-contributions\"\u003eSupport and Contributions\u003c/a\u003e\u003cbr\u003e\n \u003ca href=\"#author\"\u003eAuthor\u003c/a\u003e\u003cbr\u003e\n\u003c/p\u003e\n\u003cbr\u003e\n\n## Introduction\n\n\u003cp align=\"justify\"\u003e\n\nThe push_swap project is a part of the 42 school curriculum and aims to develop a program named `push_swap` that sorts a list of integers using two stacks. The goal of the project is to achieve the lowest possible number of operations to sort the stack A, while adhering to a limited set of instructions and utilizing the stack B.\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n### Overview\n\n\u003cp align=\"justify\"\u003e\n\nThe `push_swap` program follows a set of rules and operations to manipulate the stacks. These operations include swapping elements, pushing elements between stacks, and rotating elements within a stack. By applying these operations strategically, the program can efficiently sort the stack A.\n\nThe project requirements include writing the program in C, adhering to the Norm coding style, properly managing memory allocation, and providing a Makefile to compile the source files. The program should display the smallest list of instructions possible to sort the stack A, with the smallest number being at the top.\n\nTo evaluate the program's performance, the number of instructions generated by `push_swap` is compared against predefined limits for different stack sizes. The project also allows the use of the `libft` library and requires error handling for invalid input.\n\nBy completing the push_swap project, students gain experience in algorithm design, stack manipulation, and optimization. It challenges their problem-solving skills and encourages them to find efficient sorting strategies within the given constraints.\n\n\u003cp\u003e\n\u003cbr\u003e\n\n## Folder Structure\n\n\u003cp align=\"justify\"\u003e\n\n```\n.\n├── push_swap\n│   ├── include\n│   │   └── push_swap.h\n│   ├── lib\n│   ├── srcs\n│   │   ├── ft_error_handling.c\n│   │   ├── ft_free.c\n│   │   ├── ft_id_stack_a.c\n│   │   ├── ft_id_stack_b.c\n│   │   ├── ft_init_op.c\n│   │   ├── ft_input_preparation.c\n│   │   ├── ft_op_p.c\n│   │   ├── ft_op_r.c\n│   │   ├── ft_op_rr.c\n│   │   ├── ft_op_s.c\n│   │   ├── ft_stack_calc_a.c\n│   │   ├── ft_stack_calc_b.c\n│   │   ├── ft_stack_calc.c\n│   │   ├── ft_stack_half_opa.c\n│   │   ├── ft_stack_half_opb.c\n│   │   ├── ft_stack_op.c\n│   │   ├── ft_stack_searching_op.c\n│   │   ├── ft_stack_sorting.c\n│   │   ├── ft_utils.c\n│   │   └── main.c\n│   ├── srcs_b\n│   │   ├── ft_checker.c\n│   │   └── main.c\n│   └── Makefile\n├── resources_given\n│   ├── checker_linux\n│   └── checker_Mac\n└── README.md\n```\n\n\u003cbr\u003e\n\n## Project Requirements - Mandatory Part\n\n### Important Rules and Guidelines\n\n\u003cp align=\"justify\"\u003e\n\n- Your project must be written in C, adhering to the Norm.\n- Your functions should not terminate unexpectedly (e.g., segmentation fault, bus error, double free) except in cases of undefined behavior.\n- All dynamically allocated memory must be properly freed when no longer needed.\n- You must submit a Makefile that compiles your source files to the required output using the flags `-Wall`, `-Wextra`, and `-Werror`. The Makefile should use `cc` and must not relink. It must include at least the following rules: `$(NAME)`, `all`, `clean`, `fclean`, and `re` (include `bonus` for maximum score).\n- If your project allows the use of your `libft`, you must copy its source files and associated Makefile into a `libft` folder. Your project's Makefile must compile the library using its Makefile before compiling the project.\n- Global variables are forbidden.\n\n\u003cp\u003e\n\n### Program name\n\n\u003cp align=\"justify\"\u003e\n\nThe program is called `push_swap` and takes a stack A (a list of integers) as its arguments.\n\n\u003c/p\u003e\n\n### Files to Submit\n\n\u003cp align=\"justify\"\u003e\n\nYou need to submit the following files: `Makefile`, `*.h`, `*.c`.\n\n\u003c/p\u003e\n\n### Allowed External Functions\n\n\u003cp align=\"justify\"\u003e\n\nYou are permitted to use the `libft` library, along with the following functions:\n\n1. `read, write, malloc, free, exit`\n2. `ft_printf` and any equivalent functions you have coded\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n### Project Objective\n\n\u003cp align=\"justify\"\u003e\n\nThe objective is to sort the stack with the minimum number of operations. \n\n\u003c/p\u003e\n\n#### Sorting Rules\n\n\u003cp align=\"justify\"\u003e\n\nYou have two stacks named `a` and `b`. Stack `a` contains a random amount of unique negative and/or positive numbers, while stack `b` is initially empty. The goal is to sort the numbers in ascending order in stack `a`. To achieve this, you have the following operations at your disposal:\n\n| Operation | Description |\n| --------- | ----------- |\n| `sa` (swap a) | Swap the first two elements at the top of stack `a`. Do nothing if there is only one or no elements. |\n| `sb` (swap b) | Swap the first two elements at the top of stack `b`. Do nothing if there is only one or no elements. |\n| `ss` | Perform `sa` and `sb` simultaneously. |\n| `pa` (push a) | Take the first element at the top of `b` and put it at the top of `a`. Do nothing if `b` is empty. |\n| `pb` (push b) | Take the first element at the top of `a` and put it at the top of `b`. Do nothing if `a` is empty. |\n| `ra` (rotate a) | Shift up all elements of stack `a` by one. The first element becomes the last one. |\n| `rb` (rotate b) | Shift up all elements of stack `b` by one. The first element becomes the last one. |\n| `rr` | Perform `ra` and `rb` simultaneously. |\n| `rra` (reverse rotate a) | Shift down all elements of stack `a` by one. The last element becomes the first one. |\n| `rrb` (reverse rotate b) | Shift down all elements of stack `b` by one. The last element becomes the first one. |\n| `rrr` | Perform `rra` and `rrb` simultaneously. |\n\n\u003c/p\u003e\n\n#### Example\n\n\u003cp align=\"justify\"\u003e\n\n\u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg width=\"450\" src=\"https://github.com/f-corvaro/PUSH_SWAP/blob/main/.extra/example.png\"\u003e\n\n\u003c/p\u003e\n\n#### Requirements\n\n\u003cp align=\"justify\"\u003e\n\n- The first argument should be at the top of the stack.\n- Instructions must be separated by a newline (`\\n`) and nothing else.\n- The program must display the smallest list of instructions possible to sort stack `a`, with the smallest number at the top.\n- If no parameters are specified, the program must not display anything and should return the prompt.\n- In case of an error, the program must display `\"Error\"` followed by a newline (`\\n`) on the standard error. Errors include:\n  - Some arguments are not integers.\n  - Some arguments exceed the integer limit (MAXINT).\n  - There are duplicate arguments.\n\n\u003c/p\u003e\n\n#### Provided Resources\n\n\u003cp align=\"justify\"\u003e\n\nTwo programs named `checker` are provided: one for Linux and one for macOS. You can download them from [here](https://github.com/f-corvaro/PUSH_SWAP/tree/main/resources). To use these executables correctly, you need to modify their permissions. Run the following command:\n\n```bash\nchmod 777 \u003cname_of_the_executable\u003e\n```\n\n\u003c/p\u003e\n\n#### Usage Examples\n\n\u003cp align=\"justify\"\u003e\n\nThe `push_swap` program takes the unsorted list of integers as input and processes it using its sorting algorithm. It generates a list of instructions that describe the steps required to sort the list.\n\n```bash\n$\u003eARG=\"4 67 3 87 23\"; ./push_swap $ARG | ./checker_OS $ARG\nOK\n$\u003e\n```\n\nIf the program `checker_OS` displays \"KO\", it means that your `push_swap` program generated a list of instructions that does not correctly sort the numbers.\n\nThe command `ARG=\"4 67 3 87 23\"` initializes a variable `ARG` and assigns the string \"4 67 3 87 23\" to it. This string contains a sequence of space-separated numbers: 4, 67, 3, 87, and 23. These numbers represent the unsorted list of integers that will be passed to the `push_swap` program. The `ARG` variable acts as a placeholder for the unsorted list of integers and is used later in the command to pass the list to the `push_swap` program.\n\nA pipe (`|`) is used to connect the output of the `push_swap` program to the input of the `wc -l` command. Each instruction generated by the `push_swap` program represents a separate line. The `wc -l` command counts the number of lines in its input, effectively counting the number of instructions generated by the `push_swap` program.\n\n```bash\n$\u003e./push_swap 2 1 3 6 5 8\nsa\npb\npb\npb\nsa\npa\npa\npa\n```\n\n```bash\n$\u003e./push_swap 0 one 2 3\nError\n```\n\n```bash\n$\u003eARG=\"4 67 3 87 23\"; ./push_swap $ARG | wc -l\n6\n```\n\n\u003cp\u003e\n\n#### Performance Benchmarks\n\n\u003cp align=\"justify\"\u003e\n\nThe performance of your `push_swap` program will be evaluated based on the number of operations required to sort different sets of numbers. The benchmarks are as follows:\n\n- **Required:**\n  - Sort 3 numbers with ≤ 3 operations\n  - Sort 5 numbers with ≤ 12 operations\n\n- **Scored:**\n  - Sort 100 numbers:\n    - ≤ 700 operations (max score)\n    - ≤ 900 operations\n    - ≤ 1100 operations\n    - ≤ 1300 operations\n    - ≤ 1500 operations (min score)\n  - Sort 500 numbers:\n    - ≤ 5500 operations (max score)\n    - ≤ 7000 operations\n    - ≤ 8500 operations\n    - ≤ 10000 operations\n    - ≤ 11500 operations (min score)\n\n\u003cp\u003e\n\u003cbr\u003e\n\n## Project Requirements - Bonus Part\n\n\u003cp align=\"justify\"\u003e\n\nThe bonus task involves creating your own checker program. This checker takes the initial stack `a` as a list of integers passed as arguments. The first argument should be at the top of the stack (be mindful of the order). If no argument is given, the program should stop and display nothing. The checker will then wait and read instructions from the standard input, with each instruction followed by a newline (`\\n`). Once all instructions have been read, the program must execute them on the stack received as an argument.\n\n\u003c/p\u003e\n\n### Program Name\n\n\u003cp align=\"justify\"\u003e\n\nThe program is named `checker` and takes as arguments a stack `A` (a list of integers).\n\n\u003c/p\u003e\n\n### Files to Submit\n\n\u003cp align=\"justify\"\u003e\n\nYou need to submit the following files:\n\n- `*.h`\n- `*.c`\n\n\u003c/p\u003e\n\n\n### External functs. allowed:**\n\n\u003cp align=\"justify\"\u003e\n\nYou are allowed to use the following external functions:\n\n- Functions from the `Libft` library.\n- Standard library functions: `read`, `write`, `malloc`, `free`, `exit`.\n- `ft_printf` or any equivalent function that you have coded.\n\n\u003c/p\u003e\n\n### Expected Behavior\n\n\u003cp align=\"justify\"\u003e\n\n- **Correct Execution**: If, after executing the instructions, stack `a` is sorted in ascending order and stack `b` is empty, the program must display `\"OK\"` followed by a newline (`\\n`) on the standard output.\n- **Incorrect Execution**: In all other cases, the program must display `\"KO\"` followed by a newline (`\\n`) on the standard output.\n- **Error Handling**: In case of an error, the program must display `\"Error\"` followed by a newline (`\\n`) on the standard error. Errors include:\n  - Non-integer arguments.\n  - Arguments exceeding the integer limit.\n  - Duplicate arguments.\n  - Non-existent or incorrectly formatted instructions.\n\n\u003c/p\u003e\n\n### Usage Examples\n\n\u003cp align=\"justify\"\u003e\n\n```bash\n$\u003e./checker 3 2 1 0\nrra\npb\nsa\nrra\npa\nOK\n```\n\n```bash\n$\u003e./checker 3 2 1 0\nsa\nrra\npb\nKO\n```\n\n```bash\n$\u003e./checker 3 2 one 0\nError\n```\n\n```bash\n$\u003e./checker \"\" 1\nError\n$\u003e\n```\n\n\u003cp align=\"justify\"\u003e\n\nYou do not have to reproduce the exact behavior of the provided binary. It is mandatory to manage errors, but you can decide how to parse the arguments.\n\n\u003cp\u003e\n\u003cbr\u003e\n\n## Theoretical Background\n\n### Data Structures\n\n\u003cp align=\"justify\"\u003e\n\n- **Stacks**: Stacks are a type of data structure that follows the LIFO (Last In, First Out) principle. In a stack, the last element added is the first one to be removed. Stacks are commonly used in various applications, including the `push_swap` program.\n\n  - **Operations**: Stacks support several operations:\n\n    - `push`: This operation adds an element to the top of the stack.\n\n    - `pop`: This operation removes the top element from the stack.\n\n    - `peek`: This operation allows you to access the top element of the stack without removing it.\n\n    - `isEmpty`: This operation checks if the stack is empty or not.\n\n  Stacks are efficient for managing data that needs to be accessed in a last-in, first-out manner. They are widely used in programming and can be implemented using arrays or linked lists.\n\n\u003cp\u003e\n\u003cbr\u003e\n\n### Algorithms\n\n\u003cp align=\"justify\"\u003e\n\nAn algorithm is a step-by-step procedure or set of rules for solving a specific problem or accomplishing a specific task. It is a well-defined sequence of instructions that takes an input and produces an output. Algorithms are used in various fields, including computer science, mathematics, and engineering, to solve complex problems efficiently and effectively. \n\n\u003cp\u003e\n\u003cbr\u003e\n\n#### Sorting Algorithms\n\n\u003cp align=\"justify\"\u003e\n\nA sorting algorithm, on the other hand, is a specific type of algorithm that arranges a collection of elements in a particular order. The goal of a sorting algorithm is to rearrange the elements in ascending or descending order, based on a specified comparison criterion. Sorting algorithms are widely used in computer science and data processing to organize and retrieve data efficiently. There are various sorting algorithms available, each with its own advantages and disadvantages in terms of time complexity, space complexity, and stability. Some common sorting algorithms include merge sort, insertion sort, bubble sort, and quicksort. These algorithms employ different techniques and strategies to achieve the desired sorting outcome. Overall, sorting algorithms play a crucial role in data manipulation and analysis, enabling efficient searching, filtering, and processing of large datasets. \n\n\u003cp\u003e\n\u003cbr\u003e\n\n#### Algorithm Complexity and Efficiency\n\n\u003cp align=\"justify\"\u003e\n\nWhen analyzing algorithms, it is important to understand their complexity and efficiency. This helps us determine how the algorithm's performance scales with the size of the input.\n\nOne commonly used notation for describing algorithm complexity is Big O notation. It provides an upper bound on the running time of an algorithm, indicating how the algorithm's performance grows as the input size increases.\n\nHere are some common Big O notations and their corresponding time complexities:\n\n- **O(1)**: Constant time complexity. The algorithm's running time remains constant regardless of the input size. This is the most efficient time complexity.\n\n- **O(log n)**: Logarithmic time complexity. The algorithm's running time increases logarithmically with the input size. Algorithms with this complexity often divide the input in half at each step, such as binary search.\n\n- **O(n)**: Linear time complexity. The algorithm's running time grows linearly with the input size. As the input doubles, the running time also doubles. This is a common time complexity for algorithms that iterate through each element in the input.\n\n- **O(n log n)**: Linearithmic time complexity. The algorithm's running time grows in proportion to the product of the input size and its logarithm. This complexity is often seen in efficient sorting algorithms like merge sort and quicksort.\n\n- **O(n^2)**: Quadratic time complexity. The algorithm's running time grows quadratically with the input size. As the input doubles, the running time quadruples. This complexity is common in algorithms that involve nested loops.\n\n- **O(2^n)**: Exponential time complexity. The algorithm's running time grows exponentially with the input size. This is the least efficient time complexity and should be avoided whenever possible.\n\nBy understanding the time complexity of an algorithm, we can make informed decisions about which algorithm to use for a given problem. It allows us to assess the trade-off between efficiency and scalability, ensuring that our programs can handle larger inputs without significant performance degradation.\n\nRemember, Big O notation provides an upper bound on the running time, so an algorithm with a better time complexity is generally more efficient. However, other factors like memory usage and implementation details should also be considered when evaluating algorithm efficiency.\n\nIn addition to time complexity, algorithm analysis also involves evaluating other factors such as space complexity, which measures the amount of memory an algorithm requires to solve a problem. Space complexity is typically expressed in terms of the input size and can be analyzed using similar notations as time complexity.\n\nFurthermore, algorithm analysis may consider factors like algorithmic correctness, stability, and adaptability. Correctness ensures that the algorithm produces the expected output for all valid inputs. Stability refers to whether the algorithm preserves the relative order of equal elements during sorting or other operations. Adaptability refers to the algorithm's ability to handle different types of inputs or adapt to changing circumstances efficiently.\n\nOverall, algorithm analysis is a crucial step in designing and optimizing algorithms. It helps us understand their behavior, make informed decisions, and improve the efficiency and scalability of our programs.\n\n\u003cp\u003e\n\u003cbr\u003e\n\n\n#### Stable Sorting Algorithms\n\n\u003cp align=\"justify\"\u003e\n\nStable sorting algorithms are designed to preserve the relative order of equal elements during the sorting process. This means that if two elements have the same value, their original order will be maintained in the sorted output. Stable sorting algorithms are particularly useful when sorting objects with multiple keys or when the original order of equal elements is important.\n\nSome examples of stable sorting algorithms include:\n\n- **Merge Sort**: Merge sort is a divide-and-conquer algorithm that recursively divides the input into smaller subproblems, sorts them, and then merges the sorted subproblems to produce the final sorted output. Merge sort has a time complexity of O(n log n) and is considered one of the most efficient sorting algorithms.\n\n- **Insertion Sort**: Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a time. It iterates through the input array, comparing each element with the elements before it and inserting it into the correct position. Insertion sort has a time complexity of O(n^2) for average and worst cases, but it performs well on small input sizes and partially sorted arrays.\n\n- **Bubble Sort**: Bubble sort is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. It continues this process until the entire array is sorted. Bubble sort has a time complexity of O(n^2) and is not efficient for large input sizes, but it is easy to understand and implement.\n\n- **Binary Tree Sort**: Binary tree sort builds a binary search tree from the input elements and then performs an in-order traversal of the tree to retrieve the sorted output. Binary tree sort has a time complexity of O(n log n) and requires additional space for the tree structure.\n\n#### Unstable Sorting Algorithms\n\nUnstable sorting algorithms do not guarantee the preservation of the relative order of equal elements during the sorting process. This means that the original order of equal elements may change in the sorted output. Unstable sorting algorithms are often faster and more memory-efficient than stable sorting algorithms.\n\nSome examples of unstable sorting algorithms include:\n\n- **Quicksort**: Quicksort is a divide-and-conquer algorithm that selects a pivot element and partitions the input array into two subarrays, one with elements less than the pivot and one with elements greater than the pivot. It then recursively sorts the subarrays. Quicksort has an average time complexity of O(n log n) and a worst-case time complexity of O(n^2), but it is often faster in practice due to its efficient partitioning.\n\n- **Heap Sort**: Heap sort builds a binary heap from the input elements and repeatedly extracts the maximum element from the heap to produce the sorted output. Heap sort has a time complexity of O(n log n) and is an in-place sorting algorithm, meaning it does not require additional space beyond the input array.\n\n- **Selection Sort**: Selection sort repeatedly selects the minimum element from the unsorted part of the array and swaps it with the first unsorted element. It continues this process until the entire array is sorted. Selection sort has a time complexity of O(n^2) and is not efficient for large input sizes, but it has the advantage of minimizing the number of swaps.\n\nIt's important to consider the stability of sorting algorithms when the relative order of equal elements is significant. Stable sorting algorithms are preferred in such cases to ensure the desired order is maintained. However, if the relative order of equal elements is not important or if performance is a priority, unstable sorting algorithms can be more suitable.\n\n\u003cbr\u003e\n\n#### Additional Resources\n\n- [Top 10 Sorting Algorithms You Need to Know](https://www.crio.do/blog/top-10-sorting-algorithms/)\n- [Big O Notation Explained](https://www.bigocheatsheet.com/)\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n## Organizing Your First Major Project\n\n\u003cp align=\"justify\"\u003e\n\nEmbarking on your first significant project can be daunting, but with the right approach, you can manage it effectively. Here are some steps to help you get started:\n\n1. **Understand the Requirements**: Begin by thoroughly understanding what the program needs to accomplish. Study the necessary theory and decide on the algorithm you want to implement.\n2. **Break Down the Project**: Divide the project into smaller, manageable tasks. This will make it easier to tackle each part individually and avoid feeling overwhelmed.\n\nTo help you visualize this process, here is an example of an organization pattern:\n\nFirstly, you need to understand what this program should do. You need to study all the theory that you need and understand which algorithm you want to implement in your code. Secondly, you need to do a pattern to understand how to divide this project into many smaller ones. So, don't panic. I will show you my organization pattern:\n\n\u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP/blob/main/.extra/Push_Swap.png\"\u003e\u003cimg src=\"https://github.com/f-corvaro/PUSH_SWAP/blob/main/.extra/Push_Swap.png\"\u003e\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n## Running Tests\n\n\u003cp align=\"justify\"\u003e\n\nMy project is based on a brute force sorting algorithm. I have completed both the mandatory and bonus parts, and each function includes a Doxygen style comment explaining its purpose.\n\nTo generate a set of random numbers for testing, I used the following Python script:\n\n```py\nimport random\nnumbers = random.sample(range(-2147483648, 2147483647), 500)\nrandom.shuffle(numbers)\nprint(' '.join(map(str, numbers)))\n```\n\nYou can run this script on this [online Python interpreter](https://www.online-python.com/).\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n### Memory Leak Detection\n\n\u003cp align=\"justify\"\u003e\n\nTo find memory leaks and errors, I used `Valgrind`. Below are the steps for installation and usage:\n\n#### Installation\n\nDepending on your Linux distribution, use one of the following commands to install Valgrind:\n\n```shell\nsudo apt install valgrind  # Ubuntu, Debian, etc.\nsudo yum install valgrind  # RHEL, CentOS, Fedora, etc.\nsudo pacman -Syu valgrind  # Arch, Manjaro, Garuda, etc.\nsudo pkg ins valgrind      # FreeBSD\n```\n\n#### Usage Ubuntu\n\nTo check for memory leaks and errors, use the following Valgrind command:\n\n```shell\nvalgrind --leak-check=full --show-leak-kinds=all --track-origins=yes -s ./a.out\n```\n\n- `--leak-check=full`: Perform a detailed memory leak check.\n- `--show-leak-kinds=all`: Show all kinds of leaks, including definitely lost, indirectly lost, possibly lost, and still reachable.\n- `--track-origins=yes`: Track the origins of uninitialized values.\n- `-s`: Provide a summary of the leak check.\n- ADDITIONAL `--log-file`: Directs Valgrind's output to a specified file. This is useful for preserving extensive output that exceeds terminal capacity, allowing for easier review and analysis.\n\n#### Usage macOS\n\nTo check for memory leaks and errors on macOS, use the following `leaks` command:\n\n```leaks -atExit -- ./your_program \u003e leaks_report.txt```\n\n  - `-atExit`: Perform the leak check at the exit of the program.\n  - `--`: Indicates the end of options and the start of the program to be checked.\n  - `\u003e leaks_report.txt`: Redirects the output to a file named leaks_report.txt for easier review and analysis. This is useful for preserving extensive output that exceeds terminal capacity, allowing for easier review and analysis.\n\n\u003cbr\u003e\n\n### Graphical User Interface (GUI) for Testing\n\n\u003cp align=\"justify\"\u003e\n\nTo visualize the stacks, I used the [push-swap-gui 1.1](https://pypi.org/project/push-swap-gui/) package, written in Python. This package is available on PyPI and can be installed using `pip3`. To run the GUI, navigate to your `push_swap` folder and execute the command: `push_swap_gui`.\n\n#### Installation Steps\n\n1. **Install the GUI Package**:\n   \n ```bash\n pip3 install push_swap_gui\n ```\n\n2. **Run the GUI**\n\n ```bash\n push_swap_gui\n ```\n\n**Troubleshooting**\n\n If you encounter an error similar to the following:\n\n ```bash\n ERROR: Command errored out with exit status 1:\n command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '\"'\"'/tmp/pip-install-amjzzxzo/pyttk/setup.py'\"'\"'; __file__='\"'\"'/tmp/pip-install-amjzzxzo/pyttk/setup.py'\"'\"';f=getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__);code=f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' egg_info --egg-base /tmp/pip-install-amjzzxzo/pyttk/pip-egg-info\n cwd: /tmp/pip-install-amjzzxzo/pyttk/\n Complete output (7 lines):\n Traceback (most recent call last):\n File \"\u003cstring\u003e\", line 1, in \u003cmodule\u003e\n File \"/tmp/pip-install-amjzzxzo/pyttk/setup.py\", line 2, in \u003cmodule\u003e\n import ttk\n File \"/tmp/pip-install-amjzzxzo/pyttk/ttk.py\", line 36, in \u003cmodule\u003e\n import tkinter as Tkinter\n ModuleNotFoundError: No module named 'tkinter'\n ----------------------------------------\n ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.\n ```\n\n You need to install Tkinter, the standard GUI library for Python. Tkinter provides a fast and easy way to create GUI applications with a powerful object-oriented interface to the Tk GUI toolkit. To install Tkinter on Linux, run the following command:\n\n ```bash\n sudo apt-get install python3-tk\n ```\n\n#### How to Use the Visualizer\n\nTo use the visualizer for your `push_swap` project, follow these steps:\n\n1. **Select a Range of Numbers**: Choose a range of numbers to generate stack A. Remember that 0 counts as 1.\n2. **Configure the Visualizer**:\n   - Uncheck the square flag.\n   - Select the path to your `push_swap` executable.\n3. **Calculate Moves**: Calculate the number of moves required.\n4. **Run the Visualizer**: Click the play button to view the visualizer's moves. You can control the speed of the visualization.\n\n\u003ca href=\"https://github.com/f-corvaro/PUSH_SWAP\"\u003e\u003cimg align=\"center\" alt=\"running test\" width=\"650\" src=\"https://github.com/f-corvaro/PUSH_SWAP/blob/main/.extra/visualizer_my_ps.gif\"\u003e\n\n### Additional Resources\n\nFor automated testing, you can use the [Push_Swap Tester](https://github.com/Aldisti/push_swap-tester) by [Aldisti](https://github.com/Aldisti) or [Push-Swap-Tester](https://github.com/gemartin99/Push-Swap-Tester).\n\n\u003c/p\u003e\n\u003cbr\u003e\n\n## Evaluation Process\n\n\u003cp align=\"justify\"\u003e\n\nTest the following notable cases: 0, 1, 3, 5, 50, 100, 250, and 500. Perform multiple iterations for each case to thoroughly evaluate and understand the efficiency of the algorithm.\n\n\u003c/p\u003e\n\n### Testing Your Project\n\n\u003cp align=\"justify\"\u003e\n\nTo ensure your project meets all requirements, perform the following tests:\n\n1. **Norm Check**: Run `norminette -R CheckForbiddenSourceHeader` in the `push_swap` directory to check for norm compliance.\n2. **Command Tests**: Verify the commands requested using `make`.\n3. **Memory Leak Tests**: Check for memory leaks.\n4. **Error Management**: Ensure proper error handling.\n5. **Push_Swap Tests**: Test the functionality of your `push_swap` implementation.\n6. **Checker Tests**: Validate the checker program.\n\n## Developed Skills\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://skillicons.dev\"\u003e\n    \u003cimg src=\"https://skillicons.dev/icons?i=git,c,vim,vscode\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\u003cbr\u003e\n\n## References\n\n- [Analysis of Algorithms](https://en.wikipedia.org/wiki/Analysis_of_algorithms) - An overview of algorithm analysis, including time and space complexity.\n\n- [Stack Data Structure](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) - Detailed explanation of the stack abstract data type and its operations.\n\n- [Binary Search Algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm) - Comprehensive guide to the binary search algorithm, its implementation, and use cases.\n\n- [Push_swap Algorithm](https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a) - An article on solving the push_swap problem with the least number of moves using two stacks.\n\n- [Linked Lists in C](https://www.learn-c.org/en/Linked_lists) - Tutorial on implementing and using linked lists in the C programming language.\n\n- [Basics of Sorting Algorithms](https://medium.com/basecs/sorting-out-the-basics-behind-sorting-algorithms-b0a032873add) - Introduction to the fundamental concepts behind sorting algorithms.\n\n- [Quicksort Algorithm](https://lamfo-unb.github.io/2019/04/21/Sorting-algorithms/#:~:text=%2B1%5D%20%3D%20temp-,Quicksort,greater%20numbers%20on%20the%20right.) - In-depth explanation of the quicksort algorithm and its implementation.\n\n- [Top 10 Sorting Algorithms](https://www.crio.do/blog/top-10-sorting-algorithms/) - Overview of the top 10 sorting algorithms, their complexities, and use cases.\n\n- [Understanding Quicksort](https://medium.com/basecs/pivoting-to-understand-quicksort-part-2-30161aefe1d3) - Detailed article on the quicksort algorithm, focusing on the pivot selection.\n\n- [Efficient Push_swap Algorithm](https://medium.com/@ayogun/push-swap-c1f5d2d41e97) - Exploration of the most efficient algorithms for solving the push_swap problem.\n\n- [Fastest Push_swap Algorithm](https://zainab-dnaya.medium.com/fastest-push-swap-algorithm-2f510028602b) - Analysis of the fastest known algorithm for the push_swap problem.\n\u003cbr\u003e\n\n## Support and Contributions\n\n\u003cp align=\"center\"\u003e\nIf you find this repository helpful, please consider starring it to show your support. Your support is greatly appreciated!\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://ko-fi.com/fcorvaro\"\u003e\u003cimg width=\"180\" img align=\"center\" src=\"https://github.com/f-corvaro/42.common_core/blob/main/.extra/support-me-ko-fi.svg\"\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/sponsors/f-corvaro\"\u003e\u003cimg width=\"180\" img align=\"center\" src=\"https://github.com/f-corvaro/42.common_core/blob/main/.extra/support-me-github.svg\"\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\n\u003cbr\u003e\n\n## Author\n\n\u003cp align=\"center\"\u003e\u003ca href=\"https://profile.intra.42.fr/users/fcorvaro\"\u003e\u003cimg style=\"height:auto;\" src=\"https://avatars.githubusercontent.com/u/102758065?v=4\" width=\"100\" height=\"100\"alt=\"\"\u003e\u003c/a\u003e\n\u003cp align=\"center\"\u003e\n\u003ca href=\"mailto:fcorvaro@student.42roma.it\"\u003e\u003ckbd\u003eEmail\u003c/kbd\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/f-corvaro\"\u003e\u003ckbd\u003eGithub\u003c/kbd\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/f-corvaro/\"\u003e\u003ckbd\u003eLinkedin\u003c/kbd\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\u003ca href=\"https://42born2code.slack.com/team/U050L8XAFLK\"\u003e\u003ckbd\u003eSlack\u003c/kbd\u003e\u003calt=\"\"\u003e\u003c/a\u003e\n\n\u003chr/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff-corvaro%2Fpush_swap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff-corvaro%2Fpush_swap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff-corvaro%2Fpush_swap/lists"}