Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript quicksort

Tags:

I have been looking around the web for a while and I am wondering if there is a 'stable' defacto implementation of quicksort that is generally used? I can write my own but why reinvent the wheel...

like image 636
flavour404 Avatar asked Mar 03 '11 19:03

flavour404


People also ask

Does Javascript use quicksort?

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.

What is the fastest sorting algorithm in Javascript?

Quick Sort Algorithm Quicksort is one of the most efficient ways of sorting elements in computer systems. Similor to merge sort, Quicksort works on the divide and conquer algorithm.

Is quicksort faster than merge?

Quick sort is more efficient and works faster than merge sort in case of smaller array size or datasets.

What is quick sort example?

Example of Quick Sort:Comparing 44 to the right-side elements, and if right-side elements are smaller than 44, then swap it. As 22 is smaller than 44 so swap them. Now comparing 44 to the left side element and the element must be greater than 44 then swap them. As 55 are greater than 44 so swap them.


1 Answers

Quicksort (recursive)

function quicksort(array) {    if (array.length <= 1) {      return array;    }      var pivot = array[0];        var left = [];     var right = [];      for (var i = 1; i < array.length; i++) {      array[i] < pivot ? left.push(array[i]) : right.push(array[i]);    }      return quicksort(left).concat(pivot, quicksort(right));  };    var unsorted = [23, 45, 16, 37, 3, 99, 22];  var sorted = quicksort(unsorted);    console.log('Sorted array', sorted);
like image 107
Benny Neugebauer Avatar answered Oct 20 '22 04:10

Benny Neugebauer