├── .gitignore ├── LICENSE ├── README.md └── src ├── bubble_sort └── bubble_sort.js ├── merge_sort ├── merge_sort_diagram.png └── merge_sort_recursion.js └── selection_sort └── selection_sort.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Tim Han 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting Algorithms (JavaScript) 2 | 3 | Collection of sorting algorithms in JavaScript. 4 | 5 | ## List 6 | 7 | * [Bubble Sort](https://github.com/yeb9925/sorting-algorithms/tree/master/src/bubble_sort) 8 | * [Merge Sort](https://github.com/yeb9925/sorting-algorithms/tree/master/src/merge_sort) (Recursion) 9 | * [Selection Sort](https://github.com/yeb9925/sorting-algorithms/tree/master/src/selection_sort) 10 | 11 | ## Author 12 | 13 | * **Tim Han** - [yeb9925](https://github.com/yeb9925) 14 | 15 | ## License 16 | 17 | This project is licensed under the MIT License - see the [LICENSE](https://github.com/yeb9925/sorting-algorithms/blob/master/LICENSE) file for details 18 | -------------------------------------------------------------------------------- /src/bubble_sort/bubble_sort.js: -------------------------------------------------------------------------------- 1 | // 2 | // Bubble Sort Implementation 3 | // 4 | 5 | function bubbleSort (unsortedArray) { 6 | if (unsortedArray.length <= 1) { 7 | return unsortedArray; 8 | } 9 | 10 | for (let i = 0; i < unsortedArray.length; i++) { 11 | for (let j = 0; j < (unsortedArray.length - i - 1); j++) { 12 | // Swap if the element is larger than the element right next to it 13 | if (unsortedArray[j] > unsortedArray[j+1]) { 14 | [unsortedArray[j], unsortedArray[j+1]] = [unsortedArray[j+1], unsortedArray[j]]; // ES6 Swap 15 | } 16 | } 17 | } 18 | 19 | return unsortedArray; 20 | } 21 | -------------------------------------------------------------------------------- /src/merge_sort/merge_sort_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yeb9925/sorting-algorithms-javascript/691605c00605f683ffb93456eec7f1b5f89b9cb3/src/merge_sort/merge_sort_diagram.png -------------------------------------------------------------------------------- /src/merge_sort/merge_sort_recursion.js: -------------------------------------------------------------------------------- 1 | // 2 | // Merge Sort Implentation (Recursion) 3 | // 4 | 5 | function mergeSort (unsortedArray) { 6 | // No need to sort the array if the array only has one element or empty 7 | if (unsortedArray.length <= 1) { 8 | return unsortedArray; 9 | } 10 | // In order to divide the array in half, we need to figure out the middle 11 | const middle = Math.floor(unsortedArray.length / 2); 12 | 13 | // This is where we will be dividing the array into left and right 14 | const left = unsortedArray.slice(0, middle); 15 | const right = unsortedArray.slice(middle); 16 | 17 | // Using recursion to combine the left and right 18 | return merge( 19 | mergeSort(left), mergeSort(right) 20 | ); 21 | } 22 | 23 | // Merge the two arrays: left and right 24 | function merge (left, right) { 25 | let resultArray = [], leftIndex = 0, rightIndex = 0; 26 | 27 | // We will concatenate values into the resultArray in order 28 | while (leftIndex < left.length && rightIndex < right.length) { 29 | if (left[leftIndex] < right[rightIndex]) { 30 | resultArray.push(left[leftIndex]); 31 | leftIndex++; // move left array cursor 32 | } else { 33 | resultArray.push(right[rightIndex]); 34 | rightIndex++; // move right array cursor 35 | } 36 | } 37 | 38 | // We need to concat to the resultArray because there will be one element left over after the while loop 39 | return resultArray 40 | .concat(left.slice(leftIndex)) 41 | .concat(right.slice(rightIndex)); 42 | } 43 | -------------------------------------------------------------------------------- /src/selection_sort/selection_sort.js: -------------------------------------------------------------------------------- 1 | // 2 | // Selection Sort Implementation 3 | // 4 | 5 | function selectionSort (unsortedArray) { 6 | if (unsortedArray.length <= 1) { 7 | return unsortedArray; 8 | } 9 | 10 | for (let i = 0; i < unsortedArray.length; i++) { 11 | let minIndex = i; 12 | for (let j = i + 1; j < unsortedArray.length; j++) { 13 | if (unsortedArray[j] < unsortedArray[minIndex]) { 14 | minIndex = j; 15 | } 16 | } 17 | // Swap the current element with the smaller element 18 | [unsortedArray[i], unsortedArray[minIndex]] = [unsortedArray[minIndex], unsortedArray[i]]; // ES6 Swap 19 | } 20 | 21 | return unsortedArray; 22 | } 23 | --------------------------------------------------------------------------------