|
1 | 1 | = Quick Sort
|
2 | 2 |
|
3 |
| -Veniam veniam et reprehenderit cupidatat laboris incididunt dolore. Mollit id aliqua laboris eiusmod nostrud aliquip enim cillum ipsum consequat tempor. Proident voluptate duis nisi elit aliqua pariatur quis mollit ullamco aliqua qui. |
| 3 | +Quick sort is an efficient sorting algorithm that uses "divide and conquer" paradigm to accomplish it task faster. It uses auxiliary memory in the process of sorting. |
| 4 | + |
| 5 | +== Quick Sort Implementation |
| 6 | + |
| 7 | +Quick sort implementation is as follows |
| 8 | + |
| 9 | +.Quick Sort Algorithm |
| 10 | +. It moves one element at a time (from left to right). Everything on the left of the current element is already sorted, while everything to the right is not. |
| 11 | +. Start with the first element and make it the current element. |
| 12 | +. Compare elements to right of the current element. |
| 13 | +. Quick up big values to the right of the array. |
| 14 | +.. Swap elements if the previous element is bigger than the previous one. |
| 15 | +. Move the current pointer to the next element and repeat for the rest of the array |
| 16 | + |
| 17 | + |
| 18 | +Let's convert these words into code! |
| 19 | + |
| 20 | +.Quick Sort implementation in JavaScript (QuickSort) |
| 21 | +[source, javascript] |
| 22 | +---- |
| 23 | +include::{codedir}/algorithms/sorting/Quick-sort.js[tag=sort, indent=0] |
| 24 | +---- |
| 25 | +<1> Convert any kind of iterable (array, sets, etc.) into an array |
| 26 | + |
| 27 | +As you can see this function is just a wrapper to transform things to array. The heavy lifting is done in `splitSort` as you can see below. |
| 28 | + |
| 29 | +.Quick Sort implementation in JavaScript (splitSort) |
| 30 | +[source, javascript] |
| 31 | +---- |
| 32 | +include::{codedir}/algorithms/sorting/Quick-sort.js[tag=splitSort, indent=0] |
| 33 | +---- |
| 34 | +<1> Recursively divide the array in half until two or less elements are left. |
| 35 | +<2> Sort two or less elements. |
| 36 | +<3> Quick back the sorted halves in ascending order. |
| 37 | + |
| 38 | +Quick sort has a <<Quadratic>> running time, as you might infer from the nested for-loop. |
| 39 | + |
| 40 | +== Quick Sort Properties |
| 41 | + |
| 42 | +- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_ |
| 43 | +- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_ |
| 44 | +- <<Stable>>: [big]#✅# Yes |
| 45 | +- <<In-place>>: [big]#⛔️️️️️# No, it requires auxiliary memory O(n). |
| 46 | +- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, new elements will require to sort the whole array. |
| 47 | +- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n). |
| 48 | +- Recursive: Yes |
0 commit comments