Insertion Sort 1


Collapse Content

Goal: Before sorting entire lists, sort one element in a list.

Given a sorted list of numbers with an unsorted number V in the right-most spot, can you write some simple code to insert V into the list so it remains sorted?

Print the list every time a value is shifted, until the list is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.

How to Solve: Use the standard boilerplate to process the numbers into an array. You can then copy the right-most value (V) to a variable, and consider its cell “empty”. Since this leaves an extra cell empty on the right, you can shift every number over until V can be inserted. This will create a duplicate of each value, but when you reach the right spot, you can replace a value with V.

Output Format On each line, output the entire array every time an item is shifted in it. This will include duplicate values.

Challenge

Given a sorted list with an unsorted number V in the right-most cell, can you write some simple code to insert V into the array so it remains sorted?

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • Dear Admin,

    As what i can see from the "Your Output" it is the same as "Correct Output", however the last array is not displayed so im not sure if im getting "Incorrect" due to the last array or something else.

    cont...
  • @David, it displays all the output. Make sure you're printing the final steps too.

  • Thanks Admin, i noticed just now... double checking on my end needs to be improved.

All Node Comments
Contact Us