DEV Community

Cover image for QuickSort Algorithm in Javascript

QuickSort Algorithm in Javascript

Shubham Tiwari on August 28, 2021

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 ...
Collapse
 
rrdlpl profile image
R

isn't the point of quicksort that you don't need extra memory? left and rightArray aren't necessary imo.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

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

Collapse
 
rrdlpl profile image
R

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));

Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

So this one is memory efficient

Collapse
 
owenmelbz profile image
Owen Melbourne

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.

Collapse
 
timothyokooboh profile image
timothyokooboh

This is by far the best implementation of quick sort (in terms of simplicity and comprehension) that I have seen. Thank you very much.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you sir

Collapse
 
absphreak profile image
ᴀʙʜɪɴᴀᴠ sʜᴀʀᴍᴀ✩

Good use of destructuring with recursion.

Collapse
 
goodlifeinc profile image
DJango Freeman

maybe u meant spread with recursion :)

Collapse
 
owenmelbz profile image
Owen Melbourne

Maybe you meant spread with recursion? 🙂

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

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

 
shubhamtiwari909 profile image
Shubham Tiwari

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.

Collapse
 
ankittanna profile image
Ankit Tanna

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

Collapse
 
umar9780 profile image
محمد عمر

😂

Collapse
 
umar9780 profile image
محمد عمر

legand 😂

Collapse
 
notruelimit profile image
Uros Mitrovic

You have an objectively better method, never reinvent the wheel

Collapse
 
hadihammurabi profile image
Hadi Hidayat Hammurabi

Yeesss... It is more quick i think 😄

 
shubhamtiwari909 profile image
Shubham Tiwari

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