{"id":27430376,"url":"https://github.com/yll0rd/sorting_algorithms","last_synced_at":"2025-04-14T14:43:10.198Z","repository":{"id":182817722,"uuid":"669106449","full_name":"yll0rd/sorting_algorithms","owner":"yll0rd","description":"Alx.","archived":false,"fork":false,"pushed_at":"2023-07-21T18:08:41.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-08T23:20:46.216Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/yll0rd.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}},"created_at":"2023-07-21T11:01:12.000Z","updated_at":"2024-04-08T23:20:49.783Z","dependencies_parsed_at":"2023-07-21T15:30:12.104Z","dependency_job_id":"fc03e509-b659-494d-a465-191e060f6445","html_url":"https://github.com/yll0rd/sorting_algorithms","commit_stats":null,"previous_names":["leo-youmbi/sorting_algorithms","yll0rd/sorting_algorithms"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yll0rd%2Fsorting_algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yll0rd%2Fsorting_algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yll0rd%2Fsorting_algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yll0rd%2Fsorting_algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yll0rd","download_url":"https://codeload.github.com/yll0rd/sorting_algorithms/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248898703,"owners_count":21179826,"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":[],"created_at":"2025-04-14T14:43:09.573Z","updated_at":"2025-04-14T14:43:10.186Z","avatar_url":"https://github.com/yll0rd.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 0x1B. C - Sorting algorithms \u0026 Big O\n\nIn this exercise questions I learend implementation of differnet algorithms wtih c.\n\n## Tasks\n\n\n### [0. Bubble sort](./0-bubble_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Bubble sort algorithm\n  - Prototype: `void bubble_sort(int *array, size_t size)`;\n  - You’re expected to print the array after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 0-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    bubble_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 0-bubble_sort.c 0-main.c print_array.c -o bubble\nalex@/tmp/sort$ ./bubble\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n19, 48, 71, 99, 13, 52, 96, 73, 86, 7\n19, 48, 71, 13, 99, 52, 96, 73, 86, 7\n19, 48, 71, 13, 52, 99, 96, 73, 86, 7\n19, 48, 71, 13, 52, 96, 99, 73, 86, 7\n19, 48, 71, 13, 52, 96, 73, 99, 86, 7\n19, 48, 71, 13, 52, 96, 73, 86, 99, 7\n19, 48, 71, 13, 52, 96, 73, 86, 7, 99\n19, 48, 13, 71, 52, 96, 73, 86, 7, 99\n19, 48, 13, 52, 71, 96, 73, 86, 7, 99\n19, 48, 13, 52, 71, 73, 96, 86, 7, 99\n19, 48, 13, 52, 71, 73, 86, 96, 7, 99\n19, 48, 13, 52, 71, 73, 86, 7, 96, 99\n19, 13, 48, 52, 71, 73, 86, 7, 96, 99\n19, 13, 48, 52, 71, 73, 7, 86, 96, 99\n13, 19, 48, 52, 71, 73, 7, 86, 96, 99\n13, 19, 48, 52, 71, 7, 73, 86, 96, 99\n13, 19, 48, 52, 7, 71, 73, 86, 96, 99\n13, 19, 48, 7, 52, 71, 73, 86, 96, 99\n13, 19, 7, 48, 52, 71, 73, 86, 96, 99\n13, 7, 19, 48, 52, 71, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [1. Insertion sort](./1-insertion_sort_list.c)\n\n- Write a function that sorts a doubly linked list of integers in ascending order using the Insertion sort algorithm\n  - Prototype: `void insertion_sort_list(listint_t **list)`;\n  - You are not allowed to modify the integer n of a node. You have to swap the nodes themselves.\n  - You’re expected to print the list after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 1-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * create_listint - Creates a doubly linked list from an array of integers\n *\n * @array: Array to convert to a doubly linked list\n * @size: Size of the array\n *\n * Return: Pointer to the first element of the created list. NULL on failure\n */\nlistint_t *create_listint(const int *array, size_t size)\n{\n    listint_t *list;\n    listint_t *node;\n    int *tmp;\n\n    list = NULL;\n    while (size--)\n    {\n        node = malloc(sizeof(*node));\n        if (!node)\n            return (NULL);\n        tmp = (int *)\u0026node-\u003en;\n        *tmp = array[size];\n        node-\u003enext = list;\n        node-\u003eprev = NULL;\n        list = node;\n        if (list-\u003enext)\n            list-\u003enext-\u003eprev = list;\n    }\n    return (list);\n}\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    listint_t *list;\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    list = create_listint(array, n);\n    if (!list)\n        return (1);\n    print_list(list);\n    printf(\"\\n\");\n    insertion_sort_list(\u0026list);\n    printf(\"\\n\");\n    print_list(list);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 1-main.c 1-insertion_sort_list.c print_list.c -o insertion\nalex@/tmp/sort$ ./insertion\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n19, 48, 71, 99, 13, 52, 96, 73, 86, 7\n19, 48, 71, 13, 99, 52, 96, 73, 86, 7\n19, 48, 13, 71, 99, 52, 96, 73, 86, 7\n19, 13, 48, 71, 99, 52, 96, 73, 86, 7\n13, 19, 48, 71, 99, 52, 96, 73, 86, 7\n13, 19, 48, 71, 52, 99, 96, 73, 86, 7\n13, 19, 48, 52, 71, 99, 96, 73, 86, 7\n13, 19, 48, 52, 71, 96, 99, 73, 86, 7\n13, 19, 48, 52, 71, 96, 73, 99, 86, 7\n13, 19, 48, 52, 71, 73, 96, 99, 86, 7\n13, 19, 48, 52, 71, 73, 96, 86, 99, 7\n13, 19, 48, 52, 71, 73, 86, 96, 99, 7\n13, 19, 48, 52, 71, 73, 86, 96, 7, 99\n13, 19, 48, 52, 71, 73, 86, 7, 96, 99\n13, 19, 48, 52, 71, 73, 7, 86, 96, 99\n13, 19, 48, 52, 71, 7, 73, 86, 96, 99\n13, 19, 48, 52, 7, 71, 73, 86, 96, 99\n13, 19, 48, 7, 52, 71, 73, 86, 96, 99\n13, 19, 7, 48, 52, 71, 73, 86, 96, 99\n13, 7, 19, 48, 52, 71, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [2. Selection sort](./2-selection_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Selection sort algorithm\n  - Prototype: `void selection_sort(int *array, size_t size)`;\n  - You’re expected to print the array after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 2-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    selection_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 2-main.c 2-selection_sort.c print_array.c -o select\nalex@/tmp/sort$ ./select\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n7, 48, 99, 71, 13, 52, 96, 73, 86, 19\n7, 13, 99, 71, 48, 52, 96, 73, 86, 19\n7, 13, 19, 71, 48, 52, 96, 73, 86, 99\n7, 13, 19, 48, 71, 52, 96, 73, 86, 99\n7, 13, 19, 48, 52, 71, 96, 73, 86, 99\n7, 13, 19, 48, 52, 71, 73, 96, 86, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [3. Quick sort](./3-quick_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Quick sort algorithm\n  - Prototype: `void quick_sort(int *array, size_t size)`;\n  - You must implement the Lomuto partition scheme.\n  - The pivot should always be the last element of the partition being sorted.\n  - You’re expected to print the array after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 3-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    quick_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 3-main.c 3-quick_sort.c print_array.c -o quick\nalex@/tmp/sort$ ./quick\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n7, 48, 99, 71, 13, 52, 96, 73, 86, 19\n7, 13, 99, 71, 48, 52, 96, 73, 86, 19\n7, 13, 19, 71, 48, 52, 96, 73, 86, 99\n7, 13, 19, 71, 48, 52, 73, 96, 86, 99\n7, 13, 19, 71, 48, 52, 73, 86, 96, 99\n7, 13, 19, 48, 71, 52, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [4. Shell sort - Knuth Sequence](./100-shell_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Shell sort algorithm, using the Knuth sequence\n  - Prototype: `void shell_sort(int *array, size_t size)`;\n  - You must use the following sequence of intervals (a.k.a the Knuth sequence):\n    - n+1 = n \\* 3 + 1\n    - 1, 4, 13, 40, 121, ...\n  - You’re expected to print the array each time you decrease the interval (See example below).\n\n```\nalex@/tmp/sort$ cat 100-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    shell_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 100-main.c 100-shell_sort.c print_array.c -o shell\nalex@/tmp/sort$ ./shell\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n13, 7, 96, 71, 19, 48, 99, 73, 86, 52\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [5. Cocktail shaker sort](./101-cocktail_sort_list.c)\n\n- Write a function that sorts a doubly linked list of integers in ascending order using the Cocktail shaker sort algorithm\n  - Prototype: `void cocktail_sort_list(listint_t **list)`;\n  - You are not allowed to modify the integer n of a node. You have to swap the nodes themselves.\n  - You’re expected to print the list after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 101-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * create_listint - Creates a doubly linked list from an array of integers\n *\n * @array: Array to convert to a doubly linked list\n * @size: Size of the array\n *\n * Return: Pointer to the first element of the created list. NULL on failure\n */\nlistint_t *create_listint(const int *array, size_t size)\n{\n    listint_t *list;\n    listint_t *node;\n    int *tmp;\n\n    list = NULL;\n    while (size--)\n    {\n        node = malloc(sizeof(*node));\n        if (!node)\n            return (NULL);\n        tmp = (int *)\u0026node-\u003en;\n        *tmp = array[size];\n        node-\u003enext = list;\n        node-\u003eprev = NULL;\n        list = node;\n        if (list-\u003enext)\n            list-\u003enext-\u003eprev = list;\n    }\n    return (list);\n}\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    listint_t *list;\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    list = create_listint(array, n);\n    if (!list)\n        return (1);\n    print_list(list);\n    printf(\"\\n\");\n    cocktail_sort_list(\u0026list);\n    printf(\"\\n\");\n    print_list(list);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 101-main.c 101-cocktail_sort_list.c print_list.c -o cocktail\nalex@/tmp/sort$ ./cocktail\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n19, 48, 71, 99, 13, 52, 96, 73, 86, 7\n19, 48, 71, 13, 99, 52, 96, 73, 86, 7\n19, 48, 71, 13, 52, 99, 96, 73, 86, 7\n19, 48, 71, 13, 52, 96, 99, 73, 86, 7\n19, 48, 71, 13, 52, 96, 73, 99, 86, 7\n19, 48, 71, 13, 52, 96, 73, 86, 99, 7\n19, 48, 71, 13, 52, 96, 73, 86, 7, 99\n19, 48, 71, 13, 52, 96, 73, 7, 86, 99\n19, 48, 71, 13, 52, 96, 7, 73, 86, 99\n19, 48, 71, 13, 52, 7, 96, 73, 86, 99\n19, 48, 71, 13, 7, 52, 96, 73, 86, 99\n19, 48, 71, 7, 13, 52, 96, 73, 86, 99\n19, 48, 7, 71, 13, 52, 96, 73, 86, 99\n19, 7, 48, 71, 13, 52, 96, 73, 86, 99\n7, 19, 48, 71, 13, 52, 96, 73, 86, 99\n7, 19, 48, 13, 71, 52, 96, 73, 86, 99\n7, 19, 48, 13, 52, 71, 96, 73, 86, 99\n7, 19, 48, 13, 52, 71, 73, 96, 86, 99\n7, 19, 48, 13, 52, 71, 73, 86, 96, 99\n7, 19, 13, 48, 52, 71, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [6. Counting sort](./102-counting_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Counting sort algorithm\n  - Prototype: `void counting_sort(int *array, size_t size)`;\n  - You can assume that array will contain only numbers \u003e= 0\n  - You are allowed to use malloc and free for this task\n  - You’re expected to print your counting array once it is set up (See example below)\n    - This array is of size k + 1 where k is the largest number in array\n\n```\nalex@/tmp/sort$ cat 102-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    counting_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 102-main.c 102-counting_sort.c print_array.c -o counting\nalex@/tmp/sort$ ./counting\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 10\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [7. Merge sort](./103-merge_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Merge sort algorithm\n  - Prototype: `void merge_sort(int *array, size_t size)`;\n  - You must implement the top-down merge sort algorithm\n    - When you divide an array into two sub-arrays, the size of the left array should always be \u003c= the size of the right array. i.e. {1, 2, 3, 4, 5} -\u003e {1, 2}, {3, 4, 5}\n    - Sort the left array before the right array\n  - You are allowed to use printf\n  - You are allowed to use malloc and free only once (only one call)\n  - Output: see example\n\n```\nalex@/tmp/sort$ cat 103-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    merge_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 103-main.c 103-merge_sort.c print_array.c -o merge\nalex@/tmp/sort$ ./merge\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\nMerging...\n[left]: 19\n[right]: 48\n[Done]: 19, 48\nMerging...\n[left]: 71\n[right]: 13\n[Done]: 13, 71\nMerging...\n[left]: 99\n[right]: 13, 71\n[Done]: 13, 71, 99\nMerging...\n[left]: 19, 48\n[right]: 13, 71, 99\n[Done]: 13, 19, 48, 71, 99\nMerging...\n[left]: 52\n[right]: 96\n[Done]: 52, 96\nMerging...\n[left]: 86\n[right]: 7\n[Done]: 7, 86\nMerging...\n[left]: 73\n[right]: 7, 86\n[Done]: 7, 73, 86\nMerging...\n[left]: 52, 96\n[right]: 7, 73, 86\n[Done]: 7, 52, 73, 86, 96\nMerging...\n[left]: 13, 19, 48, 71, 99\n[right]: 7, 52, 73, 86, 96\n[Done]: 7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [8. Heap sort](./104-heap_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Heap sort algorithm\n  - Prototype: `void heap_sort(int *array, size_t size)`;\n  - You must implement the sift-down heap sort algorithm\n  - You’re expected to print the array after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 104-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    heap_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 104-main.c 104-heap_sort.c print_array.c -o heap\nalex@/tmp/sort$ ./heap\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n19, 48, 99, 86, 13, 52, 96, 73, 71, 7\n19, 86, 99, 48, 13, 52, 96, 73, 71, 7\n19, 86, 99, 73, 13, 52, 96, 48, 71, 7\n99, 86, 19, 73, 13, 52, 96, 48, 71, 7\n99, 86, 96, 73, 13, 52, 19, 48, 71, 7\n7, 86, 96, 73, 13, 52, 19, 48, 71, 99\n96, 86, 7, 73, 13, 52, 19, 48, 71, 99\n96, 86, 52, 73, 13, 7, 19, 48, 71, 99\n71, 86, 52, 73, 13, 7, 19, 48, 96, 99\n86, 71, 52, 73, 13, 7, 19, 48, 96, 99\n86, 73, 52, 71, 13, 7, 19, 48, 96, 99\n48, 73, 52, 71, 13, 7, 19, 86, 96, 99\n73, 48, 52, 71, 13, 7, 19, 86, 96, 99\n73, 71, 52, 48, 13, 7, 19, 86, 96, 99\n19, 71, 52, 48, 13, 7, 73, 86, 96, 99\n71, 19, 52, 48, 13, 7, 73, 86, 96, 99\n71, 48, 52, 19, 13, 7, 73, 86, 96, 99\n7, 48, 52, 19, 13, 71, 73, 86, 96, 99\n52, 48, 7, 19, 13, 71, 73, 86, 96, 99\n13, 48, 7, 19, 52, 71, 73, 86, 96, 99\n48, 13, 7, 19, 52, 71, 73, 86, 96, 99\n48, 19, 7, 13, 52, 71, 73, 86, 96, 99\n13, 19, 7, 48, 52, 71, 73, 86, 96, 99\n19, 13, 7, 48, 52, 71, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n13, 7, 19, 48, 52, 71, 73, 86, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [9. Radix sort](./105-radix_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Radix sort algorithm\n  - Prototype: `void radix_sort(int *array, size_t size)`;\n  - You must implement the LSD radix sort algorithm\n  - You can assume that array will contain only numbers \u003e= 0\n  - You are allowed to use malloc and free for this task\n  - You’re expected to print the array each time you increase your significant digit (See example below)\n\n```\nalex@/tmp/sort$ cat 105-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    radix_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 105-main.c 105-radix_sort.c print_array.c -o radix\nalex@/tmp/sort$ ./radix\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n71, 52, 13, 73, 96, 86, 7, 48, 19, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [10. Bitonic sort](./106-bitonic_sort.c)\n\n- Write a function that sorts an array of integers in ascending order using the Bitonic sort algorithm\n  - Prototype: `void bitonic_sort(int *array, size_t size)`;\n  - You can assume that size will be equal to 2^k, where k \u003e= 0 (when array is not NULL …)\n  - You are allowed to use printf\n  - You’re expected to print the array each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 106-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {100, 93, 40, 57, 14, 58, 85, 54, 31, 56, 46, 39, 15, 26, 78, 13};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    bitonic_sort(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 106-main.c 106-bitonic_sort.c print_array.c -o bitonic\nalex@/tmp/sort$ ./bitonic\n100, 93, 40, 57, 14, 58, 85, 54, 31, 56, 46, 39, 15, 26, 78, 13\n\nMerging [16/16] (UP):\n100, 93, 40, 57, 14, 58, 85, 54, 31, 56, 46, 39, 15, 26, 78, 13\nMerging [8/16] (UP):\n100, 93, 40, 57, 14, 58, 85, 54\nMerging [4/16] (UP):\n100, 93, 40, 57\nMerging [2/16] (UP):\n100, 93\nResult [2/16] (UP):\n93, 100\nMerging [2/16] (DOWN):\n40, 57\nResult [2/16] (DOWN):\n57, 40\nResult [4/16] (UP):\n40, 57, 93, 100\nMerging [4/16] (DOWN):\n14, 58, 85, 54\nMerging [2/16] (UP):\n14, 58\nResult [2/16] (UP):\n14, 58\nMerging [2/16] (DOWN):\n85, 54\nResult [2/16] (DOWN):\n85, 54\nResult [4/16] (DOWN):\n85, 58, 54, 14\nResult [8/16] (UP):\n14, 40, 54, 57, 58, 85, 93, 100\nMerging [8/16] (DOWN):\n31, 56, 46, 39, 15, 26, 78, 13\nMerging [4/16] (UP):\n31, 56, 46, 39\nMerging [2/16] (UP):\n31, 56\nResult [2/16] (UP):\n31, 56\nMerging [2/16] (DOWN):\n46, 39\nResult [2/16] (DOWN):\n46, 39\nResult [4/16] (UP):\n31, 39, 46, 56\nMerging [4/16] (DOWN):\n15, 26, 78, 13\nMerging [2/16] (UP):\n15, 26\nResult [2/16] (UP):\n15, 26\nMerging [2/16] (DOWN):\n78, 13\nResult [2/16] (DOWN):\n78, 13\nResult [4/16] (DOWN):\n78, 26, 15, 13\nResult [8/16] (DOWN):\n78, 56, 46, 39, 31, 26, 15, 13\nResult [16/16] (UP):\n13, 14, 15, 26, 31, 39, 40, 46, 54, 56, 57, 58, 78, 85, 93, 100\n\n13, 14, 15, 26, 31, 39, 40, 46, 54, 56, 57, 58, 78, 85, 93, 100\n```\n\n### [11. Quick Sort - Hoare Partition scheme](./107-quick_sort_hoare.c)\n\n- Write a function that sorts an array of integers in ascending order using the Quick sort algorithm\n  - Prototype: `void quick_sort_hoare(int *array, size_t size)`;\n  - You must implement the Hoare partition scheme.\n  - The pivot should always be the last element of the partition being sorted.\n  - You’re expected to print the array after each time you swap two elements (See example below)\n\n```\nalex@/tmp/sort$ cat 107-main.c\n```\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"sort.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0\n */\nint main(void)\n{\n    int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};\n    size_t n = sizeof(array) / sizeof(array[0]);\n\n    print_array(array, n);\n    printf(\"\\n\");\n    quick_sort_hoare(array, n);\n    printf(\"\\n\");\n    print_array(array, n);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 107-main.c 107-quick_sort_hoare.c print_array.c -o quick\nalex@/tmp/sort$ ./quick\n19, 48, 99, 71, 13, 52, 96, 73, 86, 7\n\n7, 48, 99, 71, 13, 52, 96, 73, 86, 19\n7, 19, 99, 71, 13, 52, 96, 73, 86, 48\n7, 19, 13, 71, 99, 52, 96, 73, 86, 48\n7, 13, 19, 71, 99, 52, 96, 73, 86, 48\n7, 13, 19, 48, 99, 52, 96, 73, 86, 71\n7, 13, 19, 48, 71, 52, 96, 73, 86, 99\n7, 13, 19, 48, 52, 71, 96, 73, 86, 99\n7, 13, 19, 48, 52, 71, 86, 73, 96, 99\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n\n7, 13, 19, 48, 52, 71, 73, 86, 96, 99\n```\n\n### [12. Dealer](./1000-sort_deck.c)\n\n- Write a function that sorts a deck of cards.\n  - Prototype: void sort_deck(deck_node_t \\*\\*deck);\n  - You are allowed to use the C standard library function qsort\n  - Please use the following data structures:\n\n```c\ntypedef enum kind_e\n{\n    SPADE = 0,\n    HEART,\n    CLUB,\n    DIAMOND\n} kind_t;\n\n/**\n * struct card_s - Playing card\n *\n * @value: Value of the card\n * From \"Ace\" to \"King\"\n * @kind: Kind of the card\n */\ntypedef struct card_s\n{\n    const char *value;\n    const kind_t kind;\n} card_t;\n\n/**\n * struct deck_node_s - Deck of card\n *\n * @card: Pointer to the card of the node\n * @prev: Pointer to the previous node of the list\n * @next: Pointer to the next node of the list\n */\ntypedef struct deck_node_s\n{\n    const card_t *card;\n    struct deck_node_s *prev;\n    struct deck_node_s *next;\n} deck_node_t;\n```\n\n- Each node of the doubly linked list contains a card that you cannot modify. You have to swap the nodes.\n- You can assume there is exactly 52 elements in the doubly linked list.\n- You are free to use the sorting algorithm of your choice\n- The deck must be ordered:\n  - From Ace to King\n  - From Spades to Diamonds\n\n```\nalex@/tmp/sort$ cat 1000-main.c\n```\n\n```c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"deck.h\"\n\nvoid print_deck(const deck_node_t *deck)\n{\n    size_t i;\n    char kinds[4] = {'S', 'H', 'C', 'D'};\n\n    i = 0;\n    while (deck)\n    {\n        if (i)\n            printf(\", \");\n        printf(\"{%s, %c}\", deck-\u003ecard-\u003evalue, kinds[deck-\u003ecard-\u003ekind]);\n        if (i == 12)\n            printf(\"\\n\");\n        i = (i + 1) % 13;\n        deck = deck-\u003enext;\n    }\n}\n\ndeck_node_t *init_deck(const card_t cards[52])\n{\n    deck_node_t *deck;\n    deck_node_t *node;\n    size_t i;\n\n    i = 52;\n    deck = NULL;\n    while (i--)\n    {\n        node = malloc(sizeof(*node));\n        if (!node)\n            return (NULL);\n        node-\u003ecard = \u0026cards[i];\n        node-\u003enext = deck;\n        node-\u003eprev = NULL;\n        if (deck)\n            deck-\u003eprev = node;\n        deck = node;\n    }\n    return (deck);\n}\n\nint main(void)\n{\n    card_t cards[52] = {\n        {\"Jack\", CLUB}, {\"4\", HEART}, {\"3\", HEART}, {\"3\", DIAMOND}, {\"Queen\", HEART}, {\"5\", HEART}, {\"5\", SPADE}, {\"10\", HEART}, {\"6\", HEART}, {\"5\", DIAMOND}, {\"6\", SPADE}, {\"9\", HEART}, {\"7\", DIAMOND}, {\"Jack\", SPADE}, {\"Ace\", DIAMOND}, {\"9\", CLUB}, {\"Jack\", DIAMOND}, {\"7\", SPADE}, {\"King\", DIAMOND}, {\"10\", CLUB}, {\"King\", SPADE}, {\"8\", CLUB}, {\"9\", SPADE}, {\"6\", CLUB}, {\"Ace\", CLUB}, {\"3\", SPADE}, {\"8\", SPADE}, {\"9\", DIAMOND}, {\"2\", HEART}, {\"4\", DIAMOND}, {\"6\", DIAMOND}, {\"3\", CLUB}, {\"Queen\", CLUB}, {\"10\", SPADE}, {\"8\", DIAMOND}, {\"8\", HEART}, {\"Ace\", SPADE}, {\"Jack\", HEART}, {\"2\", CLUB}, {\"4\", SPADE}, {\"2\", SPADE}, {\"2\", DIAMOND}, {\"King\", CLUB}, {\"Queen\", SPADE}, {\"Queen\", DIAMOND}, {\"7\", CLUB}, {\"7\", HEART}, {\"5\", CLUB}, {\"10\", DIAMOND}, {\"4\", CLUB}, {\"King\", HEART}, {\"Ace\", HEART},\n    };\n    deck_node_t *deck;\n\n    deck = init_deck(cards);\n    print_deck(deck);\n    printf(\"\\n\");\n    sort_deck(\u0026deck);\n    printf(\"\\n\");\n    print_deck(deck);\n    return (0);\n}\n```\n\n```\nalex@/tmp/sort$ gcc -Wall -Wextra -Werror -pedantic 1000-main.c 1000-sort_deck.c -o deck\nalex@/tmp/sort$ ./deck\n{Jack, C}, {4, H}, {3, H}, {3, D}, {Queen, H}, {5, H}, {5, S}, {10, H}, {6, H}, {5, D}, {6, S}, {9, H}, {7, D}\n{Jack, S}, {Ace, D}, {9, C}, {Jack, D}, {7, S}, {King, D}, {10, C}, {King, S}, {8, C}, {9, S}, {6, C}, {Ace, C}, {3, S}\n{8, S}, {9, D}, {2, H}, {4, D}, {6, D}, {3, C}, {Queen, C}, {10, S}, {8, D}, {8, H}, {Ace, S}, {Jack, H}, {2, C}\n{4, S}, {2, S}, {2, D}, {King, C}, {Queen, S}, {Queen, D}, {7, C}, {7, H}, {5, C}, {10, D}, {4, C}, {King, H}, {Ace, H}\n\n\n{Ace, S}, {2, S}, {3, S}, {4, S}, {5, S}, {6, S}, {7, S}, {8, S}, {9, S}, {10, S}, {Jack, S}, {Queen, S}, {King, S}\n{Ace, H}, {2, H}, {3, H}, {4, H}, {5, H}, {6, H}, {7, H}, {8, H}, {9, H}, {10, H}, {Jack, H}, {Queen, H}, {King, H}\n{Ace, C}, {2, C}, {3, C}, {4, C}, {5, C}, {6, C}, {7, C}, {8, C}, {9, C}, {10, C}, {Jack, C}, {Queen, C}, {King, C}\n{Ace, D}, {2, D}, {3, D}, {4, D}, {5, D}, {6, D}, {7, D}, {8, D}, {9, D}, {10, D}, {Jack, D}, {Queen, D}, {King, D}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyll0rd%2Fsorting_algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyll0rd%2Fsorting_algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyll0rd%2Fsorting_algorithms/lists"}