// c is inc/dec constant
for (let i = 0; i < n; i += c) {
// some O(1) expression
}
for (let i = n; i > 0; i -= c) {
// some O(1) expression
}
// c is inc/dec constant
for (let i = 0; i < n; i += c) {
for (let j = 0; j < n; j += c) {
// some O(1) expression
}
}
for (let i = n; i > 0; i -= c) {
for (let j = n; j > 0; j -= c) {
// some O(1) expression
}
}
// c is inc/dec constant
for (let i = 0; i < n; i *= c) {
// some O(1) expression
}
for (let i = n; i > 0; i /= c) {
// some O(1) expression
}
// recursive function
const recursive = (n) => {
if (n === 0) return;
// some O(1) expression
recurseive(n-1);
}
Binary Search(iterative implementation) has O(Logn) time complexity.
The series that we get in the first loop is 1, c, c2, c3, ... ck. If we put k equals to Logcn, we get cLogcn, which is n. 🤷♂
// c is constant greater than one
for (let i = 2; i <- n; i = Math.pow(i, c)) {
// some O(1) expression
}
for (let i = n; i > 0; i /= c) {
// some O(1) expression
}
const swap = (arr, xp, yp) => {
let temp = arr[xp];
arr[xp] = arr[yp];
arr[yp] = temp;
};
export const selectionSort = (arr = []) => {
const n = arr.length;
for (let i = 0; i < n; i++) {
let minIndex = i;
// let minString = arr[i];
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
// minString = arr[j];
minIndex = j;
}
}
// Swapping the minimum element
// found with the first element.
if (minIndex !== i) {
swap(arr, minIndex, i);
}
}
return arr;
};
export const selectionSortString = (arr = []) => {
const n = arr.length;
for (let i = 0; i < n; i++) {
let minIndex = i;
let minString = arr[i];
for (let j = i + 1; j < n; j++) {
if (arr[j].localeCompare(minString) === -1) {
minString = arr[j];
minIndex = j;
}
}
// Swapping the minimum element
// found with the first element.
if (minIndex !== i) {
swap(arr, minIndex, i);
}
}
return arr;
};
export const bubbleSort = (arr = []) => {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
return arr;
};
Source
export const insertionSort = (arr = []) => {
for (let i = 0; i < arr.length; i++) {
let val = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > val) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = val;
}
return arr;
};
SourceThe array is initially divided into two equal halves and then they are combined in a sorted manner.
We can think of it as a recursive algorithm that continuously splits the array in half until it cannot be further divided. This means that if the array becomes empty or has only one element left, the dividing will stop, i.e. it is the base case to stop the recursion.
If the array has multiple elements, we split the array into halves and recursively invoke the merge sort on each of the halves. Finally, when both the halves are sorted, the merge operation is applied.
Merge operation is the process of taking two smaller sorted arrays and combining them to eventually make a larger one.
leftvariable to 0 and
rightvariable to n-1
mid = (left + right)/2
merge sorton
(left,mid)
merge sorton
(mid+1,right)
left < right
Searching Algorithms