btholt / btholt/algorithms-exercises

Insertion Sort Discrepancy

Open
#36 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
253
Forks
834
PR merge metrics
No merged PRs in 30d

Description

TLDR;

Change the word **_swap_** to **_shift_**.

=============================

As someone who is new to these algorithms, I found your description of Insertion Sort a bit confusing, especially after seeing your [solution](https://github.com/btholt/algorithms-exercises/blob/main/specs/insertion-sort/insertion-sort.solution.test.js).

> With insertion sort, you treat the first part of your list as sorted and the second part of your list as unsorted. Our algorithm will start by saying everything [before] the 1 index (so just index 0, the first element) is sorted and everything after unsorted. By definition a list of one is already sorted.

So far so good...

> From there, we start with the next element in the list (in this case, the 1 index, the second element) and loop backwards over our sorted list, asking "is the element that I'm looking to insert larger than what's here? If not, you work your way to the back of the array. If you land at the first element of the sorted part of the list, what you have is smaller than everything else and you put it at the start. You then repeat this until you've done it over the whole list!

Ok...here's where it gets confusing...(emphasis mine)...

> The mechanism by which we'll do this is that we'll keep moving bigger elements to the right by **_swapping items in the array as we move_** across the element. When we come to the place where we're going to insert, we'll **_stop doing those swaps_**

Based on _that_ description, having done no other research on the algorithm, here is my solution that keeps **_swapping_** items inside the inner `for` loop until it is in the right position.

```
function insertionSort(nums) {
for (let i = 1; i < nums.length; i++) {
for (let j = i; j > 0; j--) {
if (nums[j] < nums[j - 1]) {
const temp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = temp;
}
}
}
return nums;
}
```

This passes the test and the Sort Visualizer nicely demonstrates how each out-of-order item works its way backwards, **_swapping_** one index at a time, until it's in the right position.

Image

Your [solution](https://github.com/btholt/algorithms-exercises/blob/main/specs/insertion-sort/insertion-sort.solution.test.js), which, after doing some research, I've learned is the correct implementation of Insertion Sort, is about 2x faster than the solution I came up with. Yet, I feel like my solution is more aligned with the way you described Insertion Sort (with **_swaps_**) on the [course website](https://btholt.github.io/complete-intro-to-computer-science/insertion-sort/).

Apparently, with Insertion Sort, you're not really **_swapping_** items. Instead, you store the out-of-order item in a variable like `numberToInsert` and then you start **_shifting_** items to the right until you find the correct position for `numberToInsert`. Then, once you find the correct position, and only then, you insert it at its correct position. In other words, there is no _**swapping**_ at all.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.