Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: larskotthoff/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on Aug 15, 2018

  1. Copy the full SHA
    d4af68b View commit details

Commits on Dec 11, 2018

  1. Copy the full SHA
    f4668db View commit details
  2. Copy the full SHA
    478abba View commit details
Showing with 22 additions and 10 deletions.
  1. +1 −1 src/algorithms/graph/kruskal/kruskal.js
  2. +17 −5 src/algorithms/sorting/quick-sort/QuickSort.js
  3. +4 −4 src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js
2 changes: 1 addition & 1 deletion src/algorithms/graph/kruskal/kruskal.js
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export default function kruskal(graph) {
*/
compareCallback: (graphEdgeA, graphEdgeB) => {
if (graphEdgeA.weight === graphEdgeB.weight) {
return 1;
return 0;
}

return graphEdgeA.weight <= graphEdgeB.weight ? -1 : 1;
22 changes: 17 additions & 5 deletions src/algorithms/sorting/quick-sort/QuickSort.js
Original file line number Diff line number Diff line change
@@ -14,13 +14,25 @@ export default class QuickSort extends Sort {
return array;
}

// Init left and right arrays.
// Init left, center, and right arrays.
const leftArray = [];
const rightArray = [];

// Take the first element of array as a pivot.
const pivotElement = array.shift();
const centerArray = [pivotElement];
const centerArray = [];

// Take the median element of first, mid, and last elements.
let pivotElement = array[0];
const mid = Math.floor(array.length / 2);
if ((this.comparator.lessThan(array[mid], array[array.length - 1])
&& this.comparator.greaterThan(array[mid], array[0]))
|| (this.comparator.greaterThan(array[mid], array[array.length - 1])
&& this.comparator.lessThan(array[mid], array[0]))) {
pivotElement = array[mid];
} else if ((this.comparator.lessThan(array[array.length - 1], array[mid])
&& this.comparator.greaterThan(array[array.length - 1], array[0]))
|| (this.comparator.greaterThan(array[array.length - 1], array[mid])
&& this.comparator.lessThan(array[array.length - 1], array[0]))) {
pivotElement = array[array.length - 1];
}

// Split all array elements between left, center and right arrays.
while (array.length) {
8 changes: 4 additions & 4 deletions src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js
Original file line number Diff line number Diff line change
@@ -8,10 +8,10 @@ import {
} from '../../SortTester';

// Complexity constants.
const SORTED_ARRAY_VISITING_COUNT = 190;
const NOT_SORTED_ARRAY_VISITING_COUNT = 62;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 190;
const EQUAL_ARRAY_VISITING_COUNT = 19;
const SORTED_ARRAY_VISITING_COUNT = 66;
const NOT_SORTED_ARRAY_VISITING_COUNT = 83;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 66;
const EQUAL_ARRAY_VISITING_COUNT = 20;

describe('QuickSort', () => {
it('should sort array', () => {