diff --git a/README.md b/README.md index 785c08a..2068996 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ It includes code and patterns from the "JavaScript Algorithms" repository, trans ### Sort - [QuickSort](src/algorithms/sort/quick-sort/quick-sort.ts) +- [BubbleSort](src/algorithms/sort/bubble-sort/bubble-sort.ts) +- [BubbleSortSimple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts) ## Utils diff --git a/src/algorithms/sort/bubble-sort/bubble-sort-simple.test.ts b/src/algorithms/sort/bubble-sort/bubble-sort-simple.test.ts new file mode 100644 index 0000000..2c0b59d --- /dev/null +++ b/src/algorithms/sort/bubble-sort/bubble-sort-simple.test.ts @@ -0,0 +1,50 @@ +import { bubbleSortSimple } from "./bubble-sort-simple"; + +describe("bubbleSortSimple", () => { + test.each([ + { + title: "sorts array with negative numbers", + input: [-5, -2, -8, -1, -9], + expected: [-9, -8, -5, -2, -1], + }, + { + title: "sorts array with mixed positive and negative numbers", + input: [3, -1, 0, 4, -5, 2], + expected: [-5, -1, 0, 2, 3, 4], + }, + { + title: "handles array with duplicate numbers", + input: [4, 2, 4, 1, 2, 3], + expected: [1, 2, 2, 3, 4, 4], + }, + { + title: "handles single element array", + input: [1], + expected: [1], + }, + { + title: "handles empty array", + input: [], + expected: [], + }, + { + title: "sorts array with floating point numbers", + input: [3.14, 1.41, 2.71, 0.58], + expected: [0.58, 1.41, 2.71, 3.14], + }, + ])("$title", ({ input, expected }) => { + expect(bubbleSortSimple(input)).toEqual(expected); + }); + + // Special cases that need separate handling + it("maintains original array reference", () => { + const arr = [3, 1, 4, 1, 5]; + const sorted = bubbleSortSimple(arr); + expect(sorted).toBe(arr); + }); + + it("sorts a portion of array using from and to parameters", () => { + const arr = [5, 4, 3, 2, 1]; + expect(bubbleSortSimple(arr, 1, 3)).toEqual([5, 2, 3, 4, 1]); + }); +}); diff --git a/src/algorithms/sort/bubble-sort/bubble-sort-simple.ts b/src/algorithms/sort/bubble-sort/bubble-sort-simple.ts new file mode 100644 index 0000000..cda2d58 --- /dev/null +++ b/src/algorithms/sort/bubble-sort/bubble-sort-simple.ts @@ -0,0 +1,20 @@ +/** + * Sorts the given array in-place using the bubble sort algorithm. + * + * Resource: Data Structures and Algorithms in JavaScript by Federico Kereki + * + * @param arr - The array to be sorted. + * @param from - The starting index of the array to be sorted (default is 0). + * @param to - The ending index of the array to be sorted (default is the last index). + * @returns The sorted array. + */ +export function bubbleSortSimple(arr: number[], from = 0, to = arr.length - 1) { + for (let j = to; j > from; j--) { + for (let i = from; i < j; i++) { + if (arr[i] > arr[i + 1]) { + [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; + } + } + } + return arr; +} diff --git a/src/algorithms/sort/bubble-sort/bubble-sort.test.ts b/src/algorithms/sort/bubble-sort/bubble-sort.test.ts new file mode 100644 index 0000000..a9bf628 --- /dev/null +++ b/src/algorithms/sort/bubble-sort/bubble-sort.test.ts @@ -0,0 +1,18 @@ +import { bubbleSort } from "./bubble-sort"; + +describe("bubbleSort", () => { + it("sorts array of strings alphabetically", () => { + const input = ["zebra", "apple", "banana", "cat"]; + const expected = ["apple", "banana", "cat", "zebra"]; + expect(bubbleSort(input, 0, input.length - 1)).toEqual(expected); + }); + + it("sorts array of objects by custom comparator", () => { + type Person = { age: number }; + const input: Person[] = [{ age: 30 }, { age: 25 }, { age: 35 }]; + const expected: Person[] = [{ age: 25 }, { age: 30 }, { age: 35 }]; + const compareFn = (a: Person, b: Person) => a.age - b.age; + + expect(bubbleSort(input, 0, input.length - 1, compareFn)).toEqual(expected); + }); +}); diff --git a/src/algorithms/sort/bubble-sort/bubble-sort.ts b/src/algorithms/sort/bubble-sort/bubble-sort.ts new file mode 100644 index 0000000..c022f59 --- /dev/null +++ b/src/algorithms/sort/bubble-sort/bubble-sort.ts @@ -0,0 +1,26 @@ +import { Comparator, ComparatorFunction } from "@/utils"; + +/** + * Sorts the given array in-place using the bubble sort algorithm. + * + * @param arr - The array to be sorted. + * @param from - The starting index of the array to be sorted (default is 0). + * @param to - The ending index of the array to be sorted (default is the last index). + * @returns The sorted array. + */ +export function bubbleSort( + arr: Element[], + from = 0, + to = arr.length - 1, + compareFn?: ComparatorFunction +) { + const comparator = new Comparator(compareFn); + for (let j = to; j > from; j--) { + for (let i = from; i < j; i++) { + if (comparator.greaterThan(arr[i], arr[i + 1])) { + [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; + } + } + } + return arr; +}