{"id":20574659,"url":"https://github.com/llefranc/42_push_swap","last_synced_at":"2025-10-08T03:35:22.642Z","repository":{"id":151135595,"uuid":"345669175","full_name":"llefranc/42_push_swap","owner":"llefranc","description":"School project: implement the best algorithm to sort one stack using another stack (quicksort algorithm).","archived":false,"fork":false,"pushed_at":"2021-10-26T20:13:20.000Z","size":2326,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-16T21:40:22.960Z","etag":null,"topics":["42","42born2code","42paris","42school","push-swap","pushswap","quicksort","quicksort-algorithm","stack"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/llefranc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2021-03-08T13:40:18.000Z","updated_at":"2021-10-26T20:13:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"26b46da3-57f6-4be8-bc9f-645d6da92869","html_url":"https://github.com/llefranc/42_push_swap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llefranc%2F42_push_swap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llefranc%2F42_push_swap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llefranc%2F42_push_swap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llefranc%2F42_push_swap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/llefranc","download_url":"https://codeload.github.com/llefranc/42_push_swap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242196021,"owners_count":20087761,"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","42born2code","42paris","42school","push-swap","pushswap","quicksort","quicksort-algorithm","stack"],"created_at":"2024-11-16T05:36:25.968Z","updated_at":"2025-10-08T03:35:17.590Z","avatar_url":"https://github.com/llefranc.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# push_swap (@42Paris)\n\n\u003e This project was code for MACOS. Here is the [subject][8].\n\u003e\n*\"Because swap_push isn't as natural\"*\n\n![Alt text](https://github.com/llefranc/42_push_swap/blob/master/push_swap_example.png)\n\n## About\n\nPush_swap is a project of the mandatory part of the cursus.\n\u003c/br\u003eIt's only made in C and was about creating two programs : one sorting two \"special\" stacks, and another one checking if the stacks are sorted or not. \n\u003c/br\u003eWe were only allowed to modify the two \"stacks\", called **stack a** and **stack b**, using a set of instruction : \n- `sa` *(swap a)* -- swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements).\n- `sb` *(swap b)* -- swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements).\n- `ss` sa and sb at the same time.\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 1. The first element becomes the last one.\n- `rb` *(rotate b)* -- shift up all elements of stack b by 1. The first element becomes the last one.\n- `rr` -- ra and rb at the same time.\n- `rra` *(reverse rotate a)* -- shift down all elements of stack a by 1. The flast element becomes the first one.\n- `rrb` *(reverse rotate b)* -- shift down all elements of stack b by 1. The flast element becomes the first one.\n- `rrr` -- rra and rrb at the same time.\n\nIn order to succeed, a good comprehension of what is **complexity**, and how the best **sorting algorithms** work was necessary. \n\n## How I solved the problem\n\nFor small list of integers (\u003c 5 numbers) I used custom sortings to optimize the number of instructions. \n\u003c/br\u003eFor bigger list, I implemented a variant of quicksort algorithm with several optimizations.\n\u003c/br\u003e\u003c/br\u003eAverage nb of insctructions for 100 tests :\n- :white_check_mark: : 3 numbers in `1` instruction *(grade 5/5)*\n- :white_check_mark: : 5 numbers in `9` instructions *(grade 5/5)*\n- :white_check_mark: : 100 numbers in `772` instructions *(grade 4/5)*\n- :white_check_mark: : 500 numbers in `5357` instructions *(grade 5/5)*\n\n## Building and running the project\n\n1. Download/Clone this repo\n\n        git clone https://github.com/llefranc/42_push_swap.git\n\n2. `cd` into the root directory and run `make`\n\n        cd 42_push_swap\n        make\n\n3.  Run `push_swap` with a list of integers in order to print the list of instructions on stdout.\n\t\n\t\t./push_swap 2 1 5 4\n\n4.\tRun `checker` with a list of integers to solve manually the stack. It will wait for instruction on stdin. Ctrl+D to stop typing instructions\n\t\n\t\t./checker 2 1 5 4\n\n\n5.\tPipe `push_swap` into `checker` with the same list of integers as argument to see if the output of `push_swap` correctly solved the problem\n\t\n\t\t./push_swap 2 1 5 4 | ./checker 2 1 5 4\n\n6. Use `checker` with `-v` option to visualize how push_swap solved the problem\n\t\n\t\t./push_swap 2 1 5 4 | ./checker -v 2 1 5 4\n   \n\n## Sources\n\n- [Quicksort algorithm explained step by step][7]\n- [Understanding sorting algorithms and complexity][6]\n- [Comprendre la notion de complexité (FR)][4]\n- [Visualization of 15 sorting algorithms][3]\n- [Explication de quelques algos de tri (FR)][5]\n- [Interesting to understang the basics of the project and the algos for stack of 3 / 5 numbers][1]\n- [Allows to visualize push_swap in action][2]\n\n[1]: https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a\n[2]: https://github.com/o-reo/push_swap_visualizer\n[3]: https://www.youtube.com/watch?v=kPRA0W1kECg\n[4]: https://zestedesavoir.com/tutoriels/621/algorithmique-pour-lapprenti-programmeur/399_presentation-de-la-notion-de-complexite-algorithmique/2020_la-notion-de-complexite/\n[5]: https://zestedesavoir.com/tutoriels/621/algorithmique-pour-lapprenti-programmeur/400_premiers-exemples-de-structures-de-donnees-et-dalgorithmes-courants/2024_introduction-au-probleme-du-tri/\n[6]: https://medium.com/jl-codes/understanding-sorting-algorithms-af6222995c8\n[7]: https://www.youtube.com/watch?v=7h1s2SojIRw\n[8]: https://github.com/llefranc/42_push_swap/blob/master/push_swap.en.pdf","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllefranc%2F42_push_swap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fllefranc%2F42_push_swap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllefranc%2F42_push_swap/lists"}