adds bubble sort

This commit is contained in:
ergz
2023-06-27 20:05:57 -07:00
parent 2ade90abc6
commit 2fef9a3d05
3 changed files with 58 additions and 0 deletions

18
ts/bubble_sort.test.ts Normal file
View File

@@ -0,0 +1,18 @@
function bubble_sort(arr: number[]): void {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
const t = arr[j+1];
arr[j+1] = arr[j];
arr[j] = t;
}
}
}
}
test("bubble sort works", () => {
const a = [2, 1, 3, 4, 5];
bubble_sort(a);
expect(a).toEqual([1, 2, 3, 4, 5]);
})