sync
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { MaybeAsyncComparer } from "./types.js";
|
||||
import { reverseAsyncComparer } from "./utils.js";
|
||||
import { asAsyncComparer } from "./comparer/async.js";
|
||||
import { MaybeAsyncComparisonOrComparer, AsyncComparer } from "./comparer/types.js";
|
||||
|
||||
export interface AsyncSorter {
|
||||
sort<T>(array: T[], descending: boolean, comparer: MaybeAsyncComparer<T>): Promise<void>;
|
||||
sort<T>(array: T[], descending: boolean, comparer: MaybeAsyncComparisonOrComparer<T>): Promise<void>;
|
||||
}
|
||||
|
||||
function swap(array: any[], i: number, j: number) {
|
||||
@@ -12,20 +12,26 @@ function swap(array: any[], i: number, j: number) {
|
||||
}
|
||||
|
||||
abstract class BaseAsyncSorter implements AsyncSorter {
|
||||
async sort<T>(array: T[], descending: boolean, comparer: MaybeAsyncComparer<T>) {
|
||||
await this._sort(array, descending ? reverseAsyncComparer(comparer) : comparer);
|
||||
async sort<T>(array: T[], descending: boolean, comparer: MaybeAsyncComparisonOrComparer<T>) {
|
||||
comparer = asAsyncComparer(comparer);
|
||||
|
||||
if (descending) {
|
||||
comparer = comparer.reverse();
|
||||
}
|
||||
|
||||
await this._sort(array, comparer);
|
||||
}
|
||||
|
||||
protected abstract _sort<T>(array: T[], comparer: MaybeAsyncComparer<T>): Promise<void>;
|
||||
protected abstract _sort<T>(array: T[], comparer: AsyncComparer<T>): Promise<void>;
|
||||
}
|
||||
|
||||
class InsertionSorter extends BaseAsyncSorter {
|
||||
protected override async _sort<T>(array: T[], comparer: MaybeAsyncComparer<T>) {
|
||||
export const insertionSorter: AsyncSorter = new class InsertionSorter extends BaseAsyncSorter {
|
||||
protected override async _sort<T>(array: T[], comparer: AsyncComparer<T>) {
|
||||
for (let i = 1; i < array.length; i++) {
|
||||
const obj = array[i];
|
||||
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
if (await comparer(obj, array[j]) < 0) {
|
||||
if (await comparer.compare(obj, array[j]) < 0) {
|
||||
for (let k = i; k > j; k--) {
|
||||
swap(array, k - 1, k);
|
||||
}
|
||||
@@ -35,10 +41,10 @@ class InsertionSorter extends BaseAsyncSorter {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class SelectionSorter extends BaseAsyncSorter {
|
||||
protected async _sort<T>(array: T[], comparer: MaybeAsyncComparer<T>) {
|
||||
export const selectionSorter: AsyncSorter = new class SelectionSorter extends BaseAsyncSorter {
|
||||
protected async _sort<T>(array: T[], comparer: AsyncComparer<T>) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let smallest = array[i];
|
||||
let smallestIndex = i;
|
||||
@@ -46,7 +52,7 @@ class SelectionSorter extends BaseAsyncSorter {
|
||||
for (let j = i; j < array.length; j++) {
|
||||
const current = array[j];
|
||||
|
||||
if (await comparer(current, smallest) < 0) {
|
||||
if (await comparer.compare(current, smallest) < 0) {
|
||||
smallest = current;
|
||||
smallestIndex = j;
|
||||
}
|
||||
@@ -57,10 +63,10 @@ class SelectionSorter extends BaseAsyncSorter {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class BubbleSorter extends BaseAsyncSorter {
|
||||
protected async _sort<T>(array: T[], comparer: MaybeAsyncComparer<T>) {
|
||||
export const bubbleSorter: AsyncSorter = new class BubbleSorter extends BaseAsyncSorter {
|
||||
protected async _sort<T>(array: T[], comparer: AsyncComparer<T>) {
|
||||
const length = array.length;
|
||||
|
||||
for (let k = length - 2; k >= 0; k--) {
|
||||
@@ -70,7 +76,7 @@ class BubbleSorter extends BaseAsyncSorter {
|
||||
const j = i + 1;
|
||||
const a = array[i], b = array[j];
|
||||
|
||||
if (await comparer(a, b) > 0) {
|
||||
if (await comparer.compare(a, b) > 0) {
|
||||
swap(array, i, j);
|
||||
hasSwaped = true;
|
||||
}
|
||||
@@ -81,8 +87,4 @@ class BubbleSorter extends BaseAsyncSorter {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const insertionSorter: AsyncSorter = new InsertionSorter();
|
||||
export const selectionSorter: AsyncSorter = new SelectionSorter();
|
||||
export const bubbleSorter: AsyncSorter = new BubbleSorter();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user