← Back to Data Structures & Algorithms
Question 563
Insertion Sort
Insertion Sort
Write insertion_sort(values: list[int]) -> None that sorts the list in place: for each index i from 1, take current as the value there, and with a second index j starting at i - 1 shift every larger value one slot to the right (while j is at least 0 and values[j] is above current), then drop current into the gap at j + 1. Already-sorted input makes this O(n); the worst case is O(n squared). Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated integers (possibly empty); the tests call your function and then print the list.
Expected Output:
[1, 2, 3, 4, 5, 6]= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Data Structures & AlgorithmsBig-O
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.