Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions src/algorithms/sort/bubble-sort/bubble-sort-simple.test.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
});
20 changes: 20 additions & 0 deletions src/algorithms/sort/bubble-sort/bubble-sort-simple.ts
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions src/algorithms/sort/bubble-sort/bubble-sort.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
26 changes: 26 additions & 0 deletions src/algorithms/sort/bubble-sort/bubble-sort.ts
Original file line number Diff line number Diff line change
@@ -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<Element>(
arr: Element[],
from = 0,
to = arr.length - 1,
compareFn?: ComparatorFunction<Element>
) {
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;
}