From 1c1eb9e57b618d87d51e89e220b27435917e297c Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sun, 13 Oct 2024 14:45:05 +0800 Subject: [PATCH] Switch to using random to select for extend --- gabau.github.io/src/pages/fun/SortVisPage.tsx | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/gabau.github.io/src/pages/fun/SortVisPage.tsx b/gabau.github.io/src/pages/fun/SortVisPage.tsx index b865a40..034168e 100644 --- a/gabau.github.io/src/pages/fun/SortVisPage.tsx +++ b/gabau.github.io/src/pages/fun/SortVisPage.tsx @@ -204,29 +204,14 @@ class ExtendedPartitionSortState extends PartitionSortState { // only add parititions when the partitions are not sorted addPartitions(): boolean { - return !this.sorted; + return true; } getNextStep(values: number[]): number[] | "finished" { if (this.state === "checkingSorted") { - if (this.currPos >= this.end || - this.frontPointer >= this.end || - this.start >= this.end) { - - // we have finished sorting this entire partition - this.state = "sorting"; - this.sorted = true; - this.currPos = this.start + 1; - if (this.partitions.length === 0) return "finished"; - return values; - } else if (values[this.currPos - 1] > values[this.currPos]) { - // need to sort partition - this.state = "sorting"; - this.sorted = false; - this.currPos = this.start + 1; - return values; - } - this.currPos += 1; + this.state = "sorting"; + if (this.frontPointer < this.end) + this.swap(values, this.frontPointer, Math.round(Math.random() * (this.end - 1 - this.start)) + this.start); return values; } return super.getNextStep(values);