Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quicksort chapter javaScript added #770

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* [Affine Transformations](contents/affine_transformations/affine_transformations.md)
* [Bit Logic](contents/bitlogic/bitlogic.md)
* [Taylor Series](contents/taylor_series_expansion/taylor_series_expansion.md)

* [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)
* [Quick Sort](contents/quick_sort/quick_sort.md)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not seeing a merge_sort.md file added? is that intentional?


* [Convolutions](contents/convolutions/convolutions.md)
* [Convolutions in 1D](contents/convolutions/1d/1d.md)
* [Multiplication as a Convolution](contents/convolutions/multiplication/multiplication.md)
Expand All @@ -27,8 +34,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)
Comment on lines -33 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unintended whitespace change?

* [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)
Expand All @@ -37,7 +44,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unintended whitespace change?

* [Data Compression](contents/data_compression/data_compression.md)
* [Huffman Encoding](contents/huffman_encoding/huffman_encoding.md)
* [Computer Graphics](contents/computer_graphics/computer_graphics.md)
Expand Down
57 changes: 57 additions & 0 deletions contents/quick_sort/Code/javascript/quicksort.js
Original file line number Diff line number Diff line change
@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good practice to use const instead of let for variables where the value doesn't change. (see let len on line 5 too)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I'm not sure how important it is, but it's generally good practice to do something like:

...
  let middle = Math.floor( left + (right - left)/2 ),
...

As this avoids the possibility of overflow (see here).

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

}
67 changes: 67 additions & 0 deletions contents/quick_sort/quick_sort.md
Original file line number Diff line number Diff line change
@@ -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.

<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

</pre>

## Example Code

{% method %}
{% sample lang="js" %}
[import:31-38, lang:"javascript"](Code/javascript/quicksort.js)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a restrictive range, why not the whole file?

{% endmethod %}

<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>

## 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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be an instance where it wasn't by the amazing @leios


[<p><img class="center" src="../cc/CC-BY-SA_icon.svg" /></p>](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