🔢
Algorithms & Data
Ctrlk
  • Overview
    • 1. Sort
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
        • Insertion Sort:: singly linked list
          • Recursive
          • Using Array O(N), O(N)
          • Using Iteration O(N), O(1)
      • Quick Sort
      • Merge Sort
      • Heap Sort
      • Counting Sort
      • Topological Sort
    • 2. Data Structures
    • 3. How to construct Algorithm? Paradigm
  • Algorithm Problems
    • Problem Sources
    • AlgoExpert
    • Daily Algorithms
  • Top 75 LeetCode Questions to Save Your Time
    • Source
    • Problems
  • Tip
    • Page 2
    • LinkedList
Powered by GitBook
On this page
  1. Overview
  2. 1. Sort
  3. Insertion Sort

Insertion Sort:: singly linked list

LogoAlgoExpert | Ace the Coding Interviewswww.algoexpert.io
LogoInsertion Sort List - LeetCodeLeetCode
PreviousInsertion SortNextRecursive

Last updated 16 days ago

function insertionSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    const curr = arr[i];
    for (let j = i - 1; j >= 0; j--) {
      if (arr[j] <= curr) break;
      swap(arr, j, j + 1);
    }
  }

  return arr;
}

function swap(arr, a, b) {
  const temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp;
}

// Do not edit the line below.
exports.insertionSort = insertionSort;