You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*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)*
**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)
Copy file name to clipboardExpand all lines: Sorting/Quick Sort/quick-sort.js
+39-35Lines changed: 39 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -1,47 +1,51 @@
1
-
// Merge Sort Implementation
2
-
functionquickSort(arr,left,right){
3
-
varindex;
1
+
/* Quick Sort Implementation in JavaScript */
4
2
5
-
if(arr.length>1){
6
-
index=partition(arr,left,right);
3
+
functionquickSort(arr,left,right){
4
+
varindex;
7
5
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);
11
9
12
-
if(index<right){
13
-
quickSort(arr,index,right);
14
-
}
15
-
}
10
+
if(left<index-1){
11
+
quickSort(arr,left,index-1);
12
+
}
16
13
17
-
returnarr;
14
+
if(index<right){
15
+
quickSort(arr,index,right);
16
+
}
17
+
}
18
+
19
+
returnarr;
18
20
}
19
21
20
-
functionpartition(arr,left,right){
22
+
functionpartition(arr,left,right){
21
23
22
-
varpivot=arr[Math.floor((right+left)/2)];
23
-
vari=left,j=right;
24
+
varpivot=arr[Math.floor((right+left)/2)];//Taking the middle element of the list as Pivot
25
+
vari=left,
26
+
j=right;
24
27
25
-
while(i<=j){
26
-
while(arr[i]<pivot){
27
-
i++;
28
-
}
28
+
while(i<=j){
29
+
while(arr[i]<pivot){
30
+
i++;
31
+
}
29
32
30
-
while(arr[j]>pivot){
31
-
j--;
32
-
}
33
+
while(arr[j]>pivot){
34
+
j--;
35
+
}
33
36
34
-
if(i<=j){
35
-
vartmp=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
0 commit comments