{"id":43459968,"url":"https://github.com/drminnaar/algorithms-and-data-structures","last_synced_at":"2026-02-03T05:27:58.099Z","repository":{"id":42238386,"uuid":"134303710","full_name":"drminnaar/algorithms-and-data-structures","owner":"drminnaar","description":"This project is a compilation of various algorithms and data structures that have been implemented using C# and Javascript.","archived":false,"fork":false,"pushed_at":"2023-03-04T02:41:05.000Z","size":672,"stargazers_count":12,"open_issues_count":4,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-14T10:05:53.920Z","etag":null,"topics":["algorithms","csharp","datastructures","javascript"],"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/drminnaar.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}},"created_at":"2018-05-21T17:41:10.000Z","updated_at":"2023-10-02T19:22:00.000Z","dependencies_parsed_at":"2023-02-06T11:16:26.983Z","dependency_job_id":null,"html_url":"https://github.com/drminnaar/algorithms-and-data-structures","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/drminnaar/algorithms-and-data-structures","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drminnaar%2Falgorithms-and-data-structures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drminnaar%2Falgorithms-and-data-structures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drminnaar%2Falgorithms-and-data-structures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drminnaar%2Falgorithms-and-data-structures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drminnaar","download_url":"https://codeload.github.com/drminnaar/algorithms-and-data-structures/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drminnaar%2Falgorithms-and-data-structures/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29034090,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T02:28:16.591Z","status":"ssl_error","status_checked_at":"2026-02-03T02:27:48.904Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["algorithms","csharp","datastructures","javascript"],"created_at":"2026-02-03T05:27:57.547Z","updated_at":"2026-02-03T05:27:58.090Z","avatar_url":"https://github.com/drminnaar.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"![cover](https://repository-images.githubusercontent.com/134303710/eb049280-a362-11eb-86d1-bb0c08ef19e4)\n\n# Algorithms and Data Structures\n\nThis project is a compilation of various algorithms and data structures that have been implemented using *C#* and *Javascript*.\n\n---\n\n## Overview\n\nThe idea behind this project is to re-visit the fundamentals of computer programming. These fundamentals will be explored using 2 of my favourite programming languages, namely *C#* and *Javascript*. The primary aim is to develop an aptitude to think differently about solving problems. Also, by understanding these fundamentals, one can begin to think about solving problems more efficiently.\n\n---\n\n## Projects By Programming Language\n\n* [C# Algorithms and Data Structures]\n\n* [Javascript algorithms and data structures]\n\n---\n\n## Contents\n\n* [Algorithms](#algorithms)\n  * [Sorting Algorithms](#sorting-algorithms)\n    * [Bubble Sort](#bubble-sort)\n    * [Selection Sort](#selection-sort)\n    * [Insertion Sort](#insertion-sort)\n    * [Shell Sort](#shell-sort)\n    * [Merge Sort](#merge-sort)\n    * [Quick Sort](#quick-sort)\n\n* [Data Structures](#data-structures)\n  * [Singly Linked List](#singly-linked-list)\n  * [Doubly Linked List](#doubly-linked-list)\n\n---\n\n## Algorithms\n\n### Sorting Algorithms\n\n#### Bubble Sort\n\nA _Bubble Sort_ algorithm is a comparison based sorting algorithm that sorts a list of items by repeatedly comparing and swapping adjacent elements until all elements in the list are sorted in ascending order. Because the elements with the largest values are \"bubbled\" to the end of the list (from left to right), the sorted partition is growing from right to left.\n\nIt is Big-O (n\u003csup\u003e2\u003c/sup\u003e) in terms of time complexity, where n is the number of items.\n\nExample:\n\n```javascript\nFirst Pass:\n\n[5, 1, 4, 3, 2] -\u003e [1, 5, 4, 3, 2] | 5 \u003e 1 ? swap\n[1, 5, 4, 3, 2] -\u003e [1, 4, 5, 3, 2] | 5 \u003e 4 ? swap\n[1, 4, 5, 3, 2] -\u003e [1, 4, 3, 5, 2] | 5 \u003e 3 ? swap\n[1, 4, 3, 5, 2] -\u003e [1, 4, 3, 2, 5] | 5 \u003e 2 ? swap\n```\n\n```javascript\nSecond Pass:\n\n[1, 4, 3, 2, 5] -\u003e [1, 4, 3, 2, 5] | 1 \u003c 4 ? continue\n[1, 4, 3, 2, 5] -\u003e [1, 3, 4, 2, 5] | 4 \u003e 3 ? swap\n[1, 3, 4, 2, 5] -\u003e [1, 3, 2, 4, 5] | 4 \u003e 2 ? swap\n[1, 3, 2, 4, 5] -\u003e [1, 3, 2, 4, 5] | 4 \u003c 5 ? continue\n```\n\n```javascript\nThird Pass:\n\n[1, 3, 2, 4, 5] -\u003e [1, 3, 2, 4, 5] | 1 \u003c 3 ? continue\n[1, 3, 2, 4, 5] -\u003e [1, 2, 3, 4, 5] | 3 \u003e 2 ? swap\n[1, 2, 3, 4, 5] -\u003e [1, 2, 3, 4, 5] | 3 \u003c 4 ? continue\n[1, 2, 3, 4, 5] -\u003e [1, 2, 3, 4, 5] | 4 \u003c 5 ? continue\n```\n\n##### Bubble Sort Examples\n\n[Bubble Sort with Hungarian (\"Csángó\") folk dance](http://www.youtube.com/watch?v=lyZQPjUT5B4)\n\n[![Bubble Sort with Hungarian (Csango) folk dance](http://img.youtube.com/vi/lyZQPjUT5B4/0.jpg)](http://www.youtube.com/watch?v=lyZQPjUT5B4 \"Bubble Sort with Hungarian (Csango) folk dance\")\n\n##### Bubble Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/BubbleSort.cs)\n\n```csharp\nC#\n\npublic void Sort(int[] list)\n{\n    for (int lastUnsortedIndex = list.Length - 1; lastUnsortedIndex \u003e 0; lastUnsortedIndex--)\n    {\n        for(int i = 0; i \u003c lastUnsortedIndex; i++)\n        {\n            if (list[i] \u003e list[i + 1])\n            {\n                // swap\n                var temp = list[i + 1];\n                list[i + 1] = list[i];\n                list[i] = temp;\n            }\n        }\n    }\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/bubbleSort/bubbleSort.js)\n\n```javascript\nJavascript\n\nfunction sort(list) {\n    for (let lastUnsortedIndex = list.length - 1; lastUnsortedIndex \u003e 0; lastUnsortedIndex--) {\n        for (let i = 0; i \u003c lastUnsortedIndex; i++) {\n            if (list[i] \u003e list[i + 1]) {\n                // swap\n                const temp = list[i + 1];\n                list[i + 1] = list[i];\n                list[i] = temp;\n            }\n        }\n    }\n\n    return list;\n}\n```\n\n#### Selection Sort\n\nA _Selection Sort_ algorithm is a comparison based sorting algorithm where a list is divided into two parts (sorted and unsorted). It sorts a list of items by repeatedly finding the element with the largest value in the unsorted part of list, and placing it at the end in the sorted part of the list through the process of repeatedly swapping. Initially, the entire list is unsorted. However, with each iteration, the sorted part of the list grows whilst the unsorted part of the list shrinks until all that is left is a sorted partition, and hence a sorted list. In other words, the sorted partition grows from right to left with each iteration of the algorithm.\n\nIt is Big-O (n\u003csup\u003e2\u003c/sup\u003e) in terms of time complexity, where n is the number of items.\n\n```javascript\nFirst Pass:\n\n- Find index with highest value (5) in unsorted parition, and move to last unsorted index (4)\n\n[5, 1, 4, 3, 2] -\u003e [2, 1, 4, 3, 5]\n\n- Decrement last unsorted index to 3\n```\n\n```javascript\nSecond Pass:\n\n- Find index with highest value (4) in unsorted partition, and move to last unsorted index (3)\n\n[2, 1, 4, 3, 5] -\u003e [2, 1, 3, 4, 5]\n\n- Decrement last unsorted index to 2\n```\n\n```javascript\nThird Pass:\n\n- Find index with highest value (3) in unsorted partition, and move to last unsorted index (2)\n\n[2, 1, 3, 4, 5] -\u003e [2, 1, 3, 4, 5]\n\n- Decrement last unsorted index to 1\n```\n\n```javascript\nFourth Pass:\n\n- Find index with highest value (2) in unsorted partition, and move to last unsorted index (1)\n\n[2, 1, 3, 4, 5] -\u003e [1, 2, 3, 4, 5]\n\n- Decrement last unsorted index to 0\n```\n\n##### Selection Sort Examples\n\n[Select Sort with Gypsy folk dance](http://www.youtube.com/watch?v=Ns4TPTC8whw )\n\n[![Select Sort with Gypsy folk dance](http://img.youtube.com/vi/Ns4TPTC8whw/0.jpg)](http://www.youtube.com/watch?v=Ns4TPTC8whw \"Select Sort with Gypsy folk dance\")\n\n##### Selection Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/SelectionSort.cs)\n\n```csharp\nC#\n\npublic void Sort(int[] list)\n{\n    var maxValueIndex = 0;\n\n    for (int lastUnsortedIndex = (list.Length - 1); lastUnsortedIndex \u003e 0; lastUnsortedIndex--)\n    {\n        maxValueIndex = lastUnsortedIndex;\n\n        for (int i = 0; i \u003c= lastUnsortedIndex; i++)\n        {\n            if (list[i] \u003e list[maxValueIndex])\n            {\n                maxValueIndex = i;\n            }\n        }\n\n        // swap\n        var temp = list[lastUnsortedIndex];\n        list[lastUnsortedIndex] = list[maxValueIndex];\n        list[maxValueIndex] = temp;\n    }\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/selectionSort/selectionSort.js)\n\n```javascript\nJavascript\n\nfunction sort(list) {\n    let maxValueIndex = 0;\n\n    for (let lastUnsortedIndex = (list.length - 1); lastUnsortedIndex \u003e 0; lastUnsortedIndex--) {\n        maxValueIndex = lastUnsortedIndex;\n\n        for (let i = 0; i \u003c= lastUnsortedIndex; i++) {\n            if (list[i] \u003e list[maxValueIndex]) {\n                maxValueIndex = i;\n            }\n        }\n\n        // swap\n        const temp = list[lastUnsortedIndex];\n        list[lastUnsortedIndex] = list[maxValueIndex];\n        list[maxValueIndex] = temp;\n    }\n\n    return list;\n}\n```\n\n#### Insertion Sort\n\nAn _Insertion Sort_ algorithm is a comparison based sorting algorithm where a list is divided into two parts (sorted and unsorted). The list is repeatedly sorted by growing the sorted partition from the left until only the sorted parition remains. It's sort of like being dealt a card from a randomly arranged deck of cards. For each card that is dealt, you take that card and insert it into a sorted position in the cards that you hold. The deck of randomly arranged cards represents the unsorted partition whereas the cards that are held by you represent the sorted partition.\n\nThe way _Insertion Sort_ works is as follows:\n\n* It starts out with position 0 representing the sorted partition\n* Position 1 represents the first element of the unsorted partition\n* For each pass of the algorithm, the first unsorted element of the unsorted partition is compared with each element in the sorted parition until it can be inserted into it's sorted position. This process continues until there are no more elements left in the unsorted partition and all that is left is the sorted parition.\n\nFor example:\n\nWe start off with an unsorted list of characters ([d, c, b, a]) as follows:\n\nposition | 0 | 1 | 2 | 3\n--- | --- | --- | --- | ---\nelements | d | c | b | a\n\n```javascript\nFirst pass:\n\nSorted Parition = [ d ]\nUnsorted Partition = [ c, b, a ]\nkey = element at position 1 = c\n\nk \u003c= d  then  list[1] = d  and  list[0] = c\n```\n\nAfter the first pass, our list looks as follows:\n\nposition |0 | 1 | 2 | 3\n--- |--- | --- | --- | ---\nelements |c | d | b | a\n\n```javascript\nSecond pass:\n\nSorted Parition = [ c, d ]\nUnsorted Partition = [ b, a ]\nkey = element at position 2 = b\n\nk \u003c= d  then  list[2] = d  and  list[1] = b\nk \u003c= c  then  list[1] = c  and  list[0] = b\n```\n\nAfter the second pass, our list looks as follows:\n\nposition |0 | 1 | 2 | 3\n--- |--- | --- | --- | ---\nelements |b | c | d | a\n\n```javascript\nThird pass:\n\nSorted Parition = [ b, c, d ]\nUnsorted Partition = [ a ]\nkey = element at position 3 = a\n\nk \u003c= d  then  list[3] = d  and  list[2] = a\nk \u003c= c  then  list[2] = c  and  list[1] = a\nk \u003c= b  then  list[1] = b  and  list[0] = a\n```\n\nAfter the third pass, our list looks as follows:\n\nposition |0 | 1 | 2 | 3\n--- |--- | --- | --- | ---\nelements |a |b | c | d\n\n##### Insertion Sort Examples\n\n[Insertion Sort with Romanian folk dance](http://www.youtube.com/watch?v=ROalU379l3U)\n\n[![Insertion Sort with Romanian folk dance](http://img.youtube.com/vi/ROalU379l3U/0.jpg)](http://www.youtube.com/watch?v=ROalU379l3U \"Insertion Sort with Romanian folk dance\")\n\n##### Insertion Sort Time and Complexity\n\n* Worst Case Time Complexity [ Big-O ]: O(n\u003csup\u003e2\u003c/sup\u003e)\n* Best Case Time Complexity [Big-omega]: O(n)\n* Average Time Complexity [Big-theta]: O(n\u003csup\u003e2\u003c/sup\u003e)\n* Space Complexity: O(1)\n\n##### Insertion Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/InsertionSort.cs)\n\n```csharp\npublic void Sort(int[] list)\n{\n    var k = 0;\n    var i = 0;\n\n    for (var firstUnsortedIndex = 1; firstUnsortedIndex \u003c list.Length; firstUnsortedIndex++)\n    {\n        k = list[firstUnsortedIndex];\n        i = firstUnsortedIndex - 1;\n\n        while (i \u003e= 0 \u0026\u0026 k \u003c list[i])\n        {\n            // swap\n            var temp = list[i + 1];\n            list[i + 1] = list[i];\n            list[i] = temp;\n\n            i--;\n        }\n    }\n\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/insertionSort/insertionSort.js)\n\n```javascript\nJavascript\n\nfunction sort(list) {\n    let key = 0;\n    let i = 0;\n\n    for (let firstUnsortedIndex = 1; firstUnsortedIndex \u003c list.length; firstUnsortedIndex++) {\n        key = list[firstUnsortedIndex];\n        i = firstUnsortedIndex - 1;\n\n        while (i \u003e= 0 \u0026\u0026 key \u003c list[i]) {\n            // swap\n            const temp = list[i + 1];\n            list[i + 1] = list[i];\n            list[i] = temp;\n\n            i--;\n        }\n    }\n\n    return list;\n}\n```\n\n#### Shell Sort\n\nIn my explanation of an _Insertion Sort_ algorithm, I use the analogy of a dealer dealing out cards from an unsorted deck of cards where one takes each card and inserts it in it's appropriate position based on rank. The same analogy can be used to explain _Shell Sort_. Except now imagine that the dealer took his deck of cards and partially sorted it so that all the cards having the lowest values are on one side of the deck, and the cards having the largest values are on the opposite side of the deck. Therefore, when one is dealt a card, it is coming from a partially sorted deck of cards.\n\nTherefore, a _Shell Sort_ algorithm can be summarised as follows:\n\n* It's a variation of the _Insertion Sort_ algorithm\n* Therefore, _Shell Sort_ is a also comparison based sorting algorithm. However, unlike _Insertion Sort_, it is not a stable sorting algorithm.\n* Due to the introduction of a _gap_ value, elements that are distant from each other (based on gap) are compared to each other as opposed to adjacent elements.\n* Once the _Shell Sort_ algorithm reaches a _gap_ value of 1, it is equivalent to an _Insertion Sort_.\n* The reason for introducing a _gap_ value that is greater than 1, is to reduce the number of _shifting_ that occurs in a standard _Insertion Sort_ algorithm. Therefore, by the time the _Shell Sort_ algorithm reaches a value of 1, it is partially sorted.\n\nThe way _Shell Sort_ works is as follows:\n\n* Where _Insertion Sort_ chooses an element to insert using a _gap_ of 1, _Shell Sort_ starts out using a larger _gap_.\n* The _gap_ value is determined based on a _\"strategy\"_ that helps calculate a value that provides the lowest time complexity. A common _strategy_ that is used in many _Shell Sort_ algorithm examples, is to calculate the _gap_ value by initially dividing the list length by 2. Then with each pass, the _gap_ value is divided by 2 until the _gap_ is 1\n* Once a _gap_ value of 1 is reached, the sort algorithm is equivalent to an _Insertion Sort_ algorithm.\n\n##### Shell Sort Examples\n\n[Shell Sort with Hungarian (Székely) folk dance](http://www.youtube.com/watch?v=CmPA7zE8mx0)\n\n[![Shell Sort with Hungarian (Székely) folk dance](http://img.youtube.com/vi/CmPA7zE8mx0/0.jpg)](http://www.youtube.com/watch?v=CmPA7zE8mx0 \"Shell Sort with Hungarian (Székely) folk dance\")\n\n##### Shell Sort Time and Complexity\n\n* Worst Case Time Complexity [ Big-O ]: O(n\u003csup\u003e2\u003c/sup\u003e)\n* Best Case Time Complexity [Big-omega]: O(n(log(n))\u003csup\u003e2\u003c/sup\u003e)\n* Average Time Complexity [Big-theta]: O(n(log(n))\u003csup\u003e2\u003c/sup\u003e)\n* Space Complexity: O(1)\n\n##### Shell Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/ShellSort.cs)\n\n```csharp\npublic void Sort(int[] list)\n{\n    var i = 0;\n    var j = 0;\n    var key = 0;\n\n    for (int gap = list.Length / 2; gap \u003e 0; gap /= 2)\n    {\n        for (i = gap; i \u003c list.Length; i++)\n        {\n            key = list[i];\n            j = i;\n\n            while (j \u003e= gap \u0026\u0026 key \u003c list[j - gap])\n            {\n                // swap\n                var temp = list[j - gap];\n                list[j - gap] = list[j];\n                list[j] = temp;\n\n                j -= gap;\n            }\n        }\n    }\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/shellSort/shellSort.js)\n\n```javascript\nJavascript\n\nfunction sort(list) {\n    let i = 0;\n    let j = 0;\n    let key = 0;\n\n    for (let gap = list.length / 2; gap \u003e 0; gap /= 2)\n    {\n        for (i = gap; i \u003c list.length; i++)\n        {\n            key = list[i];\n            j = i;\n\n            while (j \u003e= gap \u0026\u0026 key \u003c list[j - gap])\n            {\n                // swap\n                var temp = list[j - gap];\n                list[j - gap] = list[j];\n                list[j] = temp;\n\n                j -= gap;\n            }\n        }\n    }\n\n    return list;\n}\n```\n\n#### Merge Sort\n\nThe _Merge Sort_ algorithm is a comparison based sorting algorithm that can be summarised as follows:\n\n* It is a divide and conquer algorithm\n* Typically implemented as a recursive algorithm\n* Primary made up of 2 phases namely a splitting and merging phase\n\nThe _Merge Sort_ algorithm repeatedly splits a list unti all that remains is a collection of 1 element lists. A 1 element list is sorted due to there only being 1 element. Next, the _Merge Sort_ algorithm repeatedly merges all the sub-lists until all that remains is the sorted list.\n\n##### Merge Sort Examples\n\n[Merge Sort Algorithm. (n.d). In Wikipedia](https://en.wikipedia.org/wiki/File:Merge-sort-example-300px.gif)\n\n![Merge Sort Algorithm. (n.d). In Wikipedia](https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif)\n\n[Merge Sort With Transylvanian-saxon (German) Folk Dance](http://www.youtube.com/watch?v=XaqR3G_NVoo)\n\n[![Merge Sort with Transylvanian-saxon (German) folk dance](http://img.youtube.com/vi/XaqR3G_NVoo/0.jpg)](http://www.youtube.com/watch?v=XaqR3G_NVoo \"Merge Sort with Transylvanian-saxon (German) folk dance\")\n\n##### Merge Sort Time and Complexity\n\n* Worst Case Time Complexity [ Big-O ]: O(n log n)\n* Best Case Time Complexity [Big-omega]: O(n log n)\n* Average Time Complexity [Big-theta]: O(n log n)\n* Space Complexity: O(n)\n\n##### Merge Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/MergeSort.cs)\n\n```csharp\nC#\n\npublic void Sort(int[] list)\n{\n    MergeSort(list, new int[list.Length], 0, list.Length - 1);\n}\n\nprivate void MergeSort(int[] list, int[] tempList, int leftStart, int rightEnd)\n{\n    if (leftStart \u003e= rightEnd)\n        return;\n\n    var middleIndex = (leftStart + rightEnd) / 2;\n    MergeSort(list, tempList, leftStart, middleIndex);\n    MergeSort(list, tempList, middleIndex + 1, rightEnd);\n    Merge(list, tempList, leftStart, rightEnd);\n}\n\nprivate void Merge(int[] list, int[] tempList, int leftStart, int rightEnd)\n{\n    var leftEnd = (leftStart + rightEnd) / 2;\n    var rightStart = leftEnd + 1;\n    var size = rightEnd - leftStart + 1;\n\n    var leftIndex = leftStart;\n    var rightIndex = rightStart;\n    var tempIndex = leftStart;\n\n    while (leftIndex \u003c= leftEnd \u0026\u0026 rightIndex \u003c= rightEnd)\n    {\n        tempList[tempIndex++] = list[leftIndex] \u003c list[rightIndex]\n            ? list[leftIndex++]\n            : list[rightIndex++];\n    }\n\n    Array.Copy(list, leftIndex, tempList, tempIndex, leftEnd - leftIndex + 1);\n    Array.Copy(list, rightIndex, tempList, tempIndex, rightEnd - rightIndex + 1);\n    Array.Copy(tempList, leftStart, list, leftStart, size);\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/mergeSort/mergeSort.js)\n\n```javascript\nJavascript\n\nfunction sort(list) {\n\n    mergeSort(list, [], 0, list.length - 1);\n\n    return list;\n};\n\nfunction mergeSort(list, tempList, leftStart, rightEnd) {\n\n    if (leftStart \u003e= rightEnd) {\n        return;\n    }\n\n    var middle = Math.floor((leftStart + rightEnd) / 2);\n\n    mergeSort(list, tempList, leftStart, middle);\n    mergeSort(list, tempList, middle + 1, rightEnd);\n    merge(list, tempList, leftStart, rightEnd);\n}\n\nfunction merge(list, tempList, leftStart, rightEnd) {\n\n    var leftEnd = Math.floor((leftStart + rightEnd) / 2);\n    var rightStart = leftEnd + 1;\n    var size = rightEnd - leftStart + 1;\n\n    var leftIndex = leftStart;\n    var rightIndex = rightStart;\n    var tempIndex = leftStart;\n\n    while (leftIndex \u003c= leftEnd \u0026\u0026 rightIndex \u003c= rightEnd) {\n        tempList[tempIndex++] = list[leftIndex] \u003c list[rightIndex]\n            ? list[leftIndex++]\n            : list[rightIndex++];\n    }\n\n    list.copy(leftIndex, tempList, tempIndex, leftEnd - leftIndex + 1);\n    list.copy(rightIndex, tempList, tempIndex, rightEnd - rightIndex + 1);\n    tempList.copy(leftStart, list, leftStart, size);\n}\n\nArray.prototype.copy = function(sourceIndex, destination, destinationIndex, length) {\n    while (length !== 0) {\n        destination[destinationIndex++] = this[sourceIndex++];\n        length--;\n    }\n};\n```\n\n#### Quick Sort\n\nThe _Quick Sort_ algorithm is an in-place and comparison based sorting algorithm that can be summarised as follows:\n\n* It is a divide and conquer algorithm\n* Typically implemented as a recursive algorithm\n* Partitions the list into 2 parts by establishing a pivot\n  * A pivot may be determined using different techniques\n  * Elements less than pivot are moved to the left of pivot\n  * Elements greater than pivot are moved to the right of pivot\n  * At this point the pivot is in it's correct sorted position\n* The paritioning process is repeated for the left subarray (array that is left of pivot) and right subarray (array that is right og pivot) until the list is sorted.\n\n##### Quick Sort Examples\n\n[Merge Sort With Transylvanian-saxon (German) Folk Dance](https://www.youtube.com/watch?v=ywWBy6J5gz8)\n\n[![Quick sort with Hungarian folk dance](http://img.youtube.com/vi/ywWBy6J5gz8/0.jpg)](http://www.youtube.com/watch?v=ywWBy6J5gz8 \"Quick sort with Hungarian folk dance\")\n\n##### Quick Sort Time and Complexity\n\n* Worst Case Time Complexity [ Big-O ]: O(n\u003csup\u003e2\u003c/sup\u003e)\n* Best Case Time Complexity [Big-omega]: O(n log n)\n* Average Time Complexity [Big-theta]: O(n log n)\n* Space Complexity: O(n log n)\n\n##### Quick Sort Code\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.Algorithms/Sorting/QuickSort.cs)\n\n```csharp\nC#\n\nprotected override void QickSort(T[] list)\n{\n    QickSort(list, 0, list.Length);\n}\n\nprivate void QickSort(T[] list, int start, int end)\n{\n    if (end - start \u003c 2)\n        return;\n\n    var pivotIndex = Partition(list, start, end);\n    QickSort(list, start, pivotIndex);\n    QickSort(list, pivotIndex + 1, end);\n}\n\nprivate int Partition(T[] list, int start, int end)\n{\n    var pivot = list[start];\n    var left = start;\n    var right = end;\n\n    while (left \u003c right)\n    {\n        while (left \u003c right \u0026\u0026 list[--right] \u003e= pivot) { }\n\n        if (left \u003c right)\n            list[left] = list[right];\n\n        while (left \u003c right \u0026\u0026 list[++left] \u003c= pivot) { }\n\n        if (left \u003c right)\n            list[right] = list[left];\n    }\n\n    list[right] = pivot;\n\n    return right;\n}\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/algorithms/sorting/quickSort/quickSort.js)\n\n```javascript\nJavascript\n\nconst quickSort = function(list) {\n\n    sort(list, 0, list.length);\n\n    return list;\n};\n\nconst sort = (list, start, end) =\u003e {\n    if (end - start \u003c 2) {\n        return;\n    }\n\n    let pivot = partition(list, start, end);\n    sort(list, start, pivot);\n    sort(list, pivot + 1, end);\n};\n\nconst partition = (list, start, end) =\u003e {\n    let pivot = list[start];\n    let left = start;\n    let right = end;\n\n    while (left \u003c right) {\n\n        while (left \u003c right \u0026\u0026 list[--right] \u003e= pivot) {}\n\n        if (left \u003c right) {\n            list[left] = list[right];\n        }\n\n        while (left \u003c right \u0026\u0026 list[++left] \u003c= pivot) {}\n\n        if (left \u003c right) {\n            list[right] = list[left];\n        }\n    }\n\n    list[right] = pivot;\n\n    return right;\n};\n```\n\n---\n\n## Data Structures\n\n### Singly Linked List\n\n* A _Singly Linked List_ is a data structure that represents a sequential list of _nodes_.\n* Each _node_ in the list contains a value and a pointer to the next _node_ that forms part of list.\n* The first _node_ is called the _Head_\n\nThe following diagram illustrates a list of nodes, with each node consisting of a card as it's value and a _'Next'_ pointer to the next node in the list.\n\n![singly-linked-list](https://user-images.githubusercontent.com/33935506/41312381-31ea502c-6e87-11e8-97c8-071247623692.png)\n\n#### Examples\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.DataStructures/SinglyLinkedList/SinglyLinkedList.cs)\n\n```csharp\nC#\n\npublic sealed class SinglyLinkedList\u003cT\u003e where T : class\n{\n    public long Count { get; private set; }\n\n    public Node\u003cT\u003e Head { get; private set; }\n\n    public void AddFront(T value)\n    {\n        var node = new Node\u003cT\u003e(value);\n        node.Next = Head;\n\n        Head = node;\n\n        Count++;\n    }\n\n    public Node\u003cT\u003e RemoveFront()\n    {\n        if (Head == null)\n        {\n            return null;\n        }\n\n        var removedNode = Head;\n        Head = removedNode.Next;\n        removedNode.Next = null;\n        Count--;\n\n        return removedNode;\n    }\n}\n\npublic sealed class Node\u003cT\u003e where T : class\n{\n    public Node(T value)\n    {\n        Value = value;\n    }\n\n    public T Value { get; private set; }\n\n    public Node\u003cT\u003e Next { get; set; }\n}\n\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/data-structures/singly-linked-list/SinglyLinkedList.js)\n\n```javascript\nJavascript\n\nclass Node {\n    constructor(value) {\n        this.value = value;\n        this.next = null;\n    }\n\n    toString() {\n        return this.value.toString();\n    }\n}\n\nclass SinglyLinkedList {\n\n    constructor() {\n        this.head = null;\n        this.count = 0;\n    }\n\n    any() {\n        return this.head !== null;\n    }\n\n    addFront(value) {\n        const node = new Node(value);\n        node.next = this.head;\n\n        this.head = node;\n\n        this.count++;\n    }\n\n    removeFront() {\n        if (!this.any()) {\n            return null;\n        }\n\n        const removedNode = this.head;\n        this.head = removedNode.next;\n        removedNode.next = null;\n\n        this.count--;\n\n        return removedNode;\n    }\n\n    toArray() {\n        const nodes = [];\n\n        let currentNode = this.head;\n\n        while (currentNode != null) {\n            nodes.push(currentNode.value);\n            currentNode = currentNode.next;\n        }\n\n        return nodes;\n    }\n\n    toString() {\n        const nodes = this.toArray();\n        return `SinglyLinkedList [ ${nodes.join(' -\u003e ')} ]`;\n    }\n}\n\n```\n\n### Doubly Linked List\n\n* A _Doubly Linked List_ is a data structure that represents a sequential list of _nodes_.\n* Each _node_ in the list contains a value and pointers to the next and previous _nodes_ that forms part of list.\n* The first _node_ is called the _Head_\n* The last node is called the _Tail_\n\nThe following diagram illustrates a list of nodes, with each node consisting of a card as it's value, a _'Next'_ pointer to the next node in the list, a _'Previous'_ pointer to the previous node in the list.\n\n![doubly-linked-list](https://user-images.githubusercontent.com/33935506/42072606-ad9a8fc0-7b61-11e8-9241-59037648ba31.png)\n\n#### Examples\n\n[C# Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/csharp-ads/src/ADS.DataStructures/DoublyLinkedList/DoublyLinkedList.cs)\n\n```csharp\nC#\n\npublic sealed class DoublyLinkedList\u003cT\u003e where T : class\n{\n    public long Count { get; private set; }\n\n    public Node\u003cT\u003e Head { get; private set; }\n\n    public Node\u003cT\u003e Tail { get; private set; }\n\n    public bool Any()\n    {\n        return Head != null;\n    }\n\n    public void AddFront(T value)\n    {\n        var node = new Node\u003cT\u003e(value);\n        node.Next = Head;\n\n        if (Head == null)\n        {\n            Tail = node;\n        }\n        else\n        {\n            Head.Previous = node;\n        }\n\n        Head = node;\n        Count++;\n    }\n\n    public void AddEnd(T value)\n    {\n        var node = new Node\u003cT\u003e(value);\n\n        if (Tail == null)\n        {\n            Head = node;\n        }\n        else\n        {\n            Tail.Next = node;\n            node.Previous = Tail;\n        }\n\n        Tail = node;\n        Count++;\n    }\n\n    public Node\u003cT\u003e RemoveFront()\n    {\n        if (Head == null) return null;\n\n        var removedNode = Head;\n\n        if (Head.Next == null)\n            Tail = null;\n        else\n            Head.Next.Previous = null;\n\n        Head = Head.Next;\n        removedNode.Next = null;\n        Count--;\n\n        return removedNode;\n    }\n\n    public Node\u003cT\u003e RemoveEnd()\n    {\n        if (Head == null) return null;\n\n        var removedNode = Tail;\n\n        if (Tail.Previous == null)\n            Head = null;\n        else\n            Tail.Previous.Next = null;\n\n        Tail = Tail.Previous;\n        removedNode.Previous = null;\n        Count--;\n\n        return removedNode;\n    }\n}\n\npublic sealed class Node\u003cT\u003e where T : class\n{\n    public Node(T value)\n    {\n        Value = value;\n    }\n\n    public T Value { get; private set; }\n\n    public Node\u003cT\u003e Next { get; set; }\n\n    public Node\u003cT\u003e Previous { get; set; }\n}\n\n```\n\n[Javascript Example](https://github.com/drminnaar/algorithms-and-data-structures/blob/master/javascript-ads/src/data-structures/doubly-linked-list/DoublyLinkedList.js)\n\n```javascript\nJavascript\n\nclass Node {\n    constructor(value) {\n        this.value = value;\n        this.next = null;\n        this.previous = null;\n    }\n\n    toString() {\n        return this.value.toString();\n    }\n}\n\nclass DoublyLinkedList {\n    constructor() {\n        this.head = null;\n        this.tail = null;\n        this.count = 0;\n    }\n\n    any() {\n        return this.head !== null;\n    }\n\n    addFront(value) {\n        const node = new Node(value);\n        node.next = this.head;\n\n        if (this.head === null) {\n            this.tail = node;\n        } else {\n            this.head.previous = node;\n        }\n\n        this.head = node;\n        this.count++;\n    }\n\n    addEnd(value) {\n        const node = new Node(value);\n\n        if (this.tail === null) {\n            this.head = node;\n        } else {\n            this.tail.next = node;\n            node.previous = this.tail;\n        }\n\n        this.tail = node;\n        this.count++;\n    }\n\n    removeFront() {\n        if (this.head === null) {\n            return null;\n        }\n\n        const deletedNode = this.head;\n\n        if (this.head.next == null) {\n            this.tail = null;\n        } else {\n            this.head.next.previous = null;\n        }\n\n        this.head = this.head.next;\n        deletedNode.next = null;\n        this.count--;\n\n        return deletedNode;\n    }\n\n    removeEnd() {\n        if (this.tail === null) {\n            return null;\n        }\n\n        const deletedNode = this.tail;\n\n        if (this.tail.previous === null) {\n            this.head = null;\n        } else {\n            this.tail.previous.next = null;\n        }\n\n        this.tail = this.tail.previous;\n        deletedNode.previous = null;\n        this.count--;\n\n        return deletedNode;\n    }\n\n    toArray() {\n        const nodes = [];\n\n        let currentNode = this.head;\n\n        while (currentNode != null) {\n            nodes.push(currentNode.value);\n            currentNode = currentNode.next;\n        }\n\n        return nodes;\n    }\n\n    toString() {\n        const nodes = this.toArray();\n        return `DoublyLinkedList [ ${nodes.join(' \u003c-\u003e ')} ]`;\n    }\n}\n\n```\n\n---\n\n## Authors\n\n* **Douglas Minnaar** - _Initial work_ - [drminnaar](https://github.com/drminnaar)\n\n---\n\n[C# algorithms and data structures]: https://github.com/drminnaar/algorithms-and-data-structures/tree/master/csharp-ads\n[Javascript algorithms and data structures]: https://github.com/drminnaar/algorithms-and-data-structures/tree/master/javascript-ads","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrminnaar%2Falgorithms-and-data-structures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrminnaar%2Falgorithms-and-data-structures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrminnaar%2Falgorithms-and-data-structures/lists"}