4.9 LAB: Insertion sort The script has four steps: Read a list of integers (no duplicates). Output the numbers in the list. Perform an insertion sort on the list. Output the number of comparisons and swaps performed during the insertion sort. Steps 1 and 2 are provided in the script. Implement step 3 based on the insertion sort algorithm in the book. Modify insertion_sort() to: Count the number of comparisons performed. Count the number of swaps performed. Output the list during each iteration of the outside loop. Implement step 4 at the end of the script. Hints: In order to count comparisons and swaps, modify the while loop in insertion_sort(). Use global variables for comparisons and swaps. The script includes three helper functions: read_nums() # Read and return a list of integers. print_nums(nums) # Output the numbers in nums swap(nums, n, m) # Exchange nums[n] and nums[m] Code I have so far: def read_nums(): return [int(num) for num in input().split()] def print_nums(nums): print(' '.join([str(n) for n in nums]), end='') def swap(nums, n, m): nums[n], nums[m] = nums[m], nums[n] def insertion_sort(numbers): comparisons = 0 swaps = 0 for i in range(1, len(numbers)): comparisons += 1 j = i while j > 0 and numbers[j] < numbers[j - 1]: swap(numbers, j, j - 1) swaps += 1 j -= 1 print('comparisons:', comparisons) print('swaps:', swaps) if __name__ == '__main__': numbers = read_nums() print_nums(numbers); print(end='\n\n') insertion_sort(numbers) print() Error I am getting: Input 3 2 1 5 9 8 Your output 3 2 1 5 9 8 comparisons: 5 swaps: 4 Expected output 3 2 1 5 9 8 2 3 1 5 9 8 1 2 3 5 9 8 1 2 3 5 9 8 1 2 3 5 9 8 1 2 3 5 8 9 comparisons: 7 swaps: 4
14.9 LAB: Insertion sort
The script has four steps:
- Read a list of integers (no duplicates).
- Output the numbers in the list.
- Perform an insertion sort on the list.
- Output the number of comparisons and swaps performed during the insertion sort.
Steps 1 and 2 are provided in the script.
Implement step 3 based on the insertion sort
- Count the number of comparisons performed.
- Count the number of swaps performed.
- Output the list during each iteration of the outside loop.
Implement step 4 at the end of the script.
Hints: In order to count comparisons and swaps, modify the while loop in insertion_sort(). Use global variables for comparisons and swaps.
The script includes three helper functions:
- read_nums() # Read and return a list of integers.
- print_nums(nums) # Output the numbers in nums
- swap(nums, n, m) # Exchange nums[n] and nums[m]
Code I have so far:
def read_nums():
return [int(num) for num in input().split()]
def print_nums(nums):
print(' '.join([str(n) for n in nums]), end='')
def swap(nums, n, m):
nums[n], nums[m] = nums[m], nums[n]
def insertion_sort(numbers):
comparisons = 0
swaps = 0
for i in range(1, len(numbers)):
comparisons += 1
j = i
while j > 0 and numbers[j] < numbers[j - 1]:
swap(numbers, j, j - 1)
swaps += 1
j -= 1
print('comparisons:', comparisons)
print('swaps:', swaps)
if __name__ == '__main__':
numbers = read_nums()
print_nums(numbers);
print(end='\n\n')
insertion_sort(numbers)
print()
Error I am getting:
![1: Compare output
Output differs. See highlights below. Special character legend
Input
Your output
output
2: Compare output
3 2 1 5 98
3 2 1 5 98
comparisons:
swaps: 4
3 2 1 5 98
2 3 1
1 2 3
1 2 3
5 9 8
1 2 3 5
1 2 3
5
5
5
9 8
9
8
9 8
8 9
5
comparisons: 7
swaps: 4
Output differs. See highlights below. Special character legend
0/2
0/2](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1f559ff2-7390-4b86-9602-ab51f205485c%2F0b333a55-6fb2-4ead-a55e-404f48f1ca8e%2Fidhgsab_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)