From 0b8ac95c164e8924ae4c1bfa210de1d49888bbe9 Mon Sep 17 00:00:00 2001 From: Sooraj s <52352285+Sooraj-s-98@users.noreply.github.com> Date: Fri, 16 Oct 2020 14:22:15 +0530 Subject: [PATCH 1/4] mergesort --- SUMMARY.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/SUMMARY.md b/SUMMARY.md index 270509494..cf214baee 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -15,6 +15,7 @@ * [Sorting and Searching](contents/sorting_and_searching/sorting_and_searching.md) * [Bubble Sort](contents/bubble_sort/bubble_sort.md) * [Bogo Sort](contents/bogo_sort/bogo_sort.md) + * [Merge Sort](contents/merge_sort/merge_sort.md) * [Tree Traversal](contents/tree_traversal/tree_traversal.md) * [Euclidean Algorithm](contents/euclidean_algorithm/euclidean_algorithm.md) * [Monte Carlo](contents/monte_carlo_integration/monte_carlo_integration.md) @@ -23,8 +24,8 @@ * [Thomas Algorithm](contents/thomas_algorithm/thomas_algorithm.md) * [Computational Geometry](contents/computational_geometry/computational_geometry.md) * [Gift Wrapping](contents/gift_wrapping/gift_wrapping.md) - * [Jarvis March](contents/jarvis_march/jarvis_march.md) - * [Graham Scan](contents/graham_scan/graham_scan.md) + * [Jarvis March](contents/jarvis_march/jarvis_march.md) + * [Graham Scan](contents/graham_scan/graham_scan.md) * [FFT](contents/cooley_tukey/cooley_tukey.md) * [Decision Problems](contents/decision_problems/decision_problems.md) * [Stable Marriage Problem](contents/stable_marriage_problem/stable_marriage_problem.md) @@ -33,7 +34,7 @@ * [Physics Solvers](contents/physics_solvers/physics_solvers.md) * [Verlet Integration](contents/verlet_integration/verlet_integration.md) * [Quantum Systems](contents/quantum_systems/quantum_systems.md) - * [Split-Operator Method](contents/split-operator_method/split-operator_method.md) + * [Split-Operator Method](contents/split-operator_method/split-operator_method.md) * [Data Compression](contents/data_compression/data_compression.md) * [Huffman Encoding](contents/huffman_encoding/huffman_encoding.md) * [Computer Graphics](contents/computer_graphics/computer_graphics.md) From 98c1b56983e66c51d330e78e0b364bc526d8bfc0 Mon Sep 17 00:00:00 2001 From: Sooraj s <52352285+Sooraj-s-98@users.noreply.github.com> Date: Fri, 16 Oct 2020 14:38:38 +0530 Subject: [PATCH 2/4] code added --- .../quick_sort/Code/javascript/quicksort.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 contents/quick_sort/Code/javascript/quicksort.js diff --git a/contents/quick_sort/Code/javascript/quicksort.js b/contents/quick_sort/Code/javascript/quicksort.js new file mode 100644 index 000000000..786f236c8 --- /dev/null +++ b/contents/quick_sort/Code/javascript/quicksort.js @@ -0,0 +1,57 @@ +// Find a "pivot" element in the array to compare all other +// elements against and then shift elements before or after +// pivot depending on their values +function QuickSort(arr, left = 0, right = arr.length - 1) { + let len = arr.length, + index + + if(len > 1) { + + index = partition(arr, left, right) + + if(left < index - 1) { + QuickSort(arr, left, index - 1) + } + + if(index < right) { + QuickSort(arr, index, right) + } + + } + + return arr + + } + + function partition(arr, left, right) { + let middle = Math.floor((right + left) / 2), + pivot = arr[middle], + i = left, // Start pointer at the first item in the array + j = right // Start pointer at the last item in the array + + while(i <= j) { + + // Move left pointer to the right until the value at the + // left is greater than the pivot value + while(arr[i] < pivot) { + i++ + } + + // Move right pointer to the left until the value at the + // right is less than the pivot value + while(arr[j] > pivot) { + j-- + } + + // If the left pointer is less than or equal to the + // right pointer, then swap values + if(i <= j) { + [arr[i], arr[j]] = [arr[j], arr[i]] // ES6 destructuring swap + i++ + j-- + } + } + + return i + + } \ No newline at end of file From adb56b267c3d0ca8d8cfafba01944d17f1b22b42 Mon Sep 17 00:00:00 2001 From: Sooraj s <52352285+Sooraj-s-98@users.noreply.github.com> Date: Fri, 16 Oct 2020 15:02:54 +0530 Subject: [PATCH 3/4] Create quick_sort.md --- contents/quick_sort/quick_sort.md | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 contents/quick_sort/quick_sort.md diff --git a/contents/quick_sort/quick_sort.md b/contents/quick_sort/quick_sort.md new file mode 100644 index 000000000..2c7b1be2d --- /dev/null +++ b/contents/quick_sort/quick_sort.md @@ -0,0 +1,67 @@ +### Quick sort + Quick sort is one of the most important sorting methods in javascript. It takes a pivot value(a random value) from an array. All the other elements in the array are split to two +categories.They may be less than the pivot value and greater than the pivot value.After that each of the categories(less than the pivot and greater than the pivot) are subjected +to the same procedure that is a pivot is chosen then each category is divided in to sub-categories(less than the pivot and greater than the pivot).Eventually, the sub-categories +are divided in such a way that they may contain an element or no element if there are no more elements to compare. The rest of the values will be denoted as a pivots at some +previous points and did not trickle down to this lowest sub category. + + +There are two basic operations in the algorithm, swapping items in place and partitioning a section of the array. The basic steps to partition an array are: + + 1. Find a “pivot” item in the array. This item is the basis for comparison for a single round. + 2. Start a pointer (the left pointer) at the first item in the array. + 3. Start a pointer (the right pointer) at the last item in the array. + 4. While the value at the left pointer in the array is less than the pivot value, + move the left pointer to the right (add 1). Continue until the value at the left pointer is greater than or equal to the pivot value. + 5. While the value at the right pointer in the array is greater than the pivot value, move the right pointer to the left (subtract 1). + Continue until the value at the right pointer is less than or equal to the pivot value. + 6. If the left pointer is less than or equal to the right pointer, then swap the values at these locations in the array. + 7. Move the left pointer to the right by one and the right pointer to the left by one. + 9. If the left pointer and right pointer don’t meet, go to step 1. + +{% method %} +{% sample lang="js" %} +[import:1-11, lang:"javascript"](code/javascript/quicksort.js) +{% endmethod %} + + +{% method %} +{% sample lang="js" %} +[import:13-29, lang:"javascript"](code/javascript/quickort.js) +{% endmethod %} + + +This is an example of how merge sort works over every iteration until return the result. + +
+    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
+
+
+ +## Example Code + +{% method %} +{% sample lang="js" %} +[import:31-38, lang:"javascript"](Code/javascript/quicksort.js) +{% endmethod %} + + + +## License + +##### Code Examples + +The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)). + +##### Text + +The text of this chapter was written by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode). + +[

](https://creativecommons.org/licenses/by-sa/4.0/) + +##### Pull Requests + +After initial licensing ([#560](https://github.com/algorithm-archivists/algorithm-archive/pull/560)), the following pull requests have modified the text or graphics of this chapter: +- none From 64d50198bd4128aa3d7d84ae77bd7e1bf6350940 Mon Sep 17 00:00:00 2001 From: Sooraj s <52352285+Sooraj-s-98@users.noreply.github.com> Date: Fri, 16 Oct 2020 15:05:28 +0530 Subject: [PATCH 4/4] quick sort javaScript added --- SUMMARY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/SUMMARY.md b/SUMMARY.md index cf214baee..cf3cdc91b 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -16,6 +16,7 @@ * [Bubble Sort](contents/bubble_sort/bubble_sort.md) * [Bogo Sort](contents/bogo_sort/bogo_sort.md) * [Merge Sort](contents/merge_sort/merge_sort.md) + * [Quick Sort](contents/quick_sort/quick_sort.md) * [Tree Traversal](contents/tree_traversal/tree_traversal.md) * [Euclidean Algorithm](contents/euclidean_algorithm/euclidean_algorithm.md) * [Monte Carlo](contents/monte_carlo_integration/monte_carlo_integration.md)