# Bug in Insertion Sort Code

**URL:** https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699
**Category:** programming
**Created:** [December 15, 2023, 8:07pm UTC](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699 "2023-12-15T20:07:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![transbot](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@transbot](https://forum.kirupa.com/u/transbot)
#### Post date: [December 15, 2023, 8:07pm UTC](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699/1 "2023-12-15T20:07:30Z")

</div>

found a bug in the implementation of the insertion sort algorithm in Chapter 23. The issue arises in the following code snippet:

```auto
function insertionSort(input) {
  // Variable to store the current element being compared
  let activeNumber;

  // Loop through the array starting from the second element
  for (let i = 1; i < input.length; i++) {
    // Store the current element in the activeNumber
    activeNumber = input[i];

    // Inner loop to compare activeNumber with the elements before it
    for (let j = i - 1; j >= 0; j--) {
      if (input[j] > activeNumber) {
        // Move the greater element one position ahead for the activeNumber
        input[j + 1] = input[j];
      } else {
        // If we find an element that is smaller than or equal to the activeNumber, exit the inner loop
        break;
      }
    }

    // Place the activeNumber in its correct sorted position
    input[j + 1] = activeNumber;
  }
}

let myinput = [24, 10, 17, 9, 5, 9, 1, 23, 300];
insertionSort(myinput);
alert(myinput);

```

The bug occurs because the variable `j` goes out of scope, rendering the insertion sort ineffective. To address this issue, I suggest modifying the code as follows:

```auto
// ...

// Loop through the array starting from the second element
for (let i = 1; i < input.length; i++) {
  // Store the current element in the activeNumber
  activeNumber = input[i];
  let j; // Declare j outside the loop to prevent it from going out of scope

  // Inner loop to compare activeNumber with the elements before it
  for (j = i - 1; j >= 0; j--) {
    if (input[j] > activeNumber) {
      // Move the greater element one position ahead for the activeNumber
      input[j + 1] = input[j];
    } else {
      // If we find an element that is smaller than or equal to the activeNumber, exit the inner loop
      break;
    }
  }

  // Place the activeNumber in its correct sorted position
  input[j + 1] = activeNumber;
}

// ...

```

This modification ensures that the variable `j` retains its value outside the loop, preventing any scope-related issues.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 16, 2023, 3:17am UTC](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699/2 "2023-12-16T03:17:09Z")

</div>

Thanks! I’ve gone ahead and updated the errata thread with this! 🙂

> [@Errata: Data Structures and Algorithms Book!](http://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668/5):
>
> Insertion Sort - Fixed undeclared j variable The following code declares the j variable correctly to avoid any out-of-scope errors: function insertionSort(input) { // Variable to store the current element being compared let activeNumber; // Loop through the array starting from the second element (index 1) for (let i = 1; i \< input.length; i++) { // Store the current element in the activeNumber variable activeNumber = input[i]; let j; // Inner loop to compare the …
