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:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

can you help me solve this question i  need a better understanding 

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 2 1 598
the output is:
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
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 2 1 598 the output is: 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
1 def read_nums():
6000 SAWNP
2
3
4
5
6
7
8
9
13
14
10 def swap(nums, n, m):
11
12
15
16
17
18
22
19
NE
27
28
29
30
31
234 35 36 37
"""Read numbers from input and return them in a list"""
return [int(num) for num in input().split()]
20
21
22
23
24 if __name__ == '__main__':
25
26
33
def print_nums (nums):
"""Output numbers, separating each item by a spaces;
no space or newline before the first number or after the last."""
print('.join([str(n) for n in nums]), end='')
37
"""Exchange nums [n] and nums [m]""
nums [n], nums [m] = nums [m], nums [n]
def insertion_sort (numbers):
"""Sort the list numbers using insertion sort"""|
for i in range(1, len(numbers)):
j = i
# Insert numbers [i] into sorted part
# stopping once numbers [i] is in correct position
while j> 0 and numbers [j] < numbers [j - 1]:
swap(numbers, j, j - 1)
j -= 1
#Step 1: Read numbers into a list
numbers read_nums ()
# Step 2: Output the numbers list
print_nums (numbers);
print(end='\n\n')
# Step 3: Sort the numbers list
insertion_sort(numbers)
print
()
# Step 4: TODO: Output the number of comparisons and swaps performed
Transcribed Image Text:1 def read_nums(): 6000 SAWNP 2 3 4 5 6 7 8 9 13 14 10 def swap(nums, n, m): 11 12 15 16 17 18 22 19 NE 27 28 29 30 31 234 35 36 37 """Read numbers from input and return them in a list""" return [int(num) for num in input().split()] 20 21 22 23 24 if __name__ == '__main__': 25 26 33 def print_nums (nums): """Output numbers, separating each item by a spaces; no space or newline before the first number or after the last.""" print('.join([str(n) for n in nums]), end='') 37 """Exchange nums [n] and nums [m]"" nums [n], nums [m] = nums [m], nums [n] def insertion_sort (numbers): """Sort the list numbers using insertion sort"""| for i in range(1, len(numbers)): j = i # Insert numbers [i] into sorted part # stopping once numbers [i] is in correct position while j> 0 and numbers [j] < numbers [j - 1]: swap(numbers, j, j - 1) j -= 1 #Step 1: Read numbers into a list numbers read_nums () # Step 2: Output the numbers list print_nums (numbers); print(end='\n\n') # Step 3: Sort the numbers list insertion_sort(numbers) print () # Step 4: TODO: Output the number of comparisons and swaps performed
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education