Insertion Sort
Advanced
Insertion Sort Challenge
Welcome to the magical world of Insertion Sort! In this challenge, you'll create a function that sorts a list of numbers in ascending order using the insertion sort algorithm.
Insertion sort is a simple and intuitive algorithm that works like sorting cards in your hand. Imagine you have a stack of playing cards and you want to sort them. You take one card at a time and insert it in the correct position within the sorted part of the stack.
Here’s how it works in your function:
- Start with the second element of the list.
- Compare it to the elements before it.
- Swap the element with the previous elements until you find the right position.
- Continue doing this for each element until the entire list is sorted.
Example
insertion_sort([3, 1, 4, 1, 5, 9]) # Returns: [1, 1, 3, 4, 5, 9] insertion_sort([10, 3, 15, 7, 8]) # Returns: [3, 7, 8, 10, 15] insertion_sort([1, 2, 3, 4, 5]) # Returns: [1, 2, 3, 4, 5]
Are you ready to put your sorting skills to the test and sort these numbers like a pro? Let’s get started!
Inputs
Arg 1:
[3,1,4,1,5,9]Expected Output
[1, 1, 3, 4, 5, 9]Actual Output