The script has four steps: 1. Read a list of integers (no duplicates). 2. Output the numbers in the list. 3. Perform an insertion sort on the list. 4. 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] Ex: When the input is: 3 21598 the output is 321598 231598 1 2 3598 1235 98 1 235 98 1 235 89 comparisons: 7 Swaps:

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter16: Searching, Sorting And Vector Type
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question
Python
The script has four steps:
1. Read a list of integers (no duplicates).
2. Output the numbers in the list.
3. Perform an insertion sort on the list.
4. 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]
Ex: When the input is:
3 21598
the output is:
321598
2315 98
1 235 98
1 235 98
1 2 35 98
1 2 35 89
comparisons: 7
swaps: 4
Transcribed Image Text:The script has four steps: 1. Read a list of integers (no duplicates). 2. Output the numbers in the list. 3. Perform an insertion sort on the list. 4. 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] Ex: When the input is: 3 21598 the output is: 321598 2315 98 1 235 98 1 235 98 1 2 35 98 1 2 35 89 comparisons: 7 swaps: 4
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning