mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
fix pivot picking and kruskal's algorithm
This commit is contained in:
parent
f4668db094
commit
478abbadc9
@ -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;
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user