Skip to content

Commit 942f9fb

Browse files
committed
Migrate doctest for QuickSort.js
(also remove inline test driver code)
1 parent e0e3c19 commit 942f9fb

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

Sorts/QuickSort.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
/*
1+
/**
22
* Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
3-
* For more information see here: https://en.wikipedia.org/wiki/Quicksort
4-
*/
5-
6-
/*
7-
* Doctests
83
*
9-
* > quickSort([5, 4, 3, 10, 2, 1])
10-
* [1, 2, 3, 4, 5, 10]
11-
* > quickSort([])
12-
* []
13-
* > quickSort([5, 4])
14-
* [4, 5]
15-
* > quickSort([1, 2, 3])
16-
* [1, 2, 3]
4+
* For more information see here: https://en.wikipedia.org/wiki/Quicksort
175
*/
18-
19-
function quickSort (items) {
6+
export function quickSort (items) {
207
const length = items.length
218

229
if (length <= 1) {
@@ -40,12 +27,3 @@ function quickSort (items) {
4027

4128
return sorted
4229
}
43-
44-
// Implementation of quick sort
45-
46-
let ar = [0, 5, 3, 2, 2]
47-
// Array before Sort
48-
console.log(ar)
49-
ar = quickSort(ar)
50-
// Array after sort
51-
console.log(ar)

Sorts/test/QuickSort.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { quickSort } from '../QuickSort'
2+
3+
describe('QuickSort', () => {
4+
it('should work for empty arrays', () => {
5+
expect(quickSort([])).toEqual([])
6+
})
7+
8+
it('should sort arrays correctly', () => {
9+
expect(quickSort([5, 4, 3, 10, 2, 1])).toEqual([1, 2, 3, 4, 5, 10])
10+
expect(quickSort([5, 4])).toEqual([4, 5])
11+
expect(quickSort([1, 2, 3])).toEqual([1, 2, 3])
12+
expect(quickSort([0, 5, 3, 2, 2])).toEqual([0, 2, 2, 3, 5])
13+
})
14+
})

0 commit comments

Comments
 (0)