Skip to content

Commit f7266f5

Browse files
author
Isaac Ramirez
committed
- refactor SelectionSort to extend GenericSort
- refactor InsertionSort to extend GenericSort - removed duplicated sorting tests
1 parent 7492bb0 commit f7266f5

File tree

4 files changed

+24
-302
lines changed

4 files changed

+24
-302
lines changed

src/algorithms/insertion-sort/insertion-sort.js

+4-60
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,13 @@
1-
const { StdOut } = require('../../libs')
2-
const { defaultComparator } = require('../../common')
1+
const { GenericSort } = require('../../abstracts')
32

43
/**
54
* Insertion
5+
* @augments GenericSort
66
* @classdesc Insertion Sort Algorithm.
77
* @see p. 245, 251
88
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Insertion.java.html}
99
*/
10-
class Insertion {
11-
/**
12-
* Returns whether `a` is less than `b`
13-
* based on a comparing function.
14-
* @param {*} a Comparable object `a`
15-
* @param {*} b Comparable object `b`
16-
* @param {function} comparator A comparing function that
17-
* returns -1 when `a` is less than `b`,
18-
* returns 1 when `a` is greater than `b` or
19-
* returns 0 when `a` is equal to `b`.
20-
*/
21-
static less (a, b, comparator = defaultComparator) {
22-
return comparator(a, b) < 0
23-
}
24-
25-
/**
26-
* Interchanges the values located in indexes `i` and `j`
27-
* for the given `array`.
28-
* @param {[*]} array Array of values.
29-
* @param {number} i Index 1
30-
* @param {number} j Index 2
31-
*/
32-
static exchange (array, i, j) {
33-
const temp = array[i]
34-
35-
array[i] = array[j]
36-
array[j] = temp
37-
}
38-
39-
/**
40-
* Returns `true` if the provided array is sorted,
41-
* `false` otherwise.
42-
* @param {[*]} array Array of values
43-
* @param {function} comparator A comparing function that
44-
* returns -1 when `a` is less than `b`,
45-
* returns 1 when `a` is greater than `b` or
46-
* returns 0 when `a` is equal to `b`.
47-
*/
48-
static isSorted (array, comparator = defaultComparator) {
49-
for (let i = 1; i < array.length; i++) {
50-
if (this.less(array[i], array[i - 1], comparator)) {
51-
return false
52-
}
53-
}
54-
55-
return true
56-
}
57-
58-
/**
59-
* A function that prints to the StdOut
60-
* the contents of the `array` provided.
61-
* @param {[*]} array Array of values.
62-
*/
63-
static show (array) {
64-
StdOut.println(array.join(' '))
65-
}
66-
10+
class Insertion extends GenericSort {
6711
/**
6812
* Sorts the `array` using the Insertion Sort algorithm.
6913
* @param {[*]} array Array of values.
@@ -72,7 +16,7 @@ class Insertion {
7216
* returns 1 when `a` is greater than `b` or
7317
* returns 0 when `a` is equal to `b`.
7418
*/
75-
static sort (array, comparator = defaultComparator) {
19+
static sort (array, comparator) {
7620
const n = array.length
7721

7822
for (let i = 1; i < n; i++) {

src/algorithms/insertion-sort/insertion-sort.spec.js

+8-91
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const Insertion = require('./insertion-sort')
22
const { newArrayOf } = require('../../utils')
33
const { StdRandom } = require('../../libs')
4-
const { defaultComparator: comparator } = require('../../common')
54

65
describe('Unit Tests: Insertion Sort Algorithm', () => {
76
beforeEach(() => {
@@ -11,97 +10,7 @@ describe('Unit Tests: Insertion Sort Algorithm', () => {
1110
this.allEqualArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
1211
})
1312

14-
describe('static less method', () => {
15-
it('should return true if a is less than b', () => {
16-
const a = 1
17-
const b = 2
18-
19-
expect(Insertion.less(a, b, comparator)).toBe(true)
20-
})
21-
22-
it('should return false if a is greater than b', () => {
23-
const a = 2
24-
const b = 1
25-
26-
expect(Insertion.less(a, b, comparator)).toBe(false)
27-
})
28-
29-
it('should return false if a is equal to b', () => {
30-
const a = 1
31-
const b = 1
32-
33-
expect(Insertion.less(a, b, comparator)).toBe(false)
34-
})
35-
36-
it('should have a defaultComparator function', () => {
37-
expect(Insertion.less(1, 10)).toBe(true)
38-
expect(Insertion.less(1, 1)).toBe(false)
39-
expect(Insertion.less(10, 1)).toBe(false)
40-
})
41-
})
42-
43-
describe('static exchange method', () => {
44-
it('should interchange the values of the two given indexes in the array', () => {
45-
const array = [5, 6, 7]
46-
const expectedArray = [7, 6, 5]
47-
48-
Insertion.exchange(array, 0, 2)
49-
50-
expect(array).toEqual(expectedArray)
51-
})
52-
})
53-
54-
describe('static isSorted method', () => {
55-
it('should return true if an array is sorted', () => {
56-
expect(Insertion.isSorted(this.orderedArray)).toBe(true)
57-
expect(Insertion.isSorted(this.allEqualArray)).toBe(true)
58-
})
59-
60-
it('should return false if the array is not sorted', () => {
61-
expect(Insertion.isSorted(this.unorderedArray)).toBe(false)
62-
expect(Insertion.isSorted(this.reversedArray)).toBe(false)
63-
})
64-
65-
it('should accept a comparator function', () => {
66-
const orderedArray = ['A', 'B', 'C']
67-
const unorderedArray = ['Z', 'X', 'Y']
68-
69-
expect(Insertion.isSorted(orderedArray, comparator)).toBe(true)
70-
expect(Insertion.isSorted(unorderedArray, comparator)).toBe(false)
71-
})
72-
73-
it('should return true for a sorted big array', () => {
74-
const n = 1000000 // a million!
75-
const array = newArrayOf(n, i => i) // from 0 to 999999
76-
77-
expect(Insertion.isSorted(array)).toBeTrue()
78-
})
79-
80-
it('should return false for a random big array', () => {
81-
const n = 1000000 // a million!
82-
const array = newArrayOf(n, i => i) // from 0 to 999999
83-
array[array.length - 1] = 0 // change last number to be 0
84-
85-
expect(Insertion.isSorted(array)).toBeFalse()
86-
})
87-
})
88-
89-
describe('static show method', () => {
90-
it('should be a function', () => {
91-
// I could test the call to StdOut, but I don't want to.
92-
expect(Insertion.show).toBeInstanceOf(Function)
93-
})
94-
})
95-
9613
describe('static sort method', () => {
97-
it('should sort a unordered array', () => {
98-
const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
99-
100-
Insertion.sort(this.unorderedArray)
101-
102-
expect(this.unorderedArray).toEqual(expectedArray)
103-
})
104-
10514
it('should sort an ordered array', () => {
10615
const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10716

@@ -118,6 +27,14 @@ describe('Unit Tests: Insertion Sort Algorithm', () => {
11827
expect(this.reversedArray).toEqual(expectedArray)
11928
})
12029

30+
it('should sort a unordered array', () => {
31+
const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
32+
33+
Insertion.sort(this.unorderedArray)
34+
35+
expect(this.unorderedArray).toEqual(expectedArray)
36+
})
37+
12138
it('should sort an array with all the values to be equal', () => {
12239
const expectedArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
12340

src/algorithms/selection-sort/selection-sort.js

+4-60
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,13 @@
1-
const { StdOut } = require('../../libs')
2-
const { defaultComparator } = require('../../common')
1+
const { GenericSort } = require('../../abstracts')
32

43
/**
54
* Selection
5+
* @augments GenericSort
66
* @classdesc Selection Sort Algorithm.
77
* @see p. 245, 249
88
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Selection.java.html}
99
*/
10-
class Selection {
11-
/**
12-
* Returns whether `a` is less than `b`
13-
* based on a comparing function.
14-
* @param {*} a Comparable object `a`
15-
* @param {*} b Comparable object `b`
16-
* @param {function} comparator A comparing function that
17-
* returns -1 when `a` is less than `b`,
18-
* returns 1 when `a` is greater than `b` or
19-
* returns 0 when `a` is equal to `b`.
20-
*/
21-
static less (a, b, comparator = defaultComparator) {
22-
return comparator(a, b) < 0
23-
}
24-
25-
/**
26-
* Interchanges the values located in indexes `i` and `j`
27-
* for the given `array`.
28-
* @param {[*]} array Array of values.
29-
* @param {number} i Index 1
30-
* @param {number} j Index 2
31-
*/
32-
static exchange (array, i, j) {
33-
const temp = array[i]
34-
35-
array[i] = array[j]
36-
array[j] = temp
37-
}
38-
39-
/**
40-
* Returns `true` if the provided array is sorted,
41-
* `false` otherwise.
42-
* @param {[*]} array Array of values
43-
* @param {function} comparator A comparing function that
44-
* returns -1 when `a` is less than `b`,
45-
* returns 1 when `a` is greater than `b` or
46-
* returns 0 when `a` is equal to `b`.
47-
*/
48-
static isSorted (array, comparator = defaultComparator) {
49-
for (let i = 1; i < array.length; i++) {
50-
if (this.less(array[i], array[i - 1], comparator)) {
51-
return false
52-
}
53-
}
54-
55-
return true
56-
}
57-
58-
/**
59-
* A function that prints to the StdOut
60-
* the contents of the `array` provided.
61-
* @param {[*]} array Array of values.
62-
*/
63-
static show (array) {
64-
StdOut.println(array.join(' '))
65-
}
66-
10+
class Selection extends GenericSort {
6711
/**
6812
* Sorts the `array` using the Selection Sort algorithm.
6913
* @param {[*]} array Array of values.
@@ -72,7 +16,7 @@ class Selection {
7216
* returns 1 when `a` is greater than `b` or
7317
* returns 0 when `a` is equal to `b`.
7418
*/
75-
static sort (array, comparator = defaultComparator) {
19+
static sort (array, comparator) {
7620
const n = array.length
7721

7822
for (let i = 0; i < n; i++) {

0 commit comments

Comments
 (0)