{"id":16791723,"url":"https://github.com/keivan-sf/insertion-sort-middle-tracking","last_synced_at":"2025-03-17T01:42:19.834Z","repository":{"id":205652490,"uuid":"710940987","full_name":"Keivan-sf/insertion-sort-middle-tracking","owner":"Keivan-sf","description":"Insertion-sort combined with middle-tracking to improve performance at certain scenarios","archived":false,"fork":false,"pushed_at":"2024-08-11T08:15:23.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T11:31:54.062Z","etag":null,"topics":["algorithm","insertion-sort","linkedlist"],"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/Keivan-sf.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":"2023-10-27T19:22:25.000Z","updated_at":"2024-08-11T08:15:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"ea9ebdec-cbc6-4d16-af09-b5994f1458ea","html_url":"https://github.com/Keivan-sf/insertion-sort-middle-tracking","commit_stats":null,"previous_names":["keivan-sf/insertion-sort-middle-tracking"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keivan-sf%2Finsertion-sort-middle-tracking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keivan-sf%2Finsertion-sort-middle-tracking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keivan-sf%2Finsertion-sort-middle-tracking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keivan-sf%2Finsertion-sort-middle-tracking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Keivan-sf","download_url":"https://codeload.github.com/Keivan-sf/insertion-sort-middle-tracking/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960449,"owners_count":20375101,"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":["algorithm","insertion-sort","linkedlist"],"created_at":"2024-10-13T08:35:41.567Z","updated_at":"2025-03-17T01:42:19.810Z","avatar_url":"https://github.com/Keivan-sf.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Insertion sort with middle tracking\n\nThe Idea comes from a practice in CLRS, stating that whether are we able to reduce time-complexity of insertion-sort by using binary-search instead of linear-search or not. The answer is of course, no.\nBut what if we could eliminate half of the sorted list? We could compare the element at hand to the middle element of the sorted list and perform insertion sort at **left-end** or **right-end** of the list depending on the comparison result. You might say this is not doable with array since we can't just shift the array to the right and add an element to the left side of it and you are correct, the cost is great. But we can use a linked list to sort the elements; Meaning we would have a linked list as output.\n\nSo for example if we have doubly-linked-list of the following sorted elements: `1-3-4-5-6` and we are willing to add `2` to it, we would compare `2` and the middle element (which we are keeping track of in every iteration). `2\u003c4` so we would perform insertion sort in reverse and import `2` at the head of the linked list, `2-1-3-4-5-6`. Then we would move `2` to the right until we hit its place (just like normal insertion sort, but reversed). `1-2-3-4-5-6`.\n\nThis saves a couple of steps when `n` is larger.\n\nA few tips about this algorithm:\n\n- The algorithm is no longer in-place since we are adding a linked list\n\n- Converting linked list to array is fairly cheap time-wise, so we could easily get an array output if memory is not an issue\n\n- Reversed/nearly-reversed lists are also a good scenario for this algorithm whereas in insertion-sort we had to tolerate `n^2`\n\n- Amount of steps in each iteration is increased so it will perform slightly worse in case of sorted/nearly-sorted arrays.\n\nI've run some clumsy benchmark on my pc with the n of `300,000`\n\n| Input/time     | Normal insertion-sort | With middle-tracking | Middle-tracking + converting to array |\n| -------------- | --------------------- | -------------------- | ------------------------------------- |\n| Reversed array | 107041 ms             | 13 ms                | 14 ms                                 |\n| Sorted array   | 1 ms                  | 11 ms                | 12 ms                                 |\n| Random array   | 6041 ms               | 6613 ms              | 6614 ms                               |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeivan-sf%2Finsertion-sort-middle-tracking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeivan-sf%2Finsertion-sort-middle-tracking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeivan-sf%2Finsertion-sort-middle-tracking/lists"}