[solution] insertion sort words add

main
Vasily Guzov 1 year ago
parent 3f306706c8
commit 37da2c85ec

@ -3,6 +3,9 @@ const SORT_STR = "eiinnoorrsstt";
const NUMB = [12, -2, 55, 68, 80]; const NUMB = [12, -2, 55, 68, 80];
const SORT_NUMB = [-2, 12, 55, 68, 80]; const SORT_NUMB = [-2, 12, 55, 68, 80];
const WORDS = ["strc", "avgrs", "bds"];
const SORT_WORDS = ["avgrs", "bds", "strc"];
function swapCharsInString(str: string, i: number, j: number) { function swapCharsInString(str: string, i: number, j: number) {
return ( return (
str.substring(0, i) + str.substring(0, i) +
@ -13,14 +16,19 @@ function swapCharsInString(str: string, i: number, j: number) {
); );
} }
const swapElements = (arr: number[], index1: number, index2: number) => { const swapElements = (
arr: number[] | string[],
index1: number,
index2: number,
) => {
[arr[index1], arr[index2]] = [arr[index2], arr[index1]]; [arr[index1], arr[index2]] = [arr[index2], arr[index1]];
}; };
export function insertionSort(str: string): string; export function insertionSort(str: string): string;
export function insertionSort(arr: number[]): number[]; export function insertionSort(arr: number[]): number[];
export function insertionSort(arr: string[]): string[];
export function insertionSort(sortItems: string | number[]) { export function insertionSort(sortItems: string | number[] | string[]) {
let L: number, R: number; let L: number, R: number;
for (L = 1; L < sortItems.length; L++) { for (L = 1; L < sortItems.length; L++) {
@ -47,3 +55,4 @@ export function insertionSort(sortItems: string | number[]) {
console.log(insertionSort(STR), SORT_STR); console.log(insertionSort(STR), SORT_STR);
console.log(insertionSort(NUMB), SORT_NUMB); console.log(insertionSort(NUMB), SORT_NUMB);
console.log(insertionSort(WORDS), SORT_WORDS);

Loading…
Cancel
Save