Skip to content

Commit 127dab9

Browse files
Merge pull request #588 from NisargChokshi45/feat/quicksort
Adds Quick sort implementation in JavaScript and TypeScript
2 parents e22297d + b343e20 commit 127dab9

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Quick Sort
2+
3+
Description: Quick sort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
4+
5+
Time Complexity:
6+
- Best/Average Case: O(n log n)
7+
- Worst Case: O(n²)
8+
9+
Space Complexity: O(log n)
10+
*/
11+
12+
function quickSort(arr) {
13+
if (arr.length <= 1) {
14+
return arr;
15+
}
16+
17+
const pivot = arr[Math.floor(arr.length / 2)];
18+
const left = [];
19+
const right = [];
20+
const equal = [];
21+
22+
for (let element of arr) {
23+
if (element < pivot) {
24+
left.push(element);
25+
} else if (element > pivot) {
26+
right.push(element);
27+
} else {
28+
equal.push(element);
29+
}
30+
}
31+
32+
return [...quickSort(left), ...equal, ...quickSort(right)];
33+
}
34+
35+
function runTests() {
36+
console.log("---------- Quick Sort Test Cases ----------");
37+
38+
// Test 1: Regular unsorted array
39+
const test1 = [64, 34, 25, 12, 22, 11, 90];
40+
console.log("\nTest 1 - Original:", test1);
41+
console.log("Test 1 - Sorted:", quickSort(test1));
42+
43+
// Test 2: Already sorted array
44+
const test2 = [1, 2, 3, 4, 5];
45+
console.log("\nTest 2 - Original:", test2);
46+
console.log("Test 2 - Sorted:", quickSort(test2));
47+
48+
// Test 3: Reverse sorted array
49+
const test3 = [5, 4, 3, 2, 1];
50+
console.log("\nTest 3 - Original:", test3);
51+
console.log("Test 3 - Sorted:", quickSort(test3));
52+
53+
// Test 4: Array with duplicates
54+
const test4 = [3, 1, 4, 1, 5, 9, 2, 6, 5];
55+
console.log("\nTest 4 - Original:", test4);
56+
console.log("Test 4 - Sorted:", quickSort(test4));
57+
58+
// Test 5: Single element array
59+
const test5 = [42];
60+
console.log("\nTest 5 - Original:", test5);
61+
console.log("Test 5 - Sorted:", quickSort(test5));
62+
63+
// Test 6: Empty array
64+
const test6 = [];
65+
console.log("\nTest 6 - Original:", test6);
66+
console.log("Test 6 - Sorted:", quickSort(test6));
67+
68+
// Test 7: Large array
69+
const test7 = [38, 27, 43, 3, 9, 82, 10];
70+
console.log("\nTest 7 - Original:", test7);
71+
console.log("Test 7 - Sorted:", quickSort(test7));
72+
73+
// Test 8: Array with negative numbers
74+
const test8 = [-5, 2, -3, 8, -1, 0];
75+
console.log("\nTest 8 - Original:", test8);
76+
console.log("Test 8 - Sorted:", quickSort(test8));
77+
}
78+
79+
runTests();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Quick Sort
2+
3+
Description: Quick sort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
4+
5+
Time Complexity:
6+
- Best/Average Case: O(n log n)
7+
- Worst Case: O(n²)
8+
9+
Space Complexity: O(log n)
10+
*/
11+
12+
function quickSort(arr: number[]): number[] {
13+
if (arr.length <= 1) {
14+
return arr;
15+
}
16+
17+
const pivot: number = arr[Math.floor(arr.length / 2)];
18+
const left: number[] = [];
19+
const right: number[] = [];
20+
const equal: number[] = [];
21+
22+
for (let element of arr) {
23+
if (element < pivot) {
24+
left.push(element);
25+
} else if (element > pivot) {
26+
right.push(element);
27+
} else {
28+
equal.push(element);
29+
}
30+
}
31+
32+
return [...quickSort(left), ...equal, ...quickSort(right)];
33+
}
34+
35+
function runTests(): void {
36+
console.log("---------- Quick Sort Test Cases ----------");
37+
38+
// Test 1: Regular unsorted array
39+
const test1: number[] = [64, 34, 25, 12, 22, 11, 90];
40+
console.log("\nTest 1 - Original:", test1);
41+
console.log("Test 1 - Sorted:", quickSort(test1));
42+
43+
// Test 2: Already sorted array
44+
const test2: number[] = [1, 2, 3, 4, 5];
45+
console.log("\nTest 2 - Original:", test2);
46+
console.log("Test 2 - Sorted:", quickSort(test2));
47+
48+
// Test 3: Reverse sorted array
49+
const test3: number[] = [5, 4, 3, 2, 1];
50+
console.log("\nTest 3 - Original:", test3);
51+
console.log("Test 3 - Sorted:", quickSort(test3));
52+
53+
// Test 4: Array with duplicates
54+
const test4: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5];
55+
console.log("\nTest 4 - Original:", test4);
56+
console.log("Test 4 - Sorted:", quickSort(test4));
57+
58+
// Test 5: Single element array
59+
const test5: number[] = [42];
60+
console.log("\nTest 5 - Original:", test5);
61+
console.log("Test 5 - Sorted:", quickSort(test5));
62+
63+
// Test 6: Empty array
64+
const test6: number[] = [];
65+
console.log("\nTest 6 - Original:", test6);
66+
console.log("Test 6 - Sorted:", quickSort(test6));
67+
68+
// Test 7: Large array
69+
const test7: number[] = [38, 27, 43, 3, 9, 82, 10];
70+
console.log("\nTest 7 - Original:", test7);
71+
console.log("Test 7 - Sorted:", quickSort(test7));
72+
73+
// Test 8: Array with negative numbers
74+
const test8: number[] = [-5, 2, -3, 8, -1, 0];
75+
console.log("\nTest 8 - Original:", test8);
76+
console.log("Test 8 - Sorted:", quickSort(test8));
77+
}
78+
79+
runTests();

0 commit comments

Comments
 (0)