Skip to content

Commit f9746ed

Browse files
authored
Merge pull request #1 from behzadam/feat/add-bubble-sort
Feat/add bubble sort
2 parents 0104e42 + 7376f57 commit f9746ed

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ It includes code and patterns from the "JavaScript Algorithms" repository, trans
3232
### Sort
3333

3434
- [QuickSort](src/algorithms/sort/quick-sort/quick-sort.ts)
35+
- [BubbleSort](src/algorithms/sort/bubble-sort/bubble-sort.ts)
36+
- [BubbleSortSimple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts)
3537

3638
## Utils
3739

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { bubbleSortSimple } from "./bubble-sort-simple";
2+
3+
describe("bubbleSortSimple", () => {
4+
test.each([
5+
{
6+
title: "sorts array with negative numbers",
7+
input: [-5, -2, -8, -1, -9],
8+
expected: [-9, -8, -5, -2, -1],
9+
},
10+
{
11+
title: "sorts array with mixed positive and negative numbers",
12+
input: [3, -1, 0, 4, -5, 2],
13+
expected: [-5, -1, 0, 2, 3, 4],
14+
},
15+
{
16+
title: "handles array with duplicate numbers",
17+
input: [4, 2, 4, 1, 2, 3],
18+
expected: [1, 2, 2, 3, 4, 4],
19+
},
20+
{
21+
title: "handles single element array",
22+
input: [1],
23+
expected: [1],
24+
},
25+
{
26+
title: "handles empty array",
27+
input: [],
28+
expected: [],
29+
},
30+
{
31+
title: "sorts array with floating point numbers",
32+
input: [3.14, 1.41, 2.71, 0.58],
33+
expected: [0.58, 1.41, 2.71, 3.14],
34+
},
35+
])("$title", ({ input, expected }) => {
36+
expect(bubbleSortSimple(input)).toEqual(expected);
37+
});
38+
39+
// Special cases that need separate handling
40+
it("maintains original array reference", () => {
41+
const arr = [3, 1, 4, 1, 5];
42+
const sorted = bubbleSortSimple(arr);
43+
expect(sorted).toBe(arr);
44+
});
45+
46+
it("sorts a portion of array using from and to parameters", () => {
47+
const arr = [5, 4, 3, 2, 1];
48+
expect(bubbleSortSimple(arr, 1, 3)).toEqual([5, 2, 3, 4, 1]);
49+
});
50+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Sorts the given array in-place using the bubble sort algorithm.
3+
*
4+
* Resource: Data Structures and Algorithms in JavaScript by Federico Kereki
5+
*
6+
* @param arr - The array to be sorted.
7+
* @param from - The starting index of the array to be sorted (default is 0).
8+
* @param to - The ending index of the array to be sorted (default is the last index).
9+
* @returns The sorted array.
10+
*/
11+
export function bubbleSortSimple(arr: number[], from = 0, to = arr.length - 1) {
12+
for (let j = to; j > from; j--) {
13+
for (let i = from; i < j; i++) {
14+
if (arr[i] > arr[i + 1]) {
15+
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
16+
}
17+
}
18+
}
19+
return arr;
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { bubbleSort } from "./bubble-sort";
2+
3+
describe("bubbleSort", () => {
4+
it("sorts array of strings alphabetically", () => {
5+
const input = ["zebra", "apple", "banana", "cat"];
6+
const expected = ["apple", "banana", "cat", "zebra"];
7+
expect(bubbleSort(input, 0, input.length - 1)).toEqual(expected);
8+
});
9+
10+
it("sorts array of objects by custom comparator", () => {
11+
type Person = { age: number };
12+
const input: Person[] = [{ age: 30 }, { age: 25 }, { age: 35 }];
13+
const expected: Person[] = [{ age: 25 }, { age: 30 }, { age: 35 }];
14+
const compareFn = (a: Person, b: Person) => a.age - b.age;
15+
16+
expect(bubbleSort(input, 0, input.length - 1, compareFn)).toEqual(expected);
17+
});
18+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Comparator, ComparatorFunction } from "@/utils";
2+
3+
/**
4+
* Sorts the given array in-place using the bubble sort algorithm.
5+
*
6+
* @param arr - The array to be sorted.
7+
* @param from - The starting index of the array to be sorted (default is 0).
8+
* @param to - The ending index of the array to be sorted (default is the last index).
9+
* @returns The sorted array.
10+
*/
11+
export function bubbleSort<Element>(
12+
arr: Element[],
13+
from = 0,
14+
to = arr.length - 1,
15+
compareFn?: ComparatorFunction<Element>
16+
) {
17+
const comparator = new Comparator(compareFn);
18+
for (let j = to; j > from; j--) {
19+
for (let i = from; i < j; i++) {
20+
if (comparator.greaterThan(arr[i], arr[i + 1])) {
21+
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
22+
}
23+
}
24+
}
25+
return arr;
26+
}

0 commit comments

Comments
 (0)