quicksort javascript

Solutions on MaxInterview for quicksort javascript by the best coders in the world

showing results for - "quicksort javascript"
Ashlee
27 Jan 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};
Miguel
03 Oct 2018
1const quicksort = arr =>
2  arr.length <= 1
3    ? arr
4    : [
5        ...quicksort(arr.slice(1).filter((el) => el < arr[0])),
6        arr[0],
7        ...quicksort(arr.slice(1).filter((el) => el >= arr[0])),
8      ];
Diego
23 Jan 2018
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);
Oscar
25 Sep 2018
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]
Jacob
13 Aug 2018
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
quick sort jsjavascript quick sortjavascrip quicksortquick sort code in javascriptquick srot javascriptdemonstrate quicksort javascript and explain when you might use it 3fjavascript quicksort without sortdealing with quicksort in javascriptquick sort javascript codequicksort in javascript quick sort algorithm in javascriptjs quicksort javascriptquicksort algorithm in javascript 3fquicksort jquick sorting javascriptalgorithms in javascript for quicksortquicksort in javascriptsimple quick sort algorithm in javascriptquicksort algorithm jsquicksort algorithm in jsjs quick sortquick sort javasriptquick sort javascrptpsuedo code for quick sort javascriptquick sort algorithm javascriptjavascript quicksortquick sort algorithm implementation javascriptpivot sort javascriptjs qsortimplementing quicksort in javascriptsorting in javascript quick sortjvascript quicksortjavascript quicksort implementationquicksort 28 29 jsquicksort jsquicksort algorithm javascriptjavascript quicksort algorithmquick sort function jshow to do quick sort in javascriptjavascript quicksort codejavascript quicksort examplequick sort algorithm is an example of javascriptcodigo quicksort javascriptjavascript quicksort numbersjavascript quicksort algorithumquicksort program in javascriptfast sorts in jshow to do a quick sort jsjavascript quick sort exam 5bplesort method javascript quicksortjavascript quick sort algorithmcreate quick sort function jsquicksort hoare jsquick sort implementation in javascriptjavascript implementation of quicksort algorithm on a javascript arrayquicksort javasript examplejs quicksortquick sorting in javascriptquick sort algorithm jsquick sort in jsquicksort for beginners javascriptjavascript quicksort wikipediaimplement quick sort using jsquicksort sort in javascriptwhat is quicksort javascriptjs quick sort algorithmquick sort an array javascriptsimple quick sort program in jsquick sort based javascriptquick sort in javascript whats betterquicksort in place javascriptquick sort using comparison function in javscriptquick sort in javascript in build functionquicksort method jsquick sort algorithm in jsquicksort javascript examplequicksort algorithm in javascriptjavascript algorithm for quicksortquicksort example in javascriptquick sort algorithms jsquicksort javasriptarray sorting javascript quick sortquick sort algorithms in javascriptjavascript advanced quicksort solutionquick sort example javascriptquick sort implementation javascriptmy quicksort in jssort an array using quicksort algorithm jsquick sort in js workingsort javascript is using quicksortquick sort example javacriptingquicksort implementation javascriptjavascript implementation of a quick sorthow is an integer array sorted in place using the quicksort algorithm in javascriptquicksort js examplepivot sort algorithm javascriptquicksort javascriptjavascript 2b quick sort examplequick sort algorithm array javascriptquick sort jsquick sort array of words javascriptquicksort example javascriptquicksort javascript definitionquicksort in jsquick sort js codequicksort javascript codequick sort array jsjavascript quicksort functiongeneral quick sort jsquick sort in javascriptquicksort javascript editorquick sort js implementationquick sort algorithm using jsquicksort with javascriptquick sort using javascriptjavascript advanced quicksorthow to use quick sort in javascriptquick sort program javascriptquick sort javascriptsimple quicksort algorithm javascriptquicksort string javascriptquick sort javascript implementationimplement quick sort in javascriptquick sort implementation jsquick sort in javascript in built functionusing quicksort on a user input jsquicksort javascript