Skip to content

Commit

Permalink
Add quick sort
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Jun 4, 2022
1 parent 1a35ce1 commit e6fa392
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 7 additions & 1 deletion spec/sort.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
merge,
bubbleSort, selectionSort, insertionSort, mergeSort
bubbleSort, selectionSort, insertionSort, mergeSort, quickSort
} from '../src/algorithms/sort.js'

describe("Function", function () {
Expand Down Expand Up @@ -43,4 +43,10 @@ describe("Function", function () {
numbers = mergeSort(numbers);
expect(numbers).toEqual(sorted);
});

it(quickSort.name + " which sorts numbers", function () {
let numbers = createNumbers();
quickSort(numbers, 0, numbers.length - 1);
expect(numbers).toEqual(sorted);
});
});
25 changes: 18 additions & 7 deletions src/algorithms/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,25 @@ export function merge(left, right) {
return array;
}

export function quickSort(array, left, right) {
if (left >= right) return;

// bubbleSort(numbers);
// selectionSort(numbers);
// insertionSort(numbers);
// console.log(numbers);
let start = left;
let end = right;

// merge([1, 2, 5, 6, 44, 99], [0, 4, 63, 87, 283])
let pivot = array[right]
while (left < right) {
if (array[left] > pivot) {
array[right] = array[left];
array[left] = array[right - 1];
array[right - 1] = pivot;

// const answer = mergeSort(numbers);
right--;
} else {
left++;
}
}

// console.log(answer);
quickSort(array, start, left - 1)
quickSort(array, left + 1, end)
}

0 comments on commit e6fa392

Please sign in to comment.