# Insertion Sort:: singly linked list

{% embed url="<https://www.algoexpert.io/questions/insertion-sort>" %}

<figure><img src="https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-e96895714c194fc0b1b3515bd0108dd0810a72a3%2FScreenshot%202023-06-07%20at%2010.11.10.png?alt=media" alt=""><figcaption></figcaption></figure>

```javascript
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;

```

{% embed url="<https://leetcode.com/problems/insertion-sort-list/description/>" %}
