@gabrielrufino/cerebrum 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Factorial = void 0;
4
+ class Factorial {
5
+ constructor(n) {
6
+ this.n = n;
7
+ if (n < 0)
8
+ throw new RangeError('Input should be a non-negative integer');
9
+ }
10
+ execute() {
11
+ if ([0, 1].includes(this.n))
12
+ return 1;
13
+ return this.n * new Factorial(this.n - 1).execute();
14
+ }
15
+ }
16
+ exports.Factorial = Factorial;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Fibonacci = void 0;
4
+ /**
5
+ * @complexity O(2^n)
6
+ */
4
7
  class Fibonacci {
5
8
  constructor(n) {
6
9
  this.n = n;
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BinarySearch = void 0;
4
4
  const Search_1 = require("./Search");
5
+ /**
6
+ * @complexity O(log n)
7
+ */
5
8
  class BinarySearch extends Search_1.Search {
6
9
  execute() {
7
10
  this.beforeExecute();
@@ -2,50 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const BinarySearch_1 = require("./BinarySearch");
5
+ const Search_test_1 = require("./Search.test");
5
6
  (0, vitest_1.describe)('BinarySearch', () => {
6
- (0, vitest_1.it)('should return the index of the target when it exists in the array', () => {
7
- const result = new BinarySearch_1.BinarySearch()
8
- .setElements([1, 2, 3, 4, 5])
9
- .setTarget(3)
10
- .execute();
11
- (0, vitest_1.expect)(result).toBe(2);
12
- });
13
- (0, vitest_1.it)('should return null when the target does not exist in the array', () => {
14
- const result = new BinarySearch_1.BinarySearch()
15
- .setElements([1, 2, 3, 4, 5])
16
- .setTarget(6)
17
- .execute();
18
- (0, vitest_1.expect)(result).toBeNull();
19
- });
20
- (0, vitest_1.it)('should throw an error if elements are not defined', () => {
21
- const search = new BinarySearch_1.BinarySearch()
22
- .setTarget(3);
23
- (0, vitest_1.expect)(() => search.execute()).toThrow('Elements should be defined');
24
- });
25
- (0, vitest_1.it)('should throw an error if target is not defined', () => {
26
- const search = new BinarySearch_1.BinarySearch()
27
- .setElements([1, 2, 3, 4, 5]);
28
- (0, vitest_1.expect)(() => search.execute()).toThrow('Target should be defined');
29
- });
30
- (0, vitest_1.it)('should return the index when searching for 0 in a list that contains 0', () => {
31
- const result = new BinarySearch_1.BinarySearch()
32
- .setElements([0, 1, 2, 3, 4, 5])
33
- .setTarget(0)
34
- .execute();
35
- (0, vitest_1.expect)(result).toBe(0);
36
- });
37
- (0, vitest_1.it)('should return null if target is smaller than all elements', () => {
38
- const result = new BinarySearch_1.BinarySearch()
39
- .setElements([10, 20, 30, 40, 50])
40
- .setTarget(5)
41
- .execute();
42
- (0, vitest_1.expect)(result).toBeNull();
43
- });
44
- (0, vitest_1.it)('should return null if target is larger than all elements', () => {
45
- const result = new BinarySearch_1.BinarySearch()
46
- .setElements([10, 20, 30, 40, 50])
47
- .setTarget(60)
48
- .execute();
49
- (0, vitest_1.expect)(result).toBeNull();
50
- });
7
+ Search_test_1.SearchTestsSuite.execute(BinarySearch_1.BinarySearch);
51
8
  });
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LinearSearch = void 0;
4
4
  const Search_1 = require("./Search");
5
+ /**
6
+ * @complexity O(n)
7
+ */
5
8
  class LinearSearch extends Search_1.Search {
6
9
  execute() {
7
10
  this.beforeExecute();
@@ -2,34 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const LinearSearch_1 = require("./LinearSearch");
5
+ const Search_test_1 = require("./Search.test");
5
6
  (0, vitest_1.describe)('LinearSearch', () => {
6
- (0, vitest_1.it)('should return the index of the target when it exists in the array', () => {
7
- const result = new LinearSearch_1.LinearSearch()
8
- .setElements([1, 2, 3, 4, 5])
9
- .setTarget(3)
10
- .execute();
11
- (0, vitest_1.expect)(result).toBe(2);
12
- });
13
- (0, vitest_1.it)('should return null when the target does not exist in the array', () => {
14
- const result = new LinearSearch_1.LinearSearch()
15
- .setElements([1, 2, 3, 4, 5])
16
- .setTarget(6)
17
- .execute();
18
- (0, vitest_1.expect)(result).toBeNull();
19
- });
20
- (0, vitest_1.it)('should throw an error if elements are not defined', () => {
21
- const search = new LinearSearch_1.LinearSearch()
22
- .setTarget(3);
23
- (0, vitest_1.expect)(() => search.execute()).toThrow('Elements should be defined');
24
- });
25
- (0, vitest_1.it)('should throw an error if target is not defined', () => {
26
- const search = new LinearSearch_1.LinearSearch()
27
- .setElements([1, 2, 3, 4, 5]);
28
- (0, vitest_1.expect)(() => search.execute()).toThrow('Target should be defined');
29
- });
7
+ Search_test_1.SearchTestsSuite.execute(LinearSearch_1.LinearSearch);
30
8
  (0, vitest_1.it)('should return the index when target is 0', () => {
31
9
  const result = new LinearSearch_1.LinearSearch()
32
- .setElements([1, 0, 3, 4, 5]).setTarget(0)
10
+ .setElements([1, 0, 3, 4, 5])
11
+ .setTarget(0)
33
12
  .execute();
34
13
  (0, vitest_1.expect)(result).toBe(1);
35
14
  });
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SearchTestsSuite = void 0;
4
+ const vitest_1 = require("vitest");
5
+ const Search_1 = require("./Search");
6
+ (0, vitest_1.describe)('Search', () => {
7
+ (0, vitest_1.it)('should be defined', () => {
8
+ (0, vitest_1.expect)(Search_1.Search).toBeDefined();
9
+ });
10
+ });
11
+ class SearchTestsSuite {
12
+ static execute(Class) {
13
+ (0, vitest_1.it)('should return the index of the target when it exists in the array', () => {
14
+ const result = new Class()
15
+ .setElements([1, 2, 3, 4, 5])
16
+ .setTarget(3)
17
+ .execute();
18
+ (0, vitest_1.expect)(result).toBe(2);
19
+ });
20
+ (0, vitest_1.it)('should return null when the target does not exist in the array', () => {
21
+ const result = new Class()
22
+ .setElements([1, 2, 3, 4, 5])
23
+ .setTarget(6)
24
+ .execute();
25
+ (0, vitest_1.expect)(result).toBeNull();
26
+ });
27
+ (0, vitest_1.it)('should throw an error if elements are not defined', () => {
28
+ const search = new Class()
29
+ .setTarget(3);
30
+ (0, vitest_1.expect)(() => search.execute()).toThrow('Elements should be defined');
31
+ });
32
+ (0, vitest_1.it)('should throw an error if target is not defined', () => {
33
+ const search = new Class()
34
+ .setElements([1, 2, 3, 4, 5]);
35
+ (0, vitest_1.expect)(() => search.execute()).toThrow('Target should be defined');
36
+ });
37
+ (0, vitest_1.it)('should return the index when searching for 0 in a list that contains 0', () => {
38
+ const result = new Class()
39
+ .setElements([0, 1, 2, 3, 4, 5])
40
+ .setTarget(0)
41
+ .execute();
42
+ (0, vitest_1.expect)(result).toBe(0);
43
+ });
44
+ (0, vitest_1.it)('should return null if target is smaller than all elements', () => {
45
+ const result = new Class()
46
+ .setElements([10, 20, 30, 40, 50])
47
+ .setTarget(5)
48
+ .execute();
49
+ (0, vitest_1.expect)(result).toBeNull();
50
+ });
51
+ (0, vitest_1.it)('should return null if target is larger than all elements', () => {
52
+ const result = new Class()
53
+ .setElements([10, 20, 30, 40, 50])
54
+ .setTarget(60)
55
+ .execute();
56
+ (0, vitest_1.expect)(result).toBeNull();
57
+ });
58
+ }
59
+ }
60
+ exports.SearchTestsSuite = SearchTestsSuite;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BubbleSort = void 0;
4
- class BubbleSort {
5
- constructor(numbers) {
6
- this.numbers = numbers;
7
- }
4
+ const Sort_1 = require("./Sort");
5
+ /**
6
+ * @complexity O(n^2)
7
+ */
8
+ class BubbleSort extends Sort_1.Sort {
8
9
  execute() {
9
10
  for (let i = 0; i < this.numbers.length - 1; i++) {
10
11
  for (let j = 0; j < this.numbers.length - i - 1; j++) {
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QuickSort = void 0;
4
+ const Sort_1 = require("./Sort");
5
+ /**
6
+ * @complexity O(n log n)
7
+ */
8
+ class QuickSort extends Sort_1.Sort {
9
+ execute() {
10
+ if (this.numbers.length <= 1) {
11
+ return [...this.numbers];
12
+ }
13
+ const pivotIndex = Math.floor(this.numbers.length / 2);
14
+ const left = [];
15
+ const right = [];
16
+ for (let i = 0; i < this.numbers.length; i++) {
17
+ if (i === pivotIndex) {
18
+ continue;
19
+ }
20
+ else if (this.numbers[i] <= this.numbers[pivotIndex]) {
21
+ left.push(this.numbers[i]);
22
+ }
23
+ else {
24
+ right.push(this.numbers[i]);
25
+ }
26
+ }
27
+ return [...new QuickSort(left).execute(), this.numbers[pivotIndex], ...new QuickSort(right).execute()];
28
+ }
29
+ }
30
+ exports.QuickSort = QuickSort;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SelectionSort = void 0;
4
- class SelectionSort {
5
- constructor(numbers) {
6
- this.numbers = numbers;
7
- }
4
+ const Sort_1 = require("./Sort");
5
+ /**
6
+ * @complexity O(n^2)
7
+ */
8
+ class SelectionSort extends Sort_1.Sort {
8
9
  execute() {
9
10
  for (let i = 0; i < this.numbers.length - 1; i++) {
10
11
  let minIndex = i;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Sort = void 0;
4
+ class Sort {
5
+ constructor(numbers) {
6
+ this.numbers = numbers;
7
+ }
8
+ }
9
+ exports.Sort = Sort;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gabrielrufino/cerebrum",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Algorithms made in TypeScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -19,7 +19,7 @@
19
19
  "@commitlint/config-conventional": "^19.5.0",
20
20
  "@faker-js/faker": "^8.4.1",
21
21
  "@gabrielrufino/eslint-config": "^1.6.0",
22
- "@vitest/coverage-v8": "^2.1.3",
22
+ "@vitest/coverage-v8": "^2.1.4",
23
23
  "eslint": "^8.57.1",
24
24
  "husky": "^9.1.6",
25
25
  "typescript": "^5.6.3",
@@ -0,0 +1,34 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { Factorial } from './Factorial';
3
+
4
+ describe('Factorial', () => {
5
+ it('should return 1 when n is 0', () => {
6
+ expect(new Factorial(0).execute()).toBe(1);
7
+ });
8
+
9
+ it('should return 1 when n is 1', () => {
10
+ expect(new Factorial(1).execute()).toBe(1);
11
+ });
12
+
13
+ it('should return 2 when n is 2', () => {
14
+ expect(new Factorial(2).execute()).toBe(2);
15
+ });
16
+
17
+ it('should return 6 when n is 3', () => {
18
+ expect(new Factorial(3).execute()).toBe(6);
19
+ });
20
+
21
+ it('should return 24 when n is 4', () => {
22
+ expect(new Factorial(4).execute()).toBe(24);
23
+ });
24
+
25
+ it('should return 120 when n is 5', () => {
26
+ expect(new Factorial(5).execute()).toBe(120);
27
+ });
28
+
29
+ it('should throw an error when n is negative', () => {
30
+ expect(() => new Factorial(-1).execute()).toThrow(
31
+ 'Input should be a non-negative integer'
32
+ );
33
+ });
34
+ });
@@ -0,0 +1,11 @@
1
+ export class Factorial {
2
+ constructor (private readonly n: number) {
3
+ if (n < 0) throw new RangeError('Input should be a non-negative integer');
4
+ }
5
+
6
+ public execute (): number {
7
+ if ([0, 1].includes(this.n)) return 1
8
+
9
+ return this.n * new Factorial(this.n - 1).execute()
10
+ }
11
+ }
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @complexity O(2^n)
3
+ */
1
4
  export class Fibonacci {
2
5
  constructor (private readonly n: number) {
3
6
  if (n < 1) throw new RangeError('Input should be a positive integer');
@@ -1,63 +1,7 @@
1
- import { describe, it, expect } from 'vitest'
1
+ import { describe } from 'vitest'
2
2
  import { BinarySearch } from './BinarySearch'
3
+ import { SearchTestsSuite } from './Search.test'
3
4
 
4
5
  describe('BinarySearch', () => {
5
- it('should return the index of the target when it exists in the array', () => {
6
- const result = new BinarySearch()
7
- .setElements([1, 2, 3, 4, 5])
8
- .setTarget(3)
9
- .execute()
10
-
11
- expect(result).toBe(2)
12
- })
13
-
14
- it('should return null when the target does not exist in the array', () => {
15
- const result = new BinarySearch()
16
- .setElements([1, 2, 3, 4, 5])
17
- .setTarget(6)
18
- .execute()
19
-
20
- expect(result).toBeNull()
21
- })
22
-
23
- it('should throw an error if elements are not defined', () => {
24
- const search = new BinarySearch()
25
- .setTarget(3)
26
-
27
- expect(() => search.execute()).toThrow('Elements should be defined')
28
- })
29
-
30
- it('should throw an error if target is not defined', () => {
31
- const search = new BinarySearch()
32
- .setElements([1, 2, 3, 4, 5])
33
-
34
- expect(() => search.execute()).toThrow('Target should be defined')
35
- })
36
-
37
- it('should return the index when searching for 0 in a list that contains 0', () => {
38
- const result = new BinarySearch()
39
- .setElements([0, 1, 2, 3, 4, 5])
40
- .setTarget(0)
41
- .execute()
42
-
43
- expect(result).toBe(0)
44
- })
45
-
46
- it('should return null if target is smaller than all elements', () => {
47
- const result = new BinarySearch()
48
- .setElements([10, 20, 30, 40, 50])
49
- .setTarget(5)
50
- .execute()
51
-
52
- expect(result).toBeNull()
53
- })
54
-
55
- it('should return null if target is larger than all elements', () => {
56
- const result = new BinarySearch()
57
- .setElements([10, 20, 30, 40, 50])
58
- .setTarget(60)
59
- .execute()
60
-
61
- expect(result).toBeNull()
62
- })
6
+ SearchTestsSuite.execute(BinarySearch)
63
7
  })
@@ -1,5 +1,8 @@
1
1
  import { Search } from './Search';
2
2
 
3
+ /**
4
+ * @complexity O(log n)
5
+ */
3
6
  export class BinarySearch extends Search {
4
7
  public execute(): number | null {
5
8
  this.beforeExecute()
@@ -1,42 +1,14 @@
1
- import { describe, it, expect } from 'vitest'
1
+ import { describe, expect, it } from 'vitest'
2
2
  import { LinearSearch } from './LinearSearch'
3
+ import { SearchTestsSuite } from './Search.test'
3
4
 
4
5
  describe('LinearSearch', () => {
5
- it('should return the index of the target when it exists in the array', () => {
6
- const result = new LinearSearch()
7
- .setElements([1, 2, 3, 4, 5])
8
- .setTarget(3)
9
- .execute()
10
-
11
- expect(result).toBe(2)
12
- })
13
-
14
- it('should return null when the target does not exist in the array', () => {
15
- const result = new LinearSearch()
16
- .setElements([1, 2, 3, 4, 5])
17
- .setTarget(6)
18
- .execute()
19
-
20
- expect(result).toBeNull()
21
- })
22
-
23
- it('should throw an error if elements are not defined', () => {
24
- const search = new LinearSearch()
25
- .setTarget(3)
26
-
27
- expect(() => search.execute()).toThrow('Elements should be defined')
28
- })
29
-
30
- it('should throw an error if target is not defined', () => {
31
- const search = new LinearSearch()
32
- .setElements([1, 2, 3, 4, 5])
33
-
34
- expect(() => search.execute()).toThrow('Target should be defined')
35
- })
6
+ SearchTestsSuite.execute(LinearSearch)
36
7
 
37
8
  it('should return the index when target is 0', () => {
38
9
  const result = new LinearSearch()
39
- .setElements([1, 0, 3, 4, 5]).setTarget(0)
10
+ .setElements([1, 0, 3, 4, 5])
11
+ .setTarget(0)
40
12
  .execute()
41
13
 
42
14
  expect(result).toBe(1)
@@ -1,5 +1,8 @@
1
1
  import { Search } from './Search'
2
2
 
3
+ /**
4
+ * @complexity O(n)
5
+ */
3
6
  export class LinearSearch extends Search {
4
7
  public execute (): number | null {
5
8
  this.beforeExecute()
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { Search } from './Search'
4
+
5
+ describe('Search', () => {
6
+ it('should be defined', () => {
7
+ expect(Search).toBeDefined()
8
+ })
9
+ })
10
+
11
+ export class SearchTestsSuite {
12
+ static execute(Class: { new (elements?: Array<number>, target?: number): Search }) {
13
+ it('should return the index of the target when it exists in the array', () => {
14
+ const result = new Class()
15
+ .setElements([1, 2, 3, 4, 5])
16
+ .setTarget(3)
17
+ .execute()
18
+
19
+ expect(result).toBe(2)
20
+ })
21
+
22
+ it('should return null when the target does not exist in the array', () => {
23
+ const result = new Class()
24
+ .setElements([1, 2, 3, 4, 5])
25
+ .setTarget(6)
26
+ .execute()
27
+
28
+ expect(result).toBeNull()
29
+ })
30
+
31
+ it('should throw an error if elements are not defined', () => {
32
+ const search = new Class()
33
+ .setTarget(3)
34
+
35
+ expect(() => search.execute()).toThrow('Elements should be defined')
36
+ })
37
+
38
+ it('should throw an error if target is not defined', () => {
39
+ const search = new Class()
40
+ .setElements([1, 2, 3, 4, 5])
41
+
42
+ expect(() => search.execute()).toThrow('Target should be defined')
43
+ })
44
+
45
+ it('should return the index when searching for 0 in a list that contains 0', () => {
46
+ const result = new Class()
47
+ .setElements([0, 1, 2, 3, 4, 5])
48
+ .setTarget(0)
49
+ .execute()
50
+
51
+ expect(result).toBe(0)
52
+ })
53
+
54
+ it('should return null if target is smaller than all elements', () => {
55
+ const result = new Class()
56
+ .setElements([10, 20, 30, 40, 50])
57
+ .setTarget(5)
58
+ .execute()
59
+
60
+ expect(result).toBeNull()
61
+ })
62
+
63
+ it('should return null if target is larger than all elements', () => {
64
+ const result = new Class()
65
+ .setElements([10, 20, 30, 40, 50])
66
+ .setTarget(60)
67
+ .execute()
68
+
69
+ expect(result).toBeNull()
70
+ })
71
+ }
72
+ }
@@ -1,40 +1,7 @@
1
- import { describe, it, expect } from 'vitest';
1
+ import { describe } from 'vitest';
2
2
  import { BubbleSort } from './BubbleSort';
3
+ import { SortTestsSuite } from './Sort.spec';
3
4
 
4
5
  describe('BubbleSort', () => {
5
- it('should sort an unsorted array', () => {
6
- const numbers = [64, 34, 25, 12, 22, 11, 90];
7
- const sorted = new BubbleSort(numbers).execute();
8
- expect(sorted).toEqual([11, 12, 22, 25, 34, 64, 90]);
9
- });
10
-
11
- it('should return the same array if already sorted', () => {
12
- const numbers = [1, 2, 3, 4, 5];
13
- const sorted = new BubbleSort(numbers).execute();
14
- expect(sorted).toEqual([1, 2, 3, 4, 5]);
15
- });
16
-
17
- it('should sort an array with duplicate numbers', () => {
18
- const numbers = [5, 1, 4, 2, 1];
19
- const sorted = new BubbleSort(numbers).execute();
20
- expect(sorted).toEqual([1, 1, 2, 4, 5]);
21
- });
22
-
23
- it('should handle an empty array', () => {
24
- const numbers: number[] = [];
25
- const sorted = new BubbleSort(numbers).execute();
26
- expect(sorted).toEqual([]);
27
- });
28
-
29
- it('should handle an array with a single element', () => {
30
- const numbers = [42];
31
- const sorted = new BubbleSort(numbers).execute();
32
- expect(sorted).toEqual([42]);
33
- });
34
-
35
- it('should sort an array with negative numbers', () => {
36
- const numbers = [-3, -1, -4, 2, 0];
37
- const sorted = new BubbleSort(numbers).execute();
38
- expect(sorted).toEqual([-4, -3, -1, 0, 2]);
39
- });
6
+ SortTestsSuite.execute(BubbleSort)
40
7
  });
@@ -1,8 +1,9 @@
1
- export class BubbleSort {
2
- constructor (
3
- private readonly numbers: Array<number>
4
- ) {}
1
+ import { Sort } from './Sort';
5
2
 
3
+ /**
4
+ * @complexity O(n^2)
5
+ */
6
+ export class BubbleSort extends Sort {
6
7
  public execute(): Array<number> {
7
8
  for (let i = 0; i < this.numbers.length - 1; i++) {
8
9
  for (let j = 0; j < this.numbers.length - i - 1; j++) {
@@ -0,0 +1,7 @@
1
+ import { describe } from 'vitest';
2
+ import { QuickSort } from './QuickSort';
3
+ import { SortTestsSuite } from './Sort.spec';
4
+
5
+ describe('QuickSort', () => {
6
+ SortTestsSuite.execute(QuickSort)
7
+ });
@@ -0,0 +1,28 @@
1
+ import { Sort } from './Sort';
2
+
3
+ /**
4
+ * @complexity O(n log n)
5
+ */
6
+ export class QuickSort extends Sort {
7
+ public execute(): Array<number> {
8
+ if (this.numbers.length <= 1) {
9
+ return [...this.numbers]
10
+ }
11
+
12
+ const pivotIndex = Math.floor(this.numbers.length / 2);
13
+ const left: number[] = []
14
+ const right: number[] = []
15
+
16
+ for (let i = 0; i < this.numbers.length; i++) {
17
+ if (i === pivotIndex) {
18
+ continue
19
+ } else if (this.numbers[i] <= this.numbers[pivotIndex]) {
20
+ left.push(this.numbers[i])
21
+ } else {
22
+ right.push(this.numbers[i])
23
+ }
24
+ }
25
+
26
+ return [...new QuickSort(left).execute(), this.numbers[pivotIndex], ...new QuickSort(right).execute()]
27
+ }
28
+ }
@@ -1,36 +1,7 @@
1
- import { describe, it, expect } from 'vitest';
1
+ import { describe } from 'vitest';
2
2
  import { SelectionSort } from './SelectionSort';
3
+ import { SortTestsSuite } from './Sort.spec';
3
4
 
4
5
  describe('SelectionSort', () => {
5
- it('should sort an array of numbers in ascending order', () => {
6
- const unsorted = [64, 25, 12, 22, 11];
7
- const sorted = [11, 12, 22, 25, 64];
8
- expect(new SelectionSort(unsorted).execute()).toEqual(sorted);
9
- });
10
-
11
- it('should handle an empty array', () => {
12
- const unsorted: number[] = [];
13
- expect(new SelectionSort(unsorted).execute()).toEqual([]);
14
- });
15
-
16
- it('should handle an array with one element', () => {
17
- const unsorted = [42];
18
- expect(new SelectionSort(unsorted).execute()).toEqual([42]);
19
- });
20
-
21
- it('should handle an array with already sorted elements', () => {
22
- const unsorted = [1, 2, 3, 4, 5];
23
- expect(new SelectionSort(unsorted).execute()).toEqual([1, 2, 3, 4, 5]);
24
- });
25
-
26
- it('should handle an array with identical elements', () => {
27
- const unsorted = [5, 5, 5, 5, 5];
28
- expect(new SelectionSort(unsorted).execute()).toEqual([5, 5, 5, 5, 5]);
29
- });
30
-
31
- it('should sort an array with negative numbers', () => {
32
- const unsorted = [3, -1, -5, 2, 0];
33
- const sorted = [-5, -1, 0, 2, 3];
34
- expect(new SelectionSort(unsorted).execute()).toEqual(sorted);
35
- });
6
+ SortTestsSuite.execute(SelectionSort)
36
7
  });
@@ -1,8 +1,9 @@
1
- export class SelectionSort {
2
- constructor(
3
- private readonly numbers: Array<number>
4
- ) {}
1
+ import { Sort } from './Sort';
5
2
 
3
+ /**
4
+ * @complexity O(n^2)
5
+ */
6
+ export class SelectionSort extends Sort {
6
7
  public execute(): Array<number> {
7
8
  for (let i = 0; i < this.numbers.length - 1; i++) {
8
9
  let minIndex = i;
@@ -0,0 +1,45 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { Sort } from './Sort'
4
+
5
+ describe('Sort', () => {
6
+ it('should be defined', () => {
7
+ expect(Sort).toBeDefined()
8
+ })
9
+ })
10
+
11
+ export class SortTestsSuite {
12
+ static execute(Class: { new (numbers: Array<number>): Sort }) {
13
+ it('should sort an array of numbers in ascending order', () => {
14
+ const unsorted = [64, 25, 12, 22, 11];
15
+ const sorted = [11, 12, 22, 25, 64];
16
+ expect(new Class(unsorted).execute()).toEqual(sorted);
17
+ });
18
+
19
+ it('should handle an empty array', () => {
20
+ const unsorted: number[] = [];
21
+ expect(new Class(unsorted).execute()).toEqual([]);
22
+ });
23
+
24
+ it('should handle an array with one element', () => {
25
+ const unsorted = [42];
26
+ expect(new Class(unsorted).execute()).toEqual([42]);
27
+ });
28
+
29
+ it('should handle an array with already sorted elements', () => {
30
+ const unsorted = [1, 2, 3, 4, 5];
31
+ expect(new Class(unsorted).execute()).toEqual([1, 2, 3, 4, 5]);
32
+ });
33
+
34
+ it('should handle an array with identical elements', () => {
35
+ const unsorted = [5, 5, 5, 5, 5];
36
+ expect(new Class(unsorted).execute()).toEqual([5, 5, 5, 5, 5]);
37
+ });
38
+
39
+ it('should sort an array with negative numbers', () => {
40
+ const unsorted = [3, -1, -5, 2, 0];
41
+ const sorted = [-5, -1, 0, 2, 3];
42
+ expect(new Class(unsorted).execute()).toEqual(sorted);
43
+ });
44
+ }
45
+ }
@@ -0,0 +1,7 @@
1
+ export abstract class Sort {
2
+ constructor(
3
+ protected readonly numbers: Array<number>
4
+ ) {}
5
+
6
+ public abstract execute(): Array<number>
7
+ }