Skip to content

Commit e22297d

Browse files
Merge pull request #587 from NisargChokshi45/main
Adds sorting algorithms in JavaScript and TypeScript
2 parents 0a73519 + 360ce41 commit e22297d

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Bubble Sort
2+
3+
Description: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
4+
5+
Time Complexity:
6+
- Best Case: O(n) - Sorted array
7+
- Average and Worst Case: O(n²)
8+
9+
Space Complexity: O(1)
10+
*/
11+
12+
function bubbleSort(arr) {
13+
const n = arr.length;
14+
15+
for (let i = 0; i < n - 1; i++) {
16+
let swapped = false;
17+
18+
for (let j = 0; j < n - i - 1; j++) {
19+
if (arr[j] > arr[j + 1]) {
20+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
21+
swapped = true;
22+
}
23+
}
24+
25+
if (!swapped) break;
26+
}
27+
28+
return arr;
29+
}
30+
31+
function runTests() {
32+
console.log("---------- Bubble Sort Test Cases ----------");
33+
34+
// Test 1: Regular unsorted array
35+
const test1 = [64, 34, 25, 12, 22, 11, 90];
36+
console.log("\nTest 1 - Original:", test1);
37+
console.log("Test 1 - Sorted:", bubbleSort(test1));
38+
39+
// Test 2: Already sorted array
40+
const test2 = [1, 2, 3, 4, 5];
41+
console.log("\nTest 2 - Original:", test2);
42+
console.log("Test 2 - Sorted:", bubbleSort(test2));
43+
44+
// Test 3: Reverse sorted array
45+
const test3 = [5, 4, 3, 2, 1];
46+
console.log("\nTest 3 - Original:", test3);
47+
console.log("Test 3 - Sorted:", bubbleSort(test3));
48+
49+
// Test 4: Array with duplicates
50+
const test4 = [3, 1, 4, 1, 5, 9, 2, 6, 5];
51+
console.log("\nTest 4 - Original:", test4);
52+
console.log("Test 4 - Sorted:", bubbleSort(test4));
53+
54+
// Test 5: Single element array
55+
const test5 = [42];
56+
console.log("\nTest 5 - Original:", test5);
57+
console.log("Test 5 - Sorted:", bubbleSort(test5));
58+
59+
// Test 6: Empty array
60+
const test6 = [];
61+
console.log("\nTest 6 - Original:", test6);
62+
console.log("Test 6 - Sorted:", bubbleSort(test6));
63+
}
64+
65+
runTests();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Merge Sort
2+
3+
Description: Merge sort is a divide-and-conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves.
4+
5+
Time Complexity: O(n log n)
6+
7+
Space Complexity: O(n)
8+
*/
9+
10+
function mergeSort(arr) {
11+
if (arr.length <= 1) {
12+
return arr;
13+
}
14+
15+
const middle = Math.floor(arr.length / 2);
16+
const leftList = arr.slice(0, middle);
17+
const rightList = arr.slice(middle);
18+
19+
return merge(mergeSort(leftList), mergeSort(rightList));
20+
}
21+
22+
function merge(leftList, rightList) {
23+
let result = [];
24+
let left = 0;
25+
let right = 0;
26+
27+
while (left < leftList.length && right < rightList.length) {
28+
if (leftList[left] < rightList[right]) {
29+
result.push(leftList[left]);
30+
left++;
31+
} else {
32+
result.push(rightList[right]);
33+
right++;
34+
}
35+
}
36+
37+
return result.concat(leftList.slice(left)).concat(rightList.slice(right));
38+
}
39+
40+
function runTests() {
41+
console.log("---------- Merge Sort Test Cases ----------");
42+
43+
// Test 1: Regular unsorted array
44+
const test1 = [64, 34, 25, 12, 22, 11, 90];
45+
console.log("\nTest 1 - Original:", test1);
46+
console.log("Test 1 - Sorted:", mergeSort(test1));
47+
48+
// Test 2: Already sorted array
49+
const test2 = [1, 2, 3, 4, 5];
50+
console.log("\nTest 2 - Original:", test2);
51+
console.log("Test 2 - Sorted:", mergeSort(test2));
52+
53+
// Test 3: Reverse sorted array
54+
const test3 = [5, 4, 3, 2, 1];
55+
console.log("\nTest 3 - Original:", test3);
56+
console.log("Test 3 - Sorted:", mergeSort(test3));
57+
58+
// Test 4: Array with duplicates
59+
const test4 = [3, 1, 4, 1, 5, 9, 2, 6, 5];
60+
console.log("\nTest 4 - Original:", test4);
61+
console.log("Test 4 - Sorted:", mergeSort(test4));
62+
63+
// Test 5: Single element array
64+
const test5 = [42];
65+
console.log("\nTest 5 - Original:", test5);
66+
console.log("Test 5 - Sorted:", mergeSort(test5));
67+
68+
// Test 6: Empty array
69+
const test6 = [];
70+
console.log("\nTest 6 - Original:", test6);
71+
console.log("Test 6 - Sorted:", mergeSort(test6));
72+
73+
// Test 7: Large array
74+
const test7 = [38, 27, 43, 3, 9, 82, 10];
75+
console.log("\nTest 7 - Original:", test7);
76+
console.log("Test 7 - Sorted:", mergeSort(test7));
77+
}
78+
79+
runTests();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Bubble Sort
2+
3+
Description: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
4+
5+
Time Complexity:
6+
- Best Case: O(n) - Sorted array
7+
- Average and Worst Case: O(n²)
8+
9+
Space Complexity: O(1)
10+
*/
11+
12+
function bubbleSort(arr: number[]): number[] {
13+
const n: number = arr.length;
14+
15+
for (let i = 0; i < n - 1; i++) {
16+
let swapped: boolean = false;
17+
18+
for (let j = 0; j < n - i - 1; j++) {
19+
if (arr[j] > arr[j + 1]) {
20+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
21+
swapped = true;
22+
}
23+
}
24+
25+
if (!swapped) break;
26+
}
27+
28+
return arr;
29+
}
30+
31+
function runTests(): void {
32+
console.log("---------- Bubble Sort Test Cases ----------");
33+
34+
// Test 1: Regular unsorted array
35+
const test1: number[] = [64, 34, 25, 12, 22, 11, 90];
36+
console.log("\nTest 1 - Original:", test1);
37+
console.log("Test 1 - Sorted:", bubbleSort(test1));
38+
39+
// Test 2: Already sorted array
40+
const test2: number[] = [1, 2, 3, 4, 5];
41+
console.log("\nTest 2 - Original:", test2);
42+
console.log("Test 2 - Sorted:", bubbleSort(test2));
43+
44+
// Test 3: Reverse sorted array
45+
const test3: number[] = [5, 4, 3, 2, 1];
46+
console.log("\nTest 3 - Original:", test3);
47+
console.log("Test 3 - Sorted:", bubbleSort(test3));
48+
49+
// Test 4: Array with duplicates
50+
const test4: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5];
51+
console.log("\nTest 4 - Original:", test4);
52+
console.log("Test 4 - Sorted:", bubbleSort(test4));
53+
54+
// Test 5: Single element array
55+
const test5: number[] = [42];
56+
console.log("\nTest 5 - Original:", test5);
57+
console.log("Test 5 - Sorted:", bubbleSort(test5));
58+
59+
// Test 6: Empty array
60+
const test6: number[] = [];
61+
console.log("\nTest 6 - Original:", test6);
62+
console.log("Test 6 - Sorted:", bubbleSort(test6));
63+
}
64+
65+
runTests();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Merge Sort
2+
3+
Description: Merge sort is a divide-and-conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves.
4+
5+
Time Complexity: O(n log n)
6+
7+
Space Complexity: O(n)
8+
*/
9+
10+
function mergeSort(arr: number[]): number[] {
11+
if (arr.length <= 1) {
12+
return arr;
13+
}
14+
15+
const middle: number = Math.floor(arr.length / 2);
16+
const leftList: number[] = arr.slice(0, middle);
17+
const rightList: number[] = arr.slice(middle);
18+
19+
return merge(mergeSort(leftList), mergeSort(rightList));
20+
}
21+
22+
function merge(leftList: number[], rightList: number[]): number[] {
23+
let result: number[] = [];
24+
let left: number = 0;
25+
let right: number = 0;
26+
27+
while (left < leftList.length && right < rightList.length) {
28+
if (leftList[left] < rightList[right]) {
29+
result.push(leftList[left]);
30+
left++;
31+
} else {
32+
result.push(rightList[right]);
33+
right++;
34+
}
35+
}
36+
37+
return result.concat(leftList.slice(left)).concat(rightList.slice(right));
38+
}
39+
40+
function runTests(): void {
41+
console.log("---------- Merge Sort Test Cases ----------");
42+
43+
// Test 1: Regular unsorted array
44+
const test1: number[] = [64, 34, 25, 12, 22, 11, 90];
45+
console.log("\nTest 1 - Original:", test1);
46+
console.log("Test 1 - Sorted:", mergeSort(test1));
47+
48+
// Test 2: Already sorted array
49+
const test2: number[] = [1, 2, 3, 4, 5];
50+
console.log("\nTest 2 - Original:", test2);
51+
console.log("Test 2 - Sorted:", mergeSort(test2));
52+
53+
// Test 3: Reverse sorted array
54+
const test3: number[] = [5, 4, 3, 2, 1];
55+
console.log("\nTest 3 - Original:", test3);
56+
console.log("Test 3 - Sorted:", mergeSort(test3));
57+
58+
// Test 4: Array with duplicates
59+
const test4: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5];
60+
console.log("\nTest 4 - Original:", test4);
61+
console.log("Test 4 - Sorted:", mergeSort(test4));
62+
63+
// Test 5: Single element array
64+
const test5: number[] = [42];
65+
console.log("\nTest 5 - Original:", test5);
66+
console.log("Test 5 - Sorted:", mergeSort(test5));
67+
68+
// Test 6: Empty array
69+
const test6: number[] = [];
70+
console.log("\nTest 6 - Original:", test6);
71+
console.log("Test 6 - Sorted:", mergeSort(test6));
72+
73+
// Test 7: Large array
74+
const test7: number[] = [38, 27, 43, 3, 9, 82, 10];
75+
console.log("\nTest 7 - Original:", test7);
76+
console.log("Test 7 - Sorted:", mergeSort(test7));
77+
}
78+
79+
runTests();

0 commit comments

Comments
 (0)