-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change to better resizable panels library and make default algorithm …
…quicksort
- Loading branch information
Showing
6 changed files
with
62 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
export default function* visualizeJackieSort( | ||
items: number[] | ||
): Generator<VisualizerCommand, void, number[]> { | ||
let clean = false; | ||
export default function* sort(items: number[]): Generator<VisualizerCommand, void, number[]> { | ||
yield* _quicksort(items, 0, items.length - 1); | ||
for (let i = 0; i < items.length; i ++) { | ||
yield cursors([i]); | ||
yield sorted([i]); | ||
} | ||
} | ||
|
||
function* _quicksort(arr: number[], low: number, high: number): Generator<VisualizerCommand, void, number[]> { | ||
arr = yield cursors([low, high]); | ||
if (low < high) { | ||
const [pivotIndex, updatedArr] = yield* partition(arr, low, high); | ||
arr = updatedArr; // update the array after partition | ||
yield* _quicksort(arr, low, pivotIndex - 1); | ||
yield* _quicksort(arr, pivotIndex + 1, high); | ||
} | ||
} | ||
|
||
let sortedCount = 0; | ||
function* partition(arr: number[], low: number, high: number): Generator<VisualizerCommand, [number, number[]], number[]> { | ||
let pivot = arr[high]; | ||
let i = low - 1; | ||
|
||
while (!clean) { | ||
clean = true; | ||
yield notSorted([...Array(items.length).keys()]); | ||
for (let i = 0; i < items.length - 1; i++) { | ||
yield cursors([i, i + 1]); | ||
if (items[i] > items[i + 1]) { | ||
for ( | ||
let j = items.length - ((sortedCount % (items.length - i)) + 1); | ||
j > i; | ||
j-- | ||
) { | ||
yield cursors([i, j]); | ||
if (items[j] < items[i]) { | ||
// yield notSorted([i, j]); | ||
items = yield swap(i, j); | ||
// yield sorted([i, j]); | ||
break; | ||
} | ||
} | ||
sortedCount += 1; | ||
clean = false; | ||
} else yield sorted([i, i + 1]); | ||
for (let j = low; j < high; j++) { | ||
yield cursors([i, j, high]); | ||
if (arr[j] < pivot) { | ||
i++; | ||
arr = yield swap(i, j); | ||
} | ||
} | ||
} | ||
arr = yield swap(i + 1, high); | ||
yield sorted([i + 1]); | ||
return [i + 1, arr]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters