## DEV Community 👩‍💻👨‍💻 is a community of 917,994 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. Mysterio

Posted on • Updated on

# QuickSort Algorithm in Javascript

Hello Everyone Today I am going to show you how to write QuickSort Algorithm in Javascript.

QuickSort 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 quickSort that pick pivot in different ways.

Always pick first element as pivot.
Always pick last element as pivot (implemented below)
Pick a random element as pivot.
Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time. Here is The Code Part -

``````function QuickSort(Arr){
if(Arr.length <= 1){
return Arr;
}

const pivot = Arr[Arr.length - 1];
const leftArr = [];
const rightArr = [];

for(let i=0; i < Arr.length-1;i++){
Arr[i] < pivot ? leftArr.push(Arr[i]) :  rightArr.push(Arr[i])
}

return [...QuickSort(leftArr) ,pivot,...QuickSort(rightArr)];

}

const items = [1,5,2,99,81,100,144,121,91,85,74,10];
console.log(QuickSort(items));
``````
1. So, First we will check the length of the Array and if it is 1 then we will return the array as it is.
2. Then we will select a pivot element which is last element in our case.
3. Then we will create two empty arrays leftarr and rightarr to compare elements with pivot and place the element accodingly.
4. Then we will iterate the array using for loop and inside for loop we will check each element that it is smaller than pivot or greater than pivot
5. If the Element is smaller than pivot, then we will push it into left array and if the element is greater than pivot then we will push the element into right array.
6. Then we will recursively call QuickSort for left and right array to partition the arrays until it get sorted completely.
``````Output -
[1,2,5,10,74,81,85,91,99,100,121,144]
``````

I am New to Data Structures and Algorithm.So, if you find any mistake in this post please correct it in the comment section
THANK YOU

Instagram - https://instagram.com/w_a_a_d_u__h_e_c_k

## Top comments (24) Luke Shiru

In JavaScript at least, I recommend you use the native `Array.prototype.sort`, instead of implementing your own. Performance is also better. You'll only have to do:

``````items.sort((a, b) => a - b);
``````

Cheers! Tbf - Although I agree with you in "the real world" I read this as "How to create a quicksort algorithm using Javascript" - Which this article does.

It is not a "How to sort an array using Javascript" to which your comment is related. Luke Shiru

My comment was mainly addressing this part of the post:

I am New to Data Structures and Algorithm. So, if you find any mistake in this post please correct it in the comment section.

So I let him know that in JS, at least, it doesn't make much sense to implement your own sort ☺️ Mysterio

Yeah But Data Structure is important because Understanding the logic of One algorithm can help you to write more algorithms related to your problems Ankit Tanna

That's a narrow minded thinking. If there are 100 thousand elements you would ha e to resort to a proper algorithm. Luke Shiru • Edited on

If you have to sort 100 thousand elements with JS, then the problem is elsewhere...

PS: BTW, with 100 thousand elements the `quickSort` approach exceeds the max call stack, but still with `1000` elements, `Array.prototype.sort` is still faster (200 op/s vs ~84k op/s). Mysterio

Brother its just a Simple Quick sort algorithm
Code for learning purpose
I didn't said it is better than sort method or any other sorting Algorithm
It is a just part of the DSA which I showed here and as I am learning Javascript also
So, for practice purpose I used Javascript to demonstrate the implementation of this Algorithm. Luke Shiru

I know, and I'm not saying that is something bad. At the end of your post you wrote:

So, if you find any mistake in this post please correct it in the comment section

So I commented that in JS is way faster to just use the native `Array.prototype.sort` than creating custom algorithms for that. Is actually interesting to see how you'll do a quick sort in JS, is just not practical. The user Ankit Tanna said that this algorithm is better for 100 thosand elements, which was an incorrect assertion and I just corrected him, but I have nothing against the content of the actual post :) Mysterio

Yeah I also use Array.prototype.sort method and it is easy to use and good Mysterio

can you show this correction in code as i am not getting the point😆 Sure,

``````class QuickSort {
public sort(array: number[], left: number, right: number): number[] {
if (array.length <= 1) {
return array;
}

let index = this.partition(array, left, right);

if (left < index - 1) {
this.sort(array, left, index - 1);
}

if (index < right) {
this.sort(array, index, right);
}

return array;
}
private partition(array: number[], left: number, right: number): number {
const pivot = array[Math.floor(right + left / 2)];
let i = left;
let j = right;

while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}

if (i <= j) {
this.swap(array, i, j);
console.log(`swapping \${i} \${j}`, array);
i++;
j--;
}
}

return i;
}

private swap(array: number[], i: number, j: number) {
const aux = array[i];
array[i] = array[j];
array[j] = aux;
}
}

const q = new QuickSort();
const array = [5, 6, 3, 7, 1, 8, 9];

console.log(q.sort(array, 0, array.length - 1));

``````