Skip to content

Commit e0e3c19

Browse files
committed
Migrate doctest for MergeSort.js
(also remove inline test driver code)
1 parent 9627cfb commit e0e3c19

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

Sorts/MergeSort.js

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,21 @@
1-
/**
2-
* Merge Sort is an algorithm where the main list is divided down into two half
3-
* sized lists, which then have merge sort called on these two smaller lists
4-
* recursively until there is only a sorted list of one.
1+
/*
2+
* MergeSort implementation.
3+
*
4+
* Merge Sort is an algorithm where the main list is divided down into two half sized lists, which then have merge sort
5+
* called on these two smaller lists recursively until there is only a sorted list of one.
56
*
67
* On the way up the recursive calls, the lists will be merged together inserting
78
* the smaller value first, creating a larger sorted list.
89
*/
910

1011
/**
11-
* Sort and merge two given arrays
12-
* @param {Array} list1 - sublist to break down
13-
* @param {Array} list2 - sublist to break down
14-
* @return {Array} merged list
12+
* Sort and merge two given arrays.
13+
*
14+
* @param {Array} list1 Sublist to break down.
15+
* @param {Array} list2 Sublist to break down.
16+
* @return {Array} The merged list.
1517
*/
16-
/*
17-
* Doctests
18-
* > merge([5, 4],[ 1, 2, 3])
19-
* [1, 2, 3, 5, 4]
20-
* > merge([],[1, 2])
21-
* [1, 2]
22-
* > merge([1, 2, 3], [1])
23-
* [1, 1, 2, 3]
24-
* > merge([], [])
25-
* []
26-
*
27-
* > mergeSort([5, 4])
28-
* [4, 5]
29-
* > mergeSort([8, 4, 10, 15, 9])
30-
* [4, 8, 9, 10, 15]
31-
* > mergeSort([1, 2, 3])
32-
* [1, 2, 3]
33-
* > mergeSort([ ])
34-
* [ ]
35-
*/
36-
37-
function merge (list1, list2) {
18+
export function merge (list1, list2) {
3819
const results = []
3920
let i = 0
4021
let j = 0
@@ -51,11 +32,12 @@ function merge (list1, list2) {
5132
}
5233

5334
/**
54-
* Break down the lists into smaller pieces to be merged
55-
* @param {Array} list - list to be sorted
56-
* @return {Array} sorted list
35+
* Break down the lists into smaller pieces to be merged.
36+
*
37+
* @param {Array} list List to be sorted.
38+
* @return {Array} The sorted list.
5739
*/
58-
function mergeSort (list) {
40+
export function mergeSort (list) {
5941
if (list.length < 2) return list
6042

6143
const listHalf = Math.floor(list.length / 2)
@@ -64,9 +46,3 @@ function mergeSort (list) {
6446

6547
return merge(mergeSort(subList1), mergeSort(subList2))
6648
}
67-
68-
// Merge Sort Example
69-
const unsortedArray = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
70-
const sortedArray = mergeSort(unsortedArray)
71-
72-
console.log('Before:', unsortedArray, 'After:', sortedArray)

Sorts/test/MergeSort.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { merge, mergeSort } from '../MergeSort'
2+
3+
describe('merge', () => {
4+
it('should merge arrays correctly', () => {
5+
expect(merge([5, 4], [1, 2, 3])).toEqual([1, 2, 3, 5, 4])
6+
expect(merge([], [1, 2])).toEqual([1, 2])
7+
expect(merge([1, 2, 3], [1])).toEqual([1, 1, 2, 3])
8+
expect(merge([], [])).toEqual([])
9+
})
10+
})
11+
12+
describe('MergeSort', () => {
13+
it('should work for empty arrays', () => {
14+
expect(mergeSort([])).toEqual([])
15+
})
16+
17+
it('should sort arrays correctly', () => {
18+
expect(mergeSort([5, 4])).toEqual([4, 5])
19+
expect(mergeSort([8, 4, 10, 15, 9])).toEqual([4, 8, 9, 10, 15])
20+
expect(mergeSort([1, 2, 3])).toEqual([1, 2, 3])
21+
expect(mergeSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
22+
})
23+
})

0 commit comments

Comments
 (0)