UpperLEFTY / Insertion-sort

Imagine you are playing cards. Somebody is giving you cards one by one. When you are receiving card, you are planning to put them in a way so that the smaller one is on the left. This means you want to insert them in a sorted way

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Insertion-sort

Imagine you are playing cards. Somebody is giving you cards one by one. When you are receiving card, you are planning to put them in a way so that the smaller one is on the left. This means you want to insert them in a sorted way.

Step 1: If the first card you are getting is 5. Just hold the card in your hand.

Step 2: If the second card is 2, you want to put it before 5 so that the two cards you have are sorted. When you are putting the card with number 2 at the left, you are changing the position of the card 5 from first position to second position. First position becomes available and you put 2 there.

Step 3: If the third card is 4. you will start from second position. In the second position, you have card 5 which is bigger than 4. Hence, you will move 5 to the third position. The next card to the left is 2 which is smaller than 4. Leave card 2 where it's at. Insert card 4 in the second position.

Step 4: You receive card 10. It is bigger than the previous card which is 5. Just add it at the last position.

Step 5: The next card is 7. You just move the position of the card 10 to the right and insert card 7.

Step 6: If the last card is 3. You will have to move 10 to the right as it is bigger than 3. Move the next card to the left it is 7 which is bigger than 3 Move it to the right move 5 & 4 to the right. Put the number 3 before 2 as 2 is smaller than 3.

It starts with the second element. Pick the second element to be inserted and then compare to the previous element. If the first one is bigger, move the first one to second position and second one at first. Now first and second item are sorted.

Then, pick the third element and check whether the second element is bigger than the third. Keep using the same method until you hit the first element or a element smaller than the element you are comparing with. When you get an item smaller than the picked item, you insert it.

About

Imagine you are playing cards. Somebody is giving you cards one by one. When you are receiving card, you are planning to put them in a way so that the smaller one is on the left. This means you want to insert them in a sorted way


Languages

Language:JavaScript 100.0%