fast-sort-search
Version:
This is the package for searching and sorting in array.
28 lines (26 loc) • 675 B
JavaScript
//Bubble Sort
function fastSort(arr) {
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements if they are in the wrong order
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
//Linear Search
function fastSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Return index if target found
}
}
return -1; // Return -1 if target not found
}
module.exports = {
fastSort,
fastSearch,
};