|
| 1 | +import quickSort from "./quickSort"; |
| 2 | + |
| 3 | +describe("Test for Quick Sort", () => { |
| 4 | + const nTimes = 100; |
| 5 | + const SIZE = 100; |
| 6 | + |
| 7 | + test("testing if QS sort random array using random pivot", () => { |
| 8 | + for (let i = 0; i < nTimes; i++) { |
| 9 | + const arr = Array.from({ length: SIZE }, () => |
| 10 | + Math.floor(Math.random() * 1000) |
| 11 | + ); |
| 12 | + const sortedArr = quickSort(arr); |
| 13 | + const jsSortedArr = arr.slice().sort((a, b) => a - b); |
| 14 | + expect(sortedArr[0]).toEqual(jsSortedArr); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + test("testing if QS sort random array using first element as pivot", () => { |
| 19 | + for (let i = 0; i < nTimes; i++) { |
| 20 | + const arr = Array.from({ length: SIZE }, () => |
| 21 | + Math.floor(Math.random() * 1000) |
| 22 | + ); |
| 23 | + const sortedArr = quickSort(arr, 0, arr.length, 0); |
| 24 | + const jsSortedArr = arr.slice().sort((a, b) => a - b); |
| 25 | + expect(sortedArr[0]).toEqual(jsSortedArr); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + test("testing if QS sort random array using last element as pivot", () => { |
| 30 | + for (let i = 0; i < nTimes; i++) { |
| 31 | + const arr = Array.from({ length: SIZE }, () => |
| 32 | + Math.floor(Math.random() * 1000) |
| 33 | + ); |
| 34 | + const sortedArr = quickSort(arr, 0, arr.length, 1); |
| 35 | + const jsSortedArr = arr.slice().sort((a, b) => a - b); |
| 36 | + expect(sortedArr[0]).toEqual(jsSortedArr); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + test("testing if QS sort random array using 'median of 3' element as pivot", () => { |
| 41 | + for (let i = 0; i < nTimes; i++) { |
| 42 | + const arr = Array.from({ length: SIZE }, () => |
| 43 | + Math.floor(Math.random() * 1000) |
| 44 | + ); |
| 45 | + const sortedArr = quickSort(arr, 0, arr.length, 2); |
| 46 | + const jsSortedArr = arr.slice().sort((a, b) => a - b); |
| 47 | + expect(sortedArr[0]).toEqual(jsSortedArr); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("testing if QS sort random array using 'median of 3 random elements' as pivot", () => { |
| 52 | + for (let i = 0; i < nTimes; i++) { |
| 53 | + const arr = Array.from({ length: SIZE }, () => |
| 54 | + Math.floor(Math.random() * 1000) |
| 55 | + ); |
| 56 | + const sortedArr = quickSort(arr, 0, arr.length, 2); |
| 57 | + const jsSortedArr = arr.slice().sort((a, b) => a - b); |
| 58 | + expect(sortedArr[0]).toEqual(jsSortedArr); |
| 59 | + } |
| 60 | + }); |
| 61 | +}); |
0 commit comments