Sort it yourself
Inputs
values is a list of 0 to 12 whole numbers, each from -20 to 20.
Your tasks
- Create a copy
resultofvalues. - Use an outer loop for each position
ifrom 1 up. Holdresult[i]asheld, then use an innerwhileloop to shift bigger numbers to the right, one position, until you find the place forheld. Putheldin that place. - Complete
insertionSortso it returnsresult, the numbers in increasing order. Return it, do not print it.
Example
With values = [5, 2, 9, 1, 5, 6], insertionSort returns [1, 2, 5, 5, 6, 9].
Rules
- Do not use a built-in sort.
- Do not change
values.
Test data
TEST DATA 1: values is [5, 2, 9, 1, 5, 6]
TEST DATA 2: values is []
Practice focus
Prefix sums, counting, binary search, best stretch and sorting, all written as plain loops. This is where loops turn into named algorithms. You have already written the loops.