function bubbleSort(arr) {
let n = arr.length;
let swapped;
for (let i = 0; i < n - 1; i++) {
swapped = false;
for (let j = 0; j < n - i - 1; j++) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap if they are in wrong order
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
swapped = true;
}
}
// If no swaps occurred, array is sorted
if (!swapped) break;
}
return arr;
}
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
Key Characteristics:
function selectionSort(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum with the first element
if (minIdx !== i) {
[arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
}
}
return arr;
}
Selection Sort divides the input list into two parts: a sorted sublist and an unsorted sublist. The algorithm repeatedly finds the minimum element from the unsorted part and moves it to the end of the sorted part.
Key Characteristics:
function mergeSort(arr, left = 0, right = arr.length - 1) {
if (left >= right) return;
const mid = Math.floor((left + right) / 2);
// Recursively sort both halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right);
}
function merge(arr, left, mid, right) {
const leftArr = arr.slice(left, mid + 1);
const rightArr = arr.slice(mid + 1, right + 1);
let i = 0, j = 0, k = left;
while (i < leftArr.length && j < rightArr.length) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
// Copy remaining elements
while (i < leftArr.length) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < rightArr.length) {
arr[k] = rightArr[j];
j++;
k++;
}
}
Merge Sort is a divide-and-conquer algorithm that divides the input array into two halves, recursively sorts each half, and then merges the sorted halves.
Key Characteristics:
function quickSort(arr, low = 0, high = arr.length - 1) {
if (low < high) {
// Partition the array and get pivot index
const pi = partition(arr, low, high);
// Recursively sort elements before and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
function partition(arr, low, high) {
// Choose rightmost element as pivot
const pivot = arr[high];
// Index of smaller element
let i = low - 1;
for (let j = low; j < high; j++) {
// If current element is smaller than pivot
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
// Swap pivot to correct position
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
}
Quick Sort is a divide-and-conquer algorithm that selects a 'pivot' element and partitions the array around the pivot, placing smaller elements to the left and larger elements to the right.
Key Characteristics:
function insertionSort(arr) {
let n = arr.length;
for (let i = 1; i < n; i++) {
// Element to be inserted
let key = arr[i];
let j = i - 1;
// Move elements greater than key one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
// Insert key at correct position
arr[j + 1] = key;
}
return arr;
}
Insertion Sort builds the final sorted array one element at a time by repeatedly taking the next element and inserting it into its correct position in the already sorted part.
Key Characteristics: