Skip to content

Commit f21e24e

Browse files
committed
up sorting algorithm insertion function
1 parent 4e92f89 commit f21e24e

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

src/algorithm/sorting/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
import { bubbleSort, bubbleSortV2 } from './bubble-sort';
1+
import { bubbleSort, bubbleSortV2 } from './bubble-sort';
2+
import { selectionSort, selectionSortCallback } from './selection-sort';
3+
4+
export { bubbleSort, bubbleSortV2, selectionSort, selectionSortCallback };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, it, expect } from "bun:test";
2+
import { insertionSort, insertionSortCallback } from "./insertion-sort";
3+
4+
describe('Insertion Sort Functions', () => {
5+
const unsortedArray = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
6+
const sortedArrayAsc = [0, 1, 2, 4, 5, 6, 44, 63, 87, 99, 283];
7+
const sortedArrayDesc = [...sortedArrayAsc].reverse();
8+
9+
describe('insertionSort', () => {
10+
it('should sort the array in ascending order', () => {
11+
const result = insertionSort([...unsortedArray]); // Spread to avoid mutation
12+
expect(result).toEqual(sortedArrayAsc);
13+
});
14+
15+
it('should handle an already sorted array', () => {
16+
const result = insertionSort([...sortedArrayAsc]);
17+
expect(result).toEqual(sortedArrayAsc);
18+
});
19+
20+
it('should handle an empty array', () => {
21+
const result = insertionSort([]);
22+
expect(result).toEqual([]);
23+
});
24+
25+
it('should handle an array with one element', () => {
26+
const result = insertionSort([42]);
27+
expect(result).toEqual([42]);
28+
});
29+
30+
it('should handle an array with duplicate elements', () => {
31+
const result = insertionSort([3, 1, 2, 3, 3, 0]);
32+
expect(result).toEqual([0, 1, 2, 3, 3, 3]);
33+
});
34+
});
35+
36+
describe('insertionSortCallback', () => {
37+
it('should sort the array in ascending order using a callback', () => {
38+
const result = insertionSortCallback([...unsortedArray], (a, b) => a - b);
39+
expect(result).toEqual(sortedArrayAsc);
40+
});
41+
42+
it('should sort the array in descending order using a callback', () => {
43+
const result = insertionSortCallback([...unsortedArray], (a, b) => b - a);
44+
expect(result).toEqual(sortedArrayDesc);
45+
});
46+
47+
it('should handle an already sorted array in ascending order', () => {
48+
const result = insertionSortCallback([...sortedArrayAsc], (a, b) => a - b);
49+
expect(result).toEqual(sortedArrayAsc);
50+
});
51+
52+
it('should handle an already sorted array in descending order', () => {
53+
const result = insertionSortCallback([...sortedArrayDesc], (a, b) => b - a);
54+
expect(result).toEqual(sortedArrayDesc);
55+
});
56+
57+
it('should handle an empty array', () => {
58+
const result = insertionSortCallback([], (a, b) => a - b);
59+
expect(result).toEqual([]);
60+
});
61+
62+
it('should handle an array with one element', () => {
63+
const result = insertionSortCallback([42], (a, b) => a - b);
64+
expect(result).toEqual([42]);
65+
});
66+
67+
it('should handle an array with duplicate elements', () => {
68+
const result = insertionSortCallback([3, 1, 2, 3, 3, 0], (a, b) => a - b);
69+
expect(result).toEqual([0, 1, 2, 3, 3, 3]);
70+
});
71+
});
72+
});
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
2+
3+
export function insertionSort(array: number[]) {
4+
//Code Here
5+
const length = array.length
6+
for (let i = 0; i < length; i++) {
7+
for (let j = i; j >= 0; j--) {
8+
// Trường hợp xấu nhất là lặp đến j = 0 => arrar[j - 1] là undefined => Vẫn đúng
9+
if (array[j] < array[j - 1]) {
10+
let temp = array[j]
11+
array[j] = array[j - 1]
12+
array[j - 1] = temp
13+
} else {
14+
break;
15+
}
16+
}
17+
}
18+
return array
19+
}
20+
21+
export function insertionSortCallback(array: number[], callback: (a: number, b: number) => number) {
22+
//Code Here
23+
const length = array.length
24+
for (let i = 0; i < length; i++) {
25+
for (let j = i; j >= 0; j--) {
26+
if (callback(array[j], array[j - 1]) < 0) {
27+
let temp = array[j]
28+
array[j] = array[j - 1]
29+
array[j - 1] = temp
30+
} else {
31+
break;
32+
}
33+
}
34+
}
35+
return array
36+
}
37+
38+
// console.log(insertionSort(numbers));
39+
console.log(insertionSortCallback(numbers, (a, b) => b - a));

src/algorithm/sorting/selection-sort.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ export function selectionSort(array: number[]) {
1414
}
1515
}
1616
// Swap the found minimum element with the first element
17-
let temp = array[i]
18-
array[i] = minItem
19-
array[minIndex] = temp
17+
if (i !== minIndex) {
18+
let temp = array[i]
19+
array[i] = minItem
20+
array[minIndex] = temp
21+
}
2022
}
2123
return array
2224
}

0 commit comments

Comments
 (0)