Skip to content

Commit 1894a02

Browse files
committed
Sorting Algos
1 parent 9ee2cc6 commit 1894a02

8 files changed

+116
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Ques 1: Implement Bubble Sort in JavaScript
2+
// Write a function to sort the given array nums in ascending order.
3+
4+
// Input: nums = [29,10,14,37,14] ----->>>>> Output: [10,14,14,29,37]
5+
6+
const bubbleSort = (arr) => {
7+
const n = arr.length;
8+
for (let i = 0; i < n; i++) {
9+
for (j = 0; j < n - i - 1; j++) {
10+
if (arr[j] > arr[j + 1]) {
11+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
12+
}
13+
}
14+
}
15+
16+
return arr;
17+
};
18+
19+
console.log(bubbleSort([29, 10, 14, 37, 14]));
20+
21+
// Best Time Complexity = O(n)
22+
// Worst Time Complexity = O(n^2)
23+
// Average Time Complexity = O(n^2)
24+
25+
// Space Complexity = O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Ques 2: Implement Selection Sort in JavaScript
2+
// Write a function to sort the given array nums in ascending order.
3+
4+
// Input: nums = [29,10,14,37,14] ----->>>>> Output: [10,14,14,29,37]
5+
6+
const selectionSort = (arr) => {
7+
const n = arr.length;
8+
for (let i = 0; i < n - 1; i++) {
9+
// n
10+
let minIndex = i;
11+
for (let j = i + 1; j < n; j++) {
12+
// n
13+
if (arr[j] < arr[minIndex]) {
14+
minIndex = j;
15+
}
16+
}
17+
if (minIndex !== i) {
18+
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
19+
}
20+
}
21+
22+
return arr;
23+
};
24+
25+
console.log(selectionSort([29, 10, 14, 37, 14]));
26+
27+
// Time Complexity = O(n^2)
28+
// Space Complexity = O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Ques 3: Implement Insertion Sort in JavaScript
2+
// Write a function to sort the given array nums in ascending order.
3+
4+
// Input: nums = [29,10,14,37,14,33,8,11] ----->>>>> Output: [8,10,11,14,14,29,33,37]
5+
6+
function insertionSort(arr) {
7+
const n = arr.length;
8+
9+
for (let i = 1; i < n; i++) {
10+
// n
11+
const key = arr[i];
12+
let j = i - 1;
13+
while (j >= 0 && arr[j] > key) {
14+
// n
15+
arr[j + 1] = arr[j];
16+
j--;
17+
}
18+
arr[j + 1] = key;
19+
}
20+
21+
return arr;
22+
}
23+
24+
console.log(insertionSort([29, 10, 14, 37, 14, 33, 8, 11]));
25+
26+
// Best Case Time Complexity = O(n)
27+
// Worst Case Time Complexity = O(n^2)
28+
// Average Case Time Complexity = O(n^2)
29+
// Space Complexity = O(1)
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Sort() Method in Javascript
2+
3+
const fruits = ["dragon fruit", "apple", "date", "cherry", "banana"];
4+
5+
console.log(fruits.sort());
6+
7+
const nums = [10, 5, 18, 1, 27];
8+
9+
const compareFunction = (a, b) => {
10+
// 1. <0 => a comes first
11+
// 2. 0 => nothing changes
12+
// 3. >0 => b comes first
13+
14+
return b - a;
15+
};
16+
17+
console.log(nums.sort(compareFunction));
18+
19+
const people = [
20+
{name: "Piyush", age: 25},
21+
{name: "Suyash", age: 22},
22+
{name: "Raj", age: 30},
23+
{name: "Ishan", age: 27},
24+
];
25+
26+
const compareFunctionForObj = (a, b) => {
27+
// 1. <0 => a comes first
28+
// 2. 0 => nothing changes
29+
// 3. >0 => b comes first
30+
31+
return a.age - b.age;
32+
};
33+
34+
console.log(people.sort(compareFunctionForObj));

0 commit comments

Comments
 (0)