quick sort js

Solutions on MaxInterview for quick sort js by the best coders in the world

showing results for - "quick sort js"
Bastien
15 Mar 2019
1function quicksort(array) {
2  if (array.length <= 1) {
3    return array;
4  }
5
6  var pivot = array[0];
7  
8  var left = []; 
9  var right = [];
10
11  for (var i = 1; i < array.length; i++) {
12    array[i] < pivot ? left.push(array[i]) : right.push(array[i]);
13  }
14
15  return quicksort(left).concat(pivot, quicksort(right));
16};
Fabian
29 May 2017
1const quickSort = array =>
2  (function qsort(arr, start, end) {
3    if (start >= end) return arr;
4    let swapPos = start;
5
6    for (let i = start; i <= end; i++) {
7      if (arr[i] <= arr[end]) {
8        [arr[swapPos], arr[i]] = [arr[i], arr[swapPos]];
9        swapPos++;
10      }
11    }
12    qsort(arr, start, --swapPos - 1);
13    qsort(arr, swapPos + 1, end);
14
15    return arr;
16  })(Array.from(array), 0, array.length - 1);
Margaux
05 Jun 2020
1var items = [5, 3, 7, 6, 2, 9];
2
3function swap(items, leftIndex, rightIndex) {
4  var temp = items[leftIndex];
5  items[leftIndex] = items[rightIndex];
6  items[rightIndex] = temp;
7}
8
9function partition(items, left, right) {
10  var pivot = items[Math.floor((right + left) / 2)], //middle element
11    i = left, //left pointer
12    j = right; //right pointer
13  while (i <= j) {
14    while (items[i] < pivot) {
15      i++;
16    }
17    while (items[j] > pivot) {
18      j--;
19    }
20    if (i <= j) {
21      swap(items, i, j); //sawpping two elements
22      i++;
23      j--;
24    }
25  }
26  return i;
27}
28
29function quickSort(items, left, right) {
30  var index;
31  if (items.length > 1) {
32    index = partition(items, left, right); //index returned from partition
33    if (left < index - 1) {
34      //more elements on the left side of the pivot
35      quickSort(items, left, index - 1);
36    }
37    if (index < right) {
38      //more elements on the right side of the pivot
39      quickSort(items, index, right);
40    }
41  }
42  return items;
43}
44
45// first call to quick sort
46var sortedArray = quickSort(items, 0, items.length - 1);
47console.log(sortedArray); //prints [2,3,5,6,7,9]
Alix
17 Jul 2019
1var items = [5,3,7,6,2,9];
2function swap(items, leftIndex, rightIndex){
3    var temp = items[leftIndex];
4    items[leftIndex] = items[rightIndex];
5    items[rightIndex] = temp;
6}
7function partition(items, left, right) {
8    var pivot   = items[Math.floor((right + left) / 2)], //middle element
9        i       = left, //left pointer
10        j       = right; //right pointer
11    while (i <= j) {
12        while (items[i] < pivot) {
13            i++;
14        }
15        while (items[j] > pivot) {
16            j--;
17        }
18        if (i <= j) {
19            swap(items, i, j); //sawpping two elements
20            i++;
21            j--;
22        }
23    }
24    return i;
25}
26
27function quickSort(items, left, right) {
28    var index;
29    if (items.length > 1) {
30        index = partition(items, left, right); //index returned from partition
31        if (left < index - 1) { //more elements on the left side of the pivot
32            quickSort(items, left, index - 1);
33        }
34        if (index < right) { //more elements on the right side of the pivot
35            quickSort(items, index, right);
36        }
37    }
38    return items;
39}
40// first call to quick sort
41var sortedArray = quickSort(items, 0, items.length - 1);
42console.log(sortedArray); //prints [2,3,5,6,7,9]
43
queries leading to this page
javascript 2b quick sort examplearray sorting javascript quick sortquick sort implementation in javascriptquicksort in javascript quicksort implementation javascriptquicksort in place javascriptquick sort algorithm jsquicksort algorithm in javascripthow to do quick sort in javascriptquicksort for beginners javascriptimplement quick sort using jsquicksort jquicksort hoare jsquick sort in javascript whats betterquicksort javascript codequick sort jsquicksort js effiecntjavascript implementation of quicksort algorithm on a javascript arraydemonstrate quicksort javascript and explain when you might use it 3fquicksort algorithm in jsquicksort algorithm jsquicksort javasript examplequick sort example javacriptingjavascript quick sort algorithmsort method javascript quicksortjavascript quicksort numbersquicksort algorithm in javascript 3fimplement quick sort in javascriptfast sorts in jsquick sort implementation javascriptpivot sort algorithm javascripthow to use javascript quicksortjavascript implementation of a quick sortquick sort in javascript in built functionquicksort 28 29 jshow is an integer array sorted in place using the quicksort algorithm in javascriptquick sort in javascript in build functionquicksort jsjavascript quicksortpsuedo code for quick sort javascriptquicksort example in javascriptquick sort js implementationquick sort array jsjavascript quicksort implementationquick sort in jsquick sort based javascriptquicksort in javascriptquicksort with javascriptquick sort javascript implementationquick sort algorithm is an example of javascriptquick sort js codesimple quicksort javascriptquicksort javascriptquick sort in js workingjavascript quicksort algorithmquick sorting in javascriptusing quicksort on a user input jsquick sort using comparison function in javscriptsort javascript is using quicksortquick sort algorithms in javascriptjavascrip quicksortjavascript quicksort wikipediaquicksort sort in javascripthow to do a quick sort jsquick sorting javascriptquick sort an array javascriptquick sort algorithms jsjs quick sort algorithmquicksort algorithm code example javascriptsimple quick sort program in jscreate quick sort function jsquick sort code in javascriptjavascript quicksort functionjavascript quicksort codequicksort javasriptquicksort javascript examplequicksort in jssimple quicksort algorithm javascriptquick sort algorithm in javascriptimplementing quicksort in javascriptquick sort javascript codejavascript quicksort examplegeneral quick sort jsquick sort javasriptquicksort string javascriptquick sort algorithm implementation javascriptquicksort javascript editorhow to create a quicksort algorithme javascriptquicksort javascript definitionjs qsorthow to implement quick sort in javascriptquick srot javascriptsort an array using quicksort algorithm jsquick sort example javascriptjavascript advanced quicksortquick sort algorithm array javascriptmy quicksort in jsquick sort algorithm javascriptquick sort using javascripthow to use quick sort in javascriptquick sort algorithm using jsquick sort array of words javascriptquicksort algorithm javascriptjavascript quicksort without sortjs quicksortpivot sort javascriptsorting in javascript quick sortcodigo quicksort javascriptquicksort js examplequick sort jsquicksort javascript es6quick sort javascrptquicksort method jsquicksort jsquick sort algorithm in jshow to create a quicksort in javascriptquick sort program javascriptquicksort example javascriptquick sort function javascritsimple quick sort algorithm in javascriptjs quicksort javascriptjavascript quick sort exam 5bplealgorithms in javascript for quicksortdealing with quicksort in javascriptjavascript quick sortquick sort in javascriptwhat is quicksort javascriptquick sort function jsquicksort program in javascriptquick sort implementation jsunderstanding quick sort via javascriptjs quick sortquick sort javascriptquick sort js