fix pivot picking and kruskal's algorithm

This commit is contained in:
Lars Kotthoff 2018-12-11 10:49:49 -07:00
parent f4668db094
commit 478abbadc9
2 changed files with 9 additions and 5 deletions

View File

@ -24,7 +24,7 @@ export default function kruskal(graph) {
*/ */
compareCallback: (graphEdgeA, graphEdgeB) => { compareCallback: (graphEdgeA, graphEdgeB) => {
if (graphEdgeA.weight === graphEdgeB.weight) { if (graphEdgeA.weight === graphEdgeB.weight) {
return 1; return 0;
} }
return graphEdgeA.weight <= graphEdgeB.weight ? -1 : 1; return graphEdgeA.weight <= graphEdgeB.weight ? -1 : 1;

View File

@ -22,11 +22,15 @@ export default class QuickSort extends Sort {
// Take the median element of first, mid, and last elements. // Take the median element of first, mid, and last elements.
let pivotElement = array[0]; let pivotElement = array[0];
const mid = Math.floor(array.length / 2); const mid = Math.floor(array.length / 2);
if ((array[mid] < array[array.length - 1] && array[mid] > array[0]) if ((this.comparator.lessThan(array[mid], array[array.length - 1])
|| (array[mid] > array[array.length - 1] && array[mid] < array[0])) { && 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]; pivotElement = array[mid];
} else if ((array[array.length - 1] < array[mid] && array[array.length - 1] > array[0]) } else if ((this.comparator.lessThan(array[array.length - 1], array[mid])
|| (array[array.length - 1] > array[mid] && array[array.length - 1] < array[0])) { && 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]; pivotElement = array[array.length - 1];
} }