{"id":20384323,"url":"https://github.com/gyakobo/sorting-algorithms","last_synced_at":"2026-02-14T00:03:00.540Z","repository":{"id":246814259,"uuid":"822253284","full_name":"Gyakobo/Sorting-Algorithms","owner":"Gyakobo","description":"This code example showcases 5 various algorithms (selection, insertion, heap, merge and quick sort) to sort a select array","archived":false,"fork":false,"pushed_at":"2024-11-12T15:04:32.000Z","size":25,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-14T13:43:53.047Z","etag":null,"topics":["heap-sort","insertion-sort","merge-sort","njit","quick-sort","selection-sort","sorting-algorithms"],"latest_commit_sha":null,"homepage":"","language":"Python","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/Gyakobo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2024-06-30T18:01:04.000Z","updated_at":"2024-12-09T19:36:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2c5f8c2-3c6a-44ad-aa37-56c53ebd7709","html_url":"https://github.com/Gyakobo/Sorting-Algorithms","commit_stats":null,"previous_names":["gyakobo/sorting-algorithms"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Gyakobo/Sorting-Algorithms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSorting-Algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSorting-Algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSorting-Algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSorting-Algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gyakobo","download_url":"https://codeload.github.com/Gyakobo/Sorting-Algorithms/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2FSorting-Algorithms/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29424602,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T22:20:51.549Z","status":"ssl_error","status_checked_at":"2026-02-13T22:20:49.838Z","response_time":78,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["heap-sort","insertion-sort","merge-sort","njit","quick-sort","selection-sort","sorting-algorithms"],"created_at":"2024-11-15T02:27:21.773Z","updated_at":"2026-02-14T00:03:00.512Z","avatar_url":"https://github.com/Gyakobo.png","language":"Python","readme":"# Sorting Algorithms\r\n\r\n![image](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge\u0026logo=python\u0026logoColor=blue)\r\n![image](https://img.shields.io/badge/windows%20terminal-4D4D4D?style=for-the-badge\u0026logo=windows%20terminal\u0026logoColor=white)\r\n\r\n\u003e[!IMPORTANT]\r\n\u003eNeedless to say, I run this program on an *AMD Ryzen 5 5625U with Radeon Graphics* and inevitably got not the best results in terms of performance. I however bid you my dear, visitor, to test out this project with better hardware and maybe commit to this project and add to my work.\r\n\r\nAuthor: [Andrew Gyakobo](https://github.com/Gyakobo)\r\n\r\n## Intro\r\n\r\nThis intro serves to test out 5 various sorting algorithms and possibly determing their big-Oh notation through this example.\r\n\r\n## Methodology\r\n\r\nFor starters, we start by importing all the necessary libraries and their respective methods. On a side note, I also add a bunch of ANSI sequence that will color code the console outputted text. By the way, if you're interested in color coding in python then please go through this [repo](https://github.com/Gyakobo/python-colored-console-output) I wrote.\r\n\r\n```python\r\nfrom random import randint\r\nimport time\r\nimport heapq\r\n\r\n# Colors\r\n# ANSI escape sequences for text colors\r\nRED = \"\\033[31m\"\r\nGREEN = \"\\033[32m\"\r\nYELLOW = \"\\033[33m\"\r\nBLUE = \"\\033[34m\"\r\nMAGENTA = \"\\033[35m\"\r\nCYAN = \"\\033[36m\"\r\nRESET = \"\\033[0m\"\r\n```\r\n\r\nAfterwards, we then write out the sorting algorithms:\r\n\r\n1) Both **Selection Sort** and **Insertion Sort** are $O(n^{2})$ algorithms, making them inefficient for large datasets. They work reasonably fast for small array but become impractically slow as the array size increases.\r\n\r\n```python\r\n# Selection Sort\r\ndef selection_sort(arr):\r\n    n = len(arr)\r\n\r\n    for i in range(n):\r\n        min_index = i\r\n\r\n        for j in range(i + 1, n):\r\n            if arr[j] \u003c arr[min_index]:\r\n                min_index = j\r\n        \r\n        arr[i], arr[min_index] = arr[min_index], arr[i]\r\n\r\n# Insertion Sort\r\ndef insertion_sort(arr):\r\n    for i in range(1, len(arr)):\r\n        key = arr[i]\r\n        j = i - 1\r\n        \r\n        while j \u003e= 0 and key \u003c arr[j]:\r\n            arr[j + 1] = arr[j]\r\n            j -= 1\r\n        arr[j + 1] = key\r\n```\r\n---\r\n\r\n2) **Heap Sort** has $O(n log n)$ time complexity, making it much more efficienct for larger datasets compared to selection and insertion sort. It performs consistently well but usually not as fast as quicksort.\r\n\r\n```python\r\n# Heap Sort\r\ndef heap_sort(arr):\r\n    n = len(arr)\r\n    heapq.heapify(arr)\r\n    sorted_arr = []\r\n    \r\n    for _ in range(n):\r\n        sorted_arr.append(heapq.heappop(arr))\r\n\r\n    arr[:] = sorted_arr \r\n```\r\n---\r\n\r\n3) **Merge Sort** also has $O(n log n)$ time complexity and provides stable sorting, but it requires additional space for merging, which could be a disadvantage in memory-constrained environments.\r\n\r\n```python\r\n# Merge Sort\r\ndef merge_sort(arr):\r\n    if len(arr) \u003e 1:\r\n        mid_point = len(arr) // 2\r\n        l = arr[:mid_point]\r\n        r = arr[mid_point:] \r\n\r\n        merge_sort(l) \r\n        merge_sort(r) \r\n\r\n        i = 0\r\n        j = 0\r\n        k = 0\r\n\r\n        while i \u003c len(l) and j \u003c len(r):\r\n            if l[i] \u003c r[j]:\r\n                arr[k] = l[i]\r\n                i += 1\r\n            else:\r\n                arr[k] = r[j]\r\n                j += 1\r\n            k += 1\r\n\r\n        while i \u003c len(l):\r\n            arr[k] = l[i]\r\n            i += 1\r\n            k += 1\r\n        \r\n        while j \u003c len(r):\r\n            arr[k] = r[j]\r\n            j += 1\r\n            k += 1\r\n```\r\n---\r\n\r\n4) **Quick Sort** is typically the fastest of the algorithms tested due to its $O(n log n)$ average-case time complexity, though it has a worst-case time complexity of $O(n^{2})$. It is efficient for large datasets, but care must be taken to avoid the worst-case scenario (e.g., using a good pivot strategy).\r\n\r\n```python\r\n# Quick Sort\r\ndef quick_sort(arr):\r\n    if len(arr) \u003c= 1:\r\n        return arr\r\n\r\n    pivot_point = arr[len(arr) // 2]\r\n    left    = [] \r\n    middle  = [] \r\n    right   = [] \r\n\r\n    for x in arr:\r\n        if x \u003c pivot_point:\r\n            left.append(x)\r\n    for x in arr:\r\n        if x == pivot_point:\r\n            middle.append(x)\r\n    for x in arr:\r\n        if x \u003e pivot_point:\r\n            right.append(x)\r\n\r\n    return quick_sort(left) + middle + quick_sort(right)\r\n```\r\n\r\n## Conclusion \u0026\u0026 Recommendations\r\n\r\n* For small arrays, *insertion sort* can be surprisingly efficient due to its simplicity.\r\n* For larger arrays, *quicksort* is generally the best choice if implemented with a good pivot strategy.\r\n* For applications requiring stable sorting or where memory usage is not a concern, *merge sort* is a good alternative.\r\n* *Heap sort* can be a good option for in-place sorting without additional memory requirements but is generally slower than quicksort.\r\n\r\nThese results highlight the importance of choosing the right sorting algorithm based on the specific requirements and constraints of the problem at hand. \r\n\r\n## License\r\nMIT\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fsorting-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyakobo%2Fsorting-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fsorting-algorithms/lists"}