Skip to content

Commit 7c9219d

Browse files
authored
chore: Merge pull request #738 from charliejmoore/cycle-sort-tests
Cycle sort tests
2 parents 6607f27 + ee54c71 commit 7c9219d

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

Diff for: Sorts/CycleSort.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
* Wikipedia: https://en.wikipedia.org/wiki/Cycle_sort
99
*/
1010

11+
/**
12+
* cycleSort takes an input array of numbers and returns the array sorted in increasing order.
13+
*
14+
* @param {number[]} list An array of numbers to be sorted.
15+
* @return {number[]} An array of numbers sorted in increasing order.
16+
*/
1117
function cycleSort (list) {
12-
let writes = 0
1318
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
1419
let value = list[cycleStart]
1520
let position = cycleStart
@@ -20,19 +25,17 @@ function cycleSort (list) {
2025
position++
2126
}
2227
}
23-
// if its the same continue
28+
// if it is the same, continue
2429
if (position === cycleStart) {
2530
continue
2631
}
27-
2832
while (value === list[position]) {
2933
position++
3034
}
3135

3236
const oldValue = list[position]
3337
list[position] = value
3438
value = oldValue
35-
writes++
3639

3740
// rotate the rest
3841
while (position !== cycleStart) {
@@ -48,20 +51,9 @@ function cycleSort (list) {
4851
const oldValueCycle = list[position]
4952
list[position] = value
5053
value = oldValueCycle
51-
writes++
5254
}
5355
}
54-
return writes
56+
return list
5557
}
5658

57-
/**
58-
* Implementation of Cycle Sort
59-
*/
60-
const array = [5, 6, 7, 8, 1, 2, 12, 14]
61-
// Before Sort
62-
console.log('\n- Before Sort | Implementation of Cycle Sort -')
63-
console.log(array)
64-
// After Sort
65-
console.log('- After Sort | Implementation of Cycle Sort -')
66-
console.log(cycleSort(array))
67-
console.log('\n')
59+
export { cycleSort }

Diff for: Sorts/test/CycleSort.test.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { cycleSort } from '../CycleSort'
2+
3+
describe('cycleSort function', () => {
4+
it('should correctly sort an input list that is sorted backwards', () => {
5+
const array = [5, 4, 3, 2, 1]
6+
expect(cycleSort(array)).toEqual([1, 2, 3, 4, 5])
7+
})
8+
9+
it('should correctly sort an input list that is unsorted', () => {
10+
const array = [15, 24, 3, 2224, 1]
11+
expect(cycleSort(array)).toEqual([1, 3, 15, 24, 2224])
12+
})
13+
14+
describe('Variations of input array lengths', () => {
15+
it('should return an empty list with the input list is an empty list', () => {
16+
expect(cycleSort([])).toEqual([])
17+
})
18+
19+
it('should correctly sort an input list of length 1', () => {
20+
expect(cycleSort([100])).toEqual([100])
21+
})
22+
23+
it('should correctly sort an input list of an odd length', () => {
24+
expect(cycleSort([101, -10, 321])).toEqual([-10, 101, 321])
25+
})
26+
27+
it('should correctly sort an input list of an even length', () => {
28+
expect(cycleSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
29+
})
30+
})
31+
32+
describe('Variations of input array elements', () => {
33+
it('should correctly sort an input list that contains only positive numbers', () => {
34+
expect(cycleSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
35+
})
36+
37+
it('should correctly sort an input list that contains only negative numbers', () => {
38+
expect(cycleSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
39+
})
40+
41+
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
42+
expect(cycleSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
43+
})
44+
45+
it('should correctly sort an input list that contains only whole numbers', () => {
46+
expect(cycleSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
47+
})
48+
49+
it('should correctly sort an input list that contains only decimal numbers', () => {
50+
expect(cycleSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
51+
})
52+
53+
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
54+
expect(cycleSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
55+
})
56+
57+
it('should correctly sort an input list that contains only fractional numbers', () => {
58+
expect(cycleSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
59+
})
60+
61+
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
62+
expect(cycleSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
63+
})
64+
65+
it('should correctly sort an input list that contains duplicates', () => {
66+
expect(cycleSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
67+
})
68+
})
69+
})

0 commit comments

Comments
 (0)