-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.js
195 lines (175 loc) · 4.08 KB
/
algorithms.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Algotithms written using pseudocose from FCS lecture
function insertionSort(a) {
for (let j = 1; j < a.length; j++) {
let i = 0;
while (a[j] > a[i]) {
i = i + 1;
}
let m = a[j];
for (let k = 1; k < j - i - 1; k++) {
a[j - k] = a[j - k - 1];
}
a[i] = m;
}
return a;
}
function insertionSort2(inputArr) {
let length = inputArr.length;
for (let i = 1; i < length; i++) {
let key = inputArr[i];
let j = i - 1;
while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
}
inputArr[j + 1] = key;
}
return inputArr;
}
function bubbleSort(List) {
let end = List.length - 1;
while (end > 1) {
let i = 0;
while (!(i > end)) {
if (List[i] > List[i + 1]) {
let a = List[i];
let b = List[i + 1];
List[i] = b;
List[i + 1] = a;
}
i = i + 1;
}
end = end - 1;
}
return List;
}
function binarySearch(List, item) {
let length = List.length;
let pos = 0;
if (length === 0) {
return "Search Failed";
} else {
let mid = Math.floor(length / 2);
if (List[mid] === item) {
pos++;
return item + " exists ";
} else if (List[mid] > item) {
let listLeft = [];
for (let index = 0; index < mid; index++) {
const element = List[index];
listLeft.push(element);
}
return binarySearch(listLeft, item);
} else if (List[mid] < item) {
let listRight = [];
for (let index = mid + 1; index < length; index++) {
const element = List[index];
listRight.push(element);
}
return binarySearch(listRight, item);
}
}
}
function heapSort(arr) {
let n = arr.length;
// Build heap (rearrange array)
for (let i = parseInt(n / 2 - 1); i >= 0; i--) {
minHeapify(arr, n, i);
}
// One by one extract an element from heap
for (let i = n - 1; i >= 0; i--) {
// Move current root to end
let temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
minHeapify(arr, i, 0);
}
return arr;
}
function minHeapify(arr, n, i) {
let smallest = i;
let l = 2 * i + 1; //left child index
let r = 2 * i + 2; //right child index
//If left child is smaller than root
if (l < n && arr[l] < arr[smallest]) {
smallest = l;
}
// If right child is smaller than smallest so far
if (r < n && arr[r] < arr[smallest]) {
smallest = r;
}
// If smallest is not root
if (smallest != i) {
let temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
// Recursively heapify the affected sub-tree
minHeapify(arr, n, smallest);
}
}
function QuickSort(List) {
let pivot;
let pivotItem;
let Listleft = [];
let ListRight = [];
if (List.length <= 1) {
return List;
} else {
pivot = Math.floor((List.length - 1) / 2);
pivotItem = List[pivot];
List.splice(pivot, 1);
for (let index = 0; index < List.length; index++) {
const element = List[index];
if (pivotItem > element) {
Listleft.push(element);
} else {
ListRight.push(element);
}
}
}
let ans = QuickSort(Listleft) + pivotItem + QuickSort(ListRight);
return ans;
}
function merge(a, b) {
let c = [];
while (a.length > 0 && b.length > 0) {
let i = 0;
if (a[i] > b[i]) {
c.push(b[i]);
b.splice(i, 1);
} else {
c.push(a[i]);
a.splice(i, 1);
}
i++;
}
while (a.length > 0) {
c.push(a[0]);
a.splice(0, 1);
}
while (b.length > 0) {
c.push(b[0]);
b.splice(0, 1);
}
return c;
}
function mergeSort(List) {
let length = List.length;
let ListLeft = [];
let ListRight = [];
if (length <= 1) {
return List;
}
for (let index = 0; index <( Math.round(length / 2) - 1); index++) {
const element = List[index];
ListLeft.push(element);
}
for (let index = (Math.round(length / 2) - 1); index < List.length; index++) {
const element = List[index];
ListLeft.push(element);
}
ListLeft = mergeSort(ListLeft);
ListRight = mergeSort(ListRight);
return merge(ListLeft, ListRight);
}