Skip to content

Commit 5fef824

Browse files
committed
Quick sort README file added and Code comment updated
1 parent ee8dcd8 commit 5fef824

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

Sorting/Merge Sort/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ While comparing two sub-lists for merging, the first element of both lists is ta
1212
![Merge Sort](./images/merge-sort.png)
1313

1414
#### A visualization on Merge Sort
15+
1516
![Merge sort demonstration](./images/merge-sort-animation.gif)
1617

1718
*An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged. (image source-wikipedia)*

Sorting/Quick Sort/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Quick Sort
2+
3+
**Quick sort** is based on the divide-and-conquer approach based on the idea of choosing one element as a pivot element and partitioning the array around it such that: Left side of pivot contains all the elements that are less than the pivot element Right side contains all elements greater than the pivot
4+
5+
It reduces the space complexity and removes the use of the auxiliary array that is used in merge sort. Selecting a random pivot in an array results in an improved time complexity in most of the cases.
6+
7+
Like Merge Sort, Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of Quick Sort that pick pivot in different ways.
8+
9+
- Always pick first element as pivot.
10+
- Always pick last element as pivot (implemented below)
11+
- Pick a random element as pivot.
12+
- Pick median as pivot.
13+
14+
The principle of the quicksort algorithm is this:
15+
16+
- Pick a “pivot” element.
17+
- “Partition” the array into 3 parts:
18+
- First part: all elements in this part is less than the pivot.
19+
- Second part: the pivot itself (only one element!)
20+
- Third part: all elements in this part is greater than or equal to the pivot.
21+
- Then, apply the quicksort algorithm to the first and the third part. (recursively)
22+
23+
![Quick Sort](./images/quick-sort.gif)
24+
25+
#### Complexity Analysis
26+
- Worst Case - O(n<sup>2</sup>)
27+
- Average Case - O(nlogn)
28+
- Best Case - O(nlogn)
29+
30+
### More on this topic
31+
- [Quick Sort - Wikipedia](https://en.wikipedia.org/wiki/Quicksort)
32+
- [Quick Sort - KhanAcademy Tutorial](https://www.khanacademy.org/computing/computer-science/algorithms/quick-sort/a/overview-of-quicksort)
33+
- [Understanding Quicksort (with interactive demo)](http://me.dt.in.th/page/Quicksort/)
34+
- [Quick Sort - HackerEarch Tutorial](https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tutorial/)
35+
- [Sorting - TopCoder Tutorial](https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/)
36+
- [Computer science in JavaScript: Quicksort](https://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/)
137 KB
Loading

Sorting/Quick Sort/quick-sort.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1-
// Merge Sort Implementation
2-
function quickSort(arr, left, right){
3-
var index;
1+
/* Quick Sort Implementation in JavaScript */
42

5-
if(arr.length > 1){
6-
index = partition(arr, left, right);
3+
function quickSort(arr, left, right) {
4+
var index;
75

8-
if(left < index - 1){
9-
quickSort(arr, left, index - 1);
10-
}
6+
//Partition the array and recursively sort them
7+
if (arr.length > 1) {
8+
index = partition(arr, left, right);
119

12-
if(index < right){
13-
quickSort(arr, index, right);
14-
}
15-
}
10+
if (left < index - 1) {
11+
quickSort(arr, left, index - 1);
12+
}
1613

17-
return arr;
14+
if (index < right) {
15+
quickSort(arr, index, right);
16+
}
17+
}
18+
19+
return arr;
1820
}
1921

20-
function partition(arr, left, right){
22+
function partition(arr, left, right) {
2123

22-
var pivot = arr[Math.floor((right+left)/2)];
23-
var i = left, j = right;
24+
var pivot = arr[Math.floor((right + left) / 2)]; //Taking the middle element of the list as Pivot
25+
var i = left,
26+
j = right;
2427

25-
while(i <= j){
26-
while(arr[i] < pivot){
27-
i++;
28-
}
28+
while (i <= j) {
29+
while (arr[i] < pivot) {
30+
i++;
31+
}
2932

30-
while(arr[j] > pivot){
31-
j--;
32-
}
33+
while (arr[j] > pivot) {
34+
j--;
35+
}
3336

34-
if(i <= j){
35-
var tmp = arr[i];
36-
arr[i] = arr[j];
37-
arr[j] = tmp;
37+
//If the elements from the left and right are swappable (means right one is smaller than the pivot and left one is larger than the pivot) then swap them
38+
if (i <= j) {
39+
var tmp = arr[i];
40+
arr[i] = arr[j];
41+
arr[j] = tmp;
3842

39-
i++;
40-
j--;
41-
}
42-
}
43+
i++;
44+
j--;
45+
}
46+
}
4347

44-
return i;
48+
return i;
4549
}
4650

4751
/******************* Testing Quick sort algorithm *********************/
@@ -51,13 +55,13 @@ function partition(arr, left, right){
5155
* Using Math.round() will give you a non-uniform distribution!
5256
*/
5357
function getRandomInt(min, max) {
54-
return Math.floor(Math.random() * (max - min + 1)) + min;
58+
return Math.floor(Math.random() * (max - min + 1)) + min;
5559
}
5660

5761
var arr = [];
5862

59-
for(var i=0;i<10;i++){ //initialize a random integer unsorted array
60-
arr.push(getRandomInt(1, 100));
63+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
64+
arr.push(getRandomInt(1, 100));
6165
}
6266

6367
console.log("Unsorted array: ");

0 commit comments

Comments
 (0)