-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-Sorting.js
More file actions
59 lines (47 loc) · 1.45 KB
/
08-Sorting.js
File metadata and controls
59 lines (47 loc) · 1.45 KB
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
let arr = [30,20,10,60,40,50,70];
let n = arr.length;
// ===============================
// Bubble Sort => O(n*n)
// ===============================
for(let i=0; i<n-1; i++){
for(let j=0; j<n-1-i; j++){
if(arr[j] > arr[j+1]){
// Swap elements
[arr[j],arr[j+1]] = [arr[j+1],arr[j]];
}
}
}
console.log(arr.join(",")); // O U T P U T => 10,20,30,40,50,60,70
// ===============================
// Selection Sort => O(n*n)
// ===============================
for (let i = 0; i < n - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// swap only single time in per pass
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
console.log(arr.join(",")); // O U T P U T => 10,20,30,40,50,60,70
// ===============================
// Insertion Sort => O(n*n)
// ===============================
for (let i = 1; i < n; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // shift right
j--;
}
arr[j + 1] = key; // insert at correct position
}
console.log(arr.join(",")); // O U T P U T => 10,20,30,40,50,60,70
// Bubble Sort:
// “Used mainly for educational purposes due to simplicity.”
// Selection Sort:
// “Used when the number of swaps needs to be minimized.”
// Insertion Sort:
// “Efficient for small or nearly sorted datasets.”