Data Structures and AlgorithmsSorting Algorithms

Sorting arranges data in a specific order. Common algorithms include Bubble Sort, Merge Sort, Quick Sort, and Insertion Sort. They vary in complexity and use cases.

// Example: Bubble Sort
function bubbleSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
}
console.log(bubbleSort([4, 2, 7, 1]));