-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
215 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,6 @@ | ||
SORTS, IN PAIR | ||
|
||
Instead of mapFn, provide pair array to define sort mapping. | ||
selectionSort, insertionSort, bubbleSort, mergeSort, (are all stable?) | ||
quickSort, shellSort, radixSort, timSort, heapSort | ||
https://en.wikipedia.org/wiki/Sorting_algorithm | ||
https://www.npmjs.com/package/sort-algorithms-js | ||
https://dl.acm.org/doi/10.5555/1778580.1778601 | ||
https://stackoverflow.com/questions/463105/in-place-radix-sort/474040#474040 | ||
|
||
|
||
MINNTH, MAXNTH, SORTEDSLICE (ARRAYVIEW) | ||
|
||
Need only part of sorted array? [bag][nth] ... | ||
Selection algorithm: Quickselect | ||
https://stackoverflow.com/questions/34395680/what-is-the-fastest-way-to-find-nth-biggest-number-of-an-int-array | ||
https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/ | ||
|
||
|
||
PARSE, OTHERS | ||
- (fm, fc)? | ||
|
||
|
||
INDEX -VE | ||
- moveWithin | ||
|
||
|
||
OLD SORT ALGORITHMS | ||
|
||
/** | ||
* Arrange values in order! | ||
* @param x an array (updated!) | ||
* @param fc compare function (a, b) | ||
* @param fm map function (v, i, x) | ||
* @param fs swap function (x, i, j) | ||
* @returns x | x[i] ≤ x[j] ∀ i ≤ j | ||
*/ | ||
function bubbleSort$<T, U=T>(x: T[], fc: CompareFunction<T|U> | null=null, fm: MapFunction<T, T|U> | null=null, fs: SwapFunction<T> | null=null): T[] { | ||
var fc = fc || COMPARE; | ||
var fm = fm || IDENTITY; // TODO: use map function | ||
var fs = fs || swap$; | ||
var X = x.length; | ||
for (var i=0; i<X-1; ++i) { | ||
for (var j=i+1; j<X; ++j) | ||
if (fc(x[i], x[j]) > 0) fs(x, i, j); | ||
} | ||
return x; | ||
} | ||
|
||
|
||
/** | ||
* Arrange values in order! | ||
* @param x an array (updated!) | ||
* @param fc compare function (a, b) | ||
* @param fm map function (v, i, x) | ||
* @param fs swap function (x, i, j) | ||
* @returns x | x[i] ≤ x[j] ∀ i ≤ j | ||
*/ | ||
function selectionSort$<T, U=T>(x: T[], fc: CompareFunction<T|U> | null=null, fm: MapFunction<T, T|U> | null=null, fs: SwapFunction<T> | null=null): T[] { | ||
var fc = fc || COMPARE; | ||
var fm = fm || IDENTITY; // TODO: use map function | ||
var fs = fs || swap$; | ||
var X = x.length; | ||
for (var i=0; i<X-1; ++i) { | ||
var l = i; | ||
for (var j=i+1; j<X; ++j) | ||
if (fc(x[l], x[j]) > 0) l = j; | ||
fs(x, i, l); | ||
} | ||
return x; | ||
} | ||
|
||
|
||
// TODO: Check if this is correct! | ||
/** | ||
* Arrange values in order! | ||
* @param x an array (updated!) | ||
* @param fc compare function (a, b) | ||
* @param fm map function (v, i, x) | ||
* @param fs swap function (x, i, j) | ||
* @returns x | x[i] ≤ x[j] ∀ i ≤ j | ||
*/ | ||
function insertionSortOld$<T, U=T>(x: T[], fc: CompareFunction<T|U> | null=null, fm: MapFunction<T, T|U> | null=null, fs: SwapFunction<T> | null=null): T[] { | ||
var fc = fc || COMPARE; | ||
var fm = fm || IDENTITY; // TODO: use map function | ||
var fs = fs || swap$; // TODO: use swap function | ||
var X = x.length; | ||
for (var i=X-2; i>=0; --i) { | ||
var xv = x[i], mv = x[i]; | ||
for (var j=i+1; j<X; j++) { | ||
if (fc(mv, x[j]) <= 0) break; | ||
x[j-1] = x[j]; | ||
} | ||
x[j-1] = xv; | ||
} | ||
return x; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.