├── bubbleSort.h ├── insertionSort.h ├── selectionSort.h ├── quickSort.h ├── LICENSE ├── mergeSort.h └── README.md /bubbleSort.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // BUBBLE SORT 6 | //------------------------------------------------------- 7 | 8 | void bubbleSort(vector &v, int n) { 9 | bool swapped = true; 10 | int i = 0; 11 | 12 | while (i < n - 1 && swapped) { // keep going while we swap in the unordered part 13 | swapped = false; 14 | 15 | for (int j = n - 1; j > i; j--) { // unordered part 16 | 17 | if (v[j] < v[j - 1]) { 18 | swap(v[j], v[j - 1]); 19 | swapped = true; 20 | } 21 | } 22 | i++; 23 | } 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /insertionSort.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // INSERTION SORT 6 | //------------------------------------------------------------ 7 | 8 | void insertionSort(vector &v, int n) { 9 | int current, pos; 10 | 11 | for (int i = 1; i < n; i++) { 12 | current = v[i]; 13 | pos = i; // limit of the ordered part, pos not included 14 | 15 | // we make space 16 | while (pos > 0 && v[pos - 1] > current) { 17 | v[pos] = v[pos - 1]; 18 | pos--; 19 | } 20 | 21 | // we move the current value to its position 22 | if (pos != i) 23 | v[pos] = current; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /selectionSort.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // SELECTION SORT 6 | //------------------------------------------------------- 7 | 8 | void selectionSort(vector &v, int n) { 9 | int minPosition, aux; 10 | 11 | for (int i = 0; i < n - 1; i++) { 12 | minPosition = i; // suppose "i" is the minimum 13 | 14 | for (int j = i + 1; j < n; j++) { // find the "min" element in the unsorted part 15 | 16 | if (v[j] < v[minPosition]) { 17 | minPosition = j; 18 | } 19 | } 20 | // swap the found "min" element to the sorted part 21 | if (minPosition > i) { 22 | swap(v[minPosition], v[i]); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /quickSort.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // QUICKSORT 6 | // ------------------------------------------------------------------------- 7 | 8 | // Partition function 9 | int partition(vector &v, int start, int end) { 10 | 11 | int pivot = start; // Take the first element as a pivot 12 | int i = start + 1; 13 | int j = end; 14 | 15 | while (i <= j) { 16 | 17 | // If element at the left is bigger than the pivot and 18 | // element at the right is smaller, swap elements 19 | 20 | if (v[i] > v[pivot] && v[j] < v[pivot]) { 21 | swap(v[i], v[j]); 22 | } 23 | else if (v[i] <= v[pivot]) { i++; } 24 | else if (v[j] >= v[pivot]) { j--; } 25 | } 26 | 27 | // we swap the pivot into the right position 28 | swap(v[j], v[pivot]); 29 | return j; 30 | } 31 | 32 | // QuickSort 33 | void quickSort(vector &v, int start, int end) { 34 | 35 | if (start < end) { 36 | int pivot = partition(v, start, end); 37 | quickSort(v, start, pivot); 38 | quickSort(v, pivot + 1, end); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Edu Martinez 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 | -------------------------------------------------------------------------------- /mergeSort.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // MERGESORT 6 | // ------------------------------------------------------------------------- 7 | 8 | // Merge 9 | void merge(vector &v, int start, int mid, int end) 10 | { 11 | vector aux(v.size()); 12 | int i = start; // begin of the first part 13 | int j = mid + 1; // begin of the second part 14 | int k = start; // start of the aux vector 15 | 16 | 17 | // Elements in both parts 18 | while (i <= mid && j <= end) { 19 | if (v[i] < v[j]) 20 | aux[k] = v[i++]; 21 | else 22 | aux[k] = v[j++]; 23 | 24 | k++; 25 | } 26 | 27 | // Left part has elements 28 | while (i <= mid) { 29 | aux[k] = v[i++]; 30 | k++; 31 | } 32 | 33 | // Right part has elements 34 | while (j <= end) { 35 | aux[k] = v[j++]; 36 | k++; 37 | } 38 | 39 | // Copy the vector 40 | for (int i = start; i <= end; i++) { 41 | v[i] = aux[i]; 42 | } 43 | 44 | } 45 | 46 | // Merge sort 47 | void mergeSort(vector &v, int start, int end) { 48 | 49 | if (start < end) { 50 | int m = (start + end) / 2; 51 | mergeSort(v, start, m); 52 | mergeSort(v, m + 1, end); 53 | merge(v, start, m, end); 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A collection of sorting algorithms implemented in C++ 2 | 3 | A collection of sorting algorithms implemented in C++ to see the advantages and disadvantages of each one of them 4 | 5 | ## Bubble Sort 6 | A slow sorting algorithm for the simplest data sets 7 | 8 | | Case | Performance | 9 | | :---: | :---: | 10 | | Worst case performance | O(n^2) | 11 | | Best case performance | O(n) | 12 | | Average case performance | O(n^2) | 13 | | Auxiliary Space | O(1) | 14 | 15 | ![bubble-sort-gif-9](https://user-images.githubusercontent.com/36489953/42171410-83532a64-7e19-11e8-95a1-b2dd3aaedc43.gif) 16 | 17 | 18 | ## Insertion Sort 19 | Insertion sort is a simple sorting algorithm that works the way we sort playing cards. It is efficient for (quite) small data sets. 20 | It is often used when the data set is nearly sorted (it takes minimum time (Order of n)). 21 | It takes maximum time to sort if elements are sorted in reverse order. 22 | 23 | 24 | | Case | Performance | 25 | | :---: | :---: | 26 | | Worst case performance | O(n^2) | 27 | | Best case performance | O(n) | 28 | | Average case performance | O(n^2) | 29 | | Auxiliary Space | O(1) | 30 | 31 | ![1_kra0ofxedgi8hvhjffci4w](https://user-images.githubusercontent.com/36489953/42171484-b508016a-7e19-11e8-8d47-3b95d788d579.gif) 32 | 33 | ## Selection Sort 34 | 35 | Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited 36 | 37 | | Case | Performance | 38 | | :---: | :---: | 39 | | Worst case performance | O(n^2) | 40 | | Best case performance | O(n^2) | 41 | | Average case performance | O(n^2) | 42 | | Auxiliary Space | O(1) | 43 | 44 | ![selectionsort](https://user-images.githubusercontent.com/36489953/42171344-5554d9d2-7e19-11e8-8537-7811ebbbd1b6.gif) 45 | 46 | 47 | ## Merge Sort 48 | Merge Sort is a Divide and Conquer algorithm that continually splits a list in half and then merge the sublists in a sorted way. 49 | A single most important advantage of merge sort over quick sort is its stability: the elements compared equal retain their original order. 50 | 51 | 52 | | Case | Performance | 53 | | :---: | :---: | 54 | | Worst case performance | O(n log n) | 55 | | Best case performance | O(n log n) | 56 | | Average case performance | O(n log n) | 57 | | Auxiliary Space | O(n) | 58 | 59 | 60 | ![merge-sort-example-300px](https://user-images.githubusercontent.com/36489953/42171944-ed5814c8-7e1a-11e8-9d30-10ae8047bb17.gif) 61 | 62 | ## Quick sort 63 | 64 | Quick Sort is a recursive sorting algorithm that is more effective than other O(nlogn) algorithms for large datasets that fit in memory, but is unstable. Quick Sort in general does not requiere extra space while Merge Sort requires O(n) extra storage 65 | 66 | | Case | Performance | 67 | | :---: | :---: | 68 | | Worst case performance | O(n^2) | 69 | | Best case performance | O(n log n) | 70 | | Average case performance | O(n log n) | 71 | | Auxiliary Space | O(log(n)) | 72 | 73 | ![quicksort-example](https://user-images.githubusercontent.com/36489953/42190383-0923306a-7e5d-11e8-86b3-1e9f7a79b782.gif) 74 | --------------------------------------------------------------------------------