-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick-sort.test.ts
141 lines (109 loc) · 4.59 KB
/
quick-sort.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { quickSort } from '../src/core/quick-sort';
describe('quickSort asc', () => {
it('should sort an array of numbers in ascending order', () => {
const inputArray = [12, 11, 13, 5, 6, 7];
const expectedOutput = [5, 6, 7, 11, 12, 13];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an empty array', () => {
const inputArray: number[] = [];
quickSort(inputArray);
expect(inputArray).toEqual([]);
});
it('should handle an array with one element', () => {
const inputArray = [42];
quickSort(inputArray);
expect(inputArray).toEqual([42]);
});
it('should handle an array with duplicate elements', () => {
const inputArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const expectedOutput = [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an array with negative numbers', () => {
const inputArray = [-5, -2, -8, -1, -3];
const expectedOutput = [-8, -5, -3, -2, -1];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should sort an array of numbers in ascending order', () => {
const inputArray = [12, 11, 13, 5, 6, 7];
const expectedOutput = [5, 6, 7, 11, 12, 13];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an already sorted array in descending order', () => {
const inputArray = [9, 7, 5, 4, 3, 1];
const expectedOutput = [1, 3, 4, 5, 7, 9];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an array with all elements equal', () => {
const inputArray = [8, 8, 8, 8, 8];
const expectedOutput = [8, 8, 8, 8, 8];
quickSort(inputArray);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle a large array with random values', () => {
const inputArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 1000));
const sortedArray = [...inputArray].sort((a, b) => a - b);
quickSort(inputArray);
expect(inputArray).toEqual(sortedArray);
});
});
describe('quickSort desc', () => {
it('should sort an array of numbers in descending order', () => {
const inputArray = [12, 11, 13, 5, 6, 7];
const expectedOutput = [13, 12, 11, 7, 6, 5];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an empty array', () => {
const inputArray: number[] = [];
quickSort(inputArray, false);
expect(inputArray).toEqual([]);
});
it('should handle an array with one element', () => {
const inputArray = [42];
quickSort(inputArray, false);
expect(inputArray).toEqual([42]);
});
it('should handle an array with duplicate elements', () => {
const inputArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const expectedOutput = [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an array with negative numbers', () => {
const inputArray = [-5, -2, -8, -1, -3];
const expectedOutput = [-1, -2, -3, -5, -8];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should sort an array of numbers in descending order', () => {
const inputArray = [12, 11, 13, 5, 6, 7];
const expectedOutput = [13, 12, 11, 7, 6, 5];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an already sorted array in descending order', () => {
const inputArray = [9, 7, 5, 4, 3, 1];
const expectedOutput = [9, 7, 5, 4, 3, 1];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle an array with all elements equal', () => {
const inputArray = [8, 8, 8, 8, 8];
const expectedOutput = [8, 8, 8, 8, 8];
quickSort(inputArray, false);
expect(inputArray).toEqual(expectedOutput);
});
it('should handle a large array with random values', () => {
const inputArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 1000));
const sortedArray = [...inputArray].sort((a, b) => b - a);
quickSort(inputArray, false);
expect(inputArray).toEqual(sortedArray);
});
});