|
1 | 1 | --- |
2 | 2 | layout: post |
3 | | -title: First post! |
| 3 | +title: Sorting Algorithms |
4 | 4 | image: /img/hello_world.jpeg |
5 | 5 |
|
6 | 6 | --- |
7 | 7 |
|
8 | 8 |
|
9 | | -## Sorting |
| 9 | +## Sorting Algorithms |
| 10 | + |
| 11 | +In computer science a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: |
| 12 | + |
| 13 | +* The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); |
| 14 | +* The output is a permutation (reordering) of the input. |
| 15 | + |
| 16 | +### Bubble |
| 17 | +![alt text][bubble-image] |
| 18 | + |
| 19 | +From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. |
| 20 | + |
| 21 | +__Properties__ |
| 22 | +* Worst case performance O(n^2) |
| 23 | +* Best case performance O(n) |
| 24 | +* Average case performance O(n^2) |
| 25 | + |
| 26 | +###### View the algorithm in [action][bubble-toptal] |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +### Insertion |
| 31 | +![alt text][insertion-image] |
| 32 | + |
| 33 | +From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. |
| 34 | + |
| 35 | +__Properties__ |
| 36 | +* Worst case performance O(n^2) |
| 37 | +* Best case performance O(n) |
| 38 | +* Average case performance O(n^2) |
| 39 | + |
| 40 | +###### View the algorithm in [action][insertion-toptal] |
| 41 | + |
| 42 | + |
| 43 | +### Merge |
| 44 | +![alt text][merge-image] |
| 45 | + |
| 46 | +From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. |
| 47 | + |
| 48 | +__Properties__ |
| 49 | +* Worst case performance O(n log n) |
| 50 | +* Best case performance O(n) |
| 51 | +* Average case performance O(n) |
| 52 | + |
| 53 | + |
| 54 | +###### View the algorithm in [action][merge-toptal] |
| 55 | + |
| 56 | +### Quick |
| 57 | +![alt text][quick-image] |
| 58 | + |
| 59 | +From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. |
| 60 | + |
| 61 | +__Properties__ |
| 62 | +* Worst case performance O(n^2) |
| 63 | +* Best case performance O(n log n) or O(n) with three-way partition |
| 64 | +* Average case performance O(n^2) |
| 65 | + |
| 66 | +###### View the algorithm in [action][quick-toptal] |
| 67 | + |
| 68 | +### Selection |
| 69 | +![alt text][selection-image] |
| 70 | + |
| 71 | +From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. |
| 72 | + |
| 73 | +__Properties__ |
| 74 | +* Worst case performance O(n^2) |
| 75 | +* Best case performance O(n^2) |
| 76 | +* Average case performance O(n^2) |
| 77 | + |
| 78 | +###### View the algorithm in [action][selection-toptal] |
| 79 | + |
| 80 | +### Shell |
| 81 | +![alt text][shell-image] |
| 82 | + |
| 83 | +From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywherem considereing every nth element gives a sorted list. Such a list is said to be h-sorted. Equivanelty, it can be thought of as h intterleaved lists, each individually sorted. |
| 84 | + |
| 85 | +__Properties__ |
| 86 | +* Worst case performance O(nlog2 2n) |
| 87 | +* Best case performance O(n log n) |
| 88 | +* Average case performance depends on gap sequence |
| 89 | + |
| 90 | +###### View the algorithm in [action][shell-toptal] |
| 91 | + |
| 92 | +###Time-Compexity Graphs |
| 93 | + |
| 94 | +Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) |
| 95 | + |
| 96 | +[Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) |
| 97 | + |
| 98 | +---------------------------------------------------------------------------------- |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +## Sorting Tutorials |
| 104 | + |
| 105 | +- [ ] Notes: |
| 106 | + - Implement sorts & know best case/worst case, average complexity of each: |
| 107 | + - no bubble sort - it's terrible - O(n^2), except when n <= 16 |
| 108 | + - [ ] stability in sorting algorithms ("Is Quicksort stable?") |
| 109 | + - [Sorting Algorithm Stability](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability) |
| 110 | + - [Stability In Sorting Algorithms](http://stackoverflow.com/questions/1517793/stability-in-sorting-algorithms) |
| 111 | + - [Stability In Sorting Algorithms](http://www.geeksforgeeks.org/stability-in-sorting-algorithms/) |
| 112 | + - [Sorting Algorithms - Stability](http://homepages.math.uic.edu/~leon/cs-mcs401-s08/handouts/stability.pdf) |
| 113 | + - [ ] Which algorithms can be used on linked lists? Which on arrays? Which on both? |
| 114 | + - I wouldn't recommend sorting a linked list, but merge sort is doable. |
| 115 | + - [Merge Sort For Linked List](http://www.geeksforgeeks.org/merge-sort-for-linked-list/) |
| 116 | + |
| 117 | +- For heapsort, see Heap data structure above. Heap sort is great, but not stable. |
| 118 | + |
| 119 | +- [ ] [Bubble Sort (video)](https://www.youtube.com/watch?v=P00xJgWzz2c&index=1&list=PL89B61F78B552C1AB) |
| 120 | +- [ ] [Analyzing Bubble Sort (video)](https://www.youtube.com/watch?v=ni_zk257Nqo&index=7&list=PL89B61F78B552C1AB) |
| 121 | +- [ ] [Insertion Sort, Merge Sort (video)](https://www.youtube.com/watch?v=Kg4bqzAqRBM&index=3&list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb) |
| 122 | +- [ ] [Insertion Sort (video)](https://www.youtube.com/watch?v=c4BRHC7kTaQ&index=2&list=PL89B61F78B552C1AB) |
| 123 | +- [ ] [Merge Sort (video)](https://www.youtube.com/watch?v=GCae1WNvnZM&index=3&list=PL89B61F78B552C1AB) |
| 124 | +- [ ] [Quicksort (video)](https://www.youtube.com/watch?v=y_G9BkAm6B8&index=4&list=PL89B61F78B552C1AB) |
| 125 | +- [ ] [Selection Sort (video)](https://www.youtube.com/watch?v=6nDMgr0-Yyo&index=8&list=PL89B61F78B552C1AB) |
| 126 | + |
| 127 | +- [ ] Stanford lectures on sorting: |
| 128 | + - [ ] [Lecture 15 | Programming Abstractions (video)](https://www.youtube.com/watch?v=ENp00xylP7c&index=15&list=PLFE6E58F856038C69) |
| 129 | + - [ ] [Lecture 16 | Programming Abstractions (video)](https://www.youtube.com/watch?v=y4M9IVgrVKo&index=16&list=PLFE6E58F856038C69) |
| 130 | + |
| 131 | +- [ ] Shai Simonson, [Aduni.org](http://www.aduni.org/): |
| 132 | + - [ ] [Algorithms - Sorting - Lecture 2 (video)](https://www.youtube.com/watch?v=odNJmw5TOEE&list=PLFDnELG9dpVxQCxuD-9BSy2E7BWY3t5Sm&index=2) |
| 133 | + - [ ] [Algorithms - Sorting II - Lecture 3 (video)](https://www.youtube.com/watch?v=hj8YKFTFKEE&list=PLFDnELG9dpVxQCxuD-9BSy2E7BWY3t5Sm&index=3) |
| 134 | + |
| 135 | +- [ ] Steven Skiena lectures on sorting: |
| 136 | + - [ ] [lecture begins at 26:46 (video)](https://youtu.be/ute-pmMkyuk?list=PLOtl7M3yp-DV69F32zdK7YJcNXpTunF2b&t=1600) |
| 137 | + - [ ] [lecture begins at 27:40 (video)](https://www.youtube.com/watch?v=yLvp-pB8mak&index=8&list=PLOtl7M3yp-DV69F32zdK7YJcNXpTunF2b) |
| 138 | + - [ ] [lecture begins at 35:00 (video)](https://www.youtube.com/watch?v=q7K9otnzlfE&index=9&list=PLOtl7M3yp-DV69F32zdK7YJcNXpTunF2b) |
| 139 | + - [ ] [lecture begins at 23:50 (video)](https://www.youtube.com/watch?v=TvqIGu9Iupw&list=PLOtl7M3yp-DV69F32zdK7YJcNXpTunF2b&index=10) |
| 140 | + |
| 141 | +- [ ] UC Berkeley: |
| 142 | + - [ ] [CS 61B Lecture 29: Sorting I (video)](https://www.youtube.com/watch?v=EiUvYS2DT6I&list=PL4BBB74C7D2A1049C&index=29) |
| 143 | + - [ ] [CS 61B Lecture 30: Sorting II (video)](https://www.youtube.com/watch?v=2hTY3t80Qsk&list=PL4BBB74C7D2A1049C&index=30) |
| 144 | + - [ ] [CS 61B Lecture 32: Sorting III (video)](https://www.youtube.com/watch?v=Y6LOLpxg6Dc&index=32&list=PL4BBB74C7D2A1049C) |
| 145 | + - [ ] [CS 61B Lecture 33: Sorting V (video)](https://www.youtube.com/watch?v=qNMQ4ly43p4&index=33&list=PL4BBB74C7D2A1049C) |
| 146 | + |
| 147 | +- [ ] - Merge sort code: |
| 148 | + - [ ] [Using output array](http://www.cs.yale.edu/homes/aspnes/classes/223/examples/sorting/mergesort.c) |
| 149 | + - [ ] [In-place](https://github.com/jwasham/practice-cpp/blob/master/merge_sort/merge_sort.cc) |
| 150 | +- [ ] - Quick sort code: |
| 151 | + - [ ] [Implementation](http://www.cs.yale.edu/homes/aspnes/classes/223/examples/randomization/quick.c) |
| 152 | + - [ ] [Implementation](https://github.com/jwasham/practice-c/blob/master/quick_sort/quick_sort.c) |
| 153 | + |
| 154 | +- [ ] Implement: |
| 155 | + - [ ] Mergesort: O(n log n) average and worst case |
| 156 | + - [ ] Quicksort O(n log n) average case |
| 157 | + - Selection sort and insertion sort are both O(n^2) average and worst case |
| 158 | + - For heapsort, see Heap data structure above. |
| 159 | + |
| 160 | +- [ ] For curiosity - not required: |
| 161 | + - [ ] [Radix Sort](http://www.cs.yale.edu/homes/aspnes/classes/223/notes.html#radixSort) |
| 162 | + - [ ] [Radix Sort (video)](https://www.youtube.com/watch?v=xhr26ia4k38) |
| 163 | + - [ ] [Radix Sort, Counting Sort (linear time given constraints) (video)](https://www.youtube.com/watch?v=Nz1KZXbghj8&index=7&list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb) |
| 164 | + - [ ] [Randomization: Matrix Multiply, Quicksort, Freivalds' algorithm (video)](https://www.youtube.com/watch?v=cNB2lADK3_s&index=8&list=PLUl4u3cNGP6317WaSNfmCvGym2ucw3oGp) |
| 165 | + - [ ] [Sorting in Linear Time (video)](https://www.youtube.com/watch?v=pOKy3RZbSws&list=PLUl4u3cNGP61hsJNdULdudlRL493b-XZf&index=14) |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort |
| 170 | +[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort |
| 171 | +[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" |
| 172 | + |
| 173 | +[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort |
| 174 | +[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort |
| 175 | +[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort" |
| 176 | + |
| 177 | +[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort |
| 178 | +[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort |
| 179 | +[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" |
| 180 | + |
| 181 | +[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort |
| 182 | +[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort |
| 183 | +[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort" |
| 184 | + |
| 185 | +[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort |
| 186 | +[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort |
| 187 | +[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" |
| 188 | + |
| 189 | +[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort |
| 190 | +[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort |
| 191 | +[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" |
| 192 | + |
0 commit comments