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] Ex: When the input is: 3 2 1 5 9 8 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

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

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 5 9 8

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

21.12 LAB: Insertion sort
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.
• utput 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:
32 159 8
the output is:
3 2 1 5 9 8
2 31 5 9 8
1 2 3 5 9 8
1 2 3 5 9 8
1 2 3 5 9 8
12 3 5 8 9
comparisons: 7
Swaps: 4
376584.2127290.gazgy
Transcribed Image Text:21.12 LAB: Insertion sort 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. • utput 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: 32 159 8 the output is: 3 2 1 5 9 8 2 31 5 9 8 1 2 3 5 9 8 1 2 3 5 9 8 1 2 3 5 9 8 12 3 5 8 9 comparisons: 7 Swaps: 4 376584.2127290.gazgy
main.py
Load default template.
1 def read_nums ( ):
"Read numbers from input and return them in a list"""
return [int(num) for num in input ().split()]
2
3
4
5 def print_nums (nums):
i"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=")
6.
7
8
10 def swap(nums, n, m):
"*"Exchange nums [n] and nums[m]""-
nums [n], nums [m] = nums[m], nums[n]
11
12
13
14 def insertion_sort(numbers):
"*"Sort the list numbers using insertion sort"""
# TODO: Count comparisons and swaps. Output the array at the end of each iteration.
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 > e and numbers[j] < numbers[j - 1]:
swap (numbers, j, i - 1)
j -- 1
15
16
17
18
20
21
22
23
24
25 if _name ==
main_':
--
# step 1: Read numbers into a list
numbers = read_nums ()
26
27
28
# step 2: Output the numbers List
print_nums (numbers);
print(end='\n\n')
29
30
31
32
# step 3: Sort the numbers List
insertion_sort (numbers)
print()
33
34
35
36
37
# step 4: TODO: Output the number of comparisons and swaps performed
38
HN2 2222
Transcribed Image Text:main.py Load default template. 1 def read_nums ( ): "Read numbers from input and return them in a list""" return [int(num) for num in input ().split()] 2 3 4 5 def print_nums (nums): i"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=") 6. 7 8 10 def swap(nums, n, m): "*"Exchange nums [n] and nums[m]""- nums [n], nums [m] = nums[m], nums[n] 11 12 13 14 def insertion_sort(numbers): "*"Sort the list numbers using insertion sort""" # TODO: Count comparisons and swaps. Output the array at the end of each iteration. 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 > e and numbers[j] < numbers[j - 1]: swap (numbers, j, i - 1) j -- 1 15 16 17 18 20 21 22 23 24 25 if _name == main_': -- # step 1: Read numbers into a list numbers = read_nums () 26 27 28 # step 2: Output the numbers List print_nums (numbers); print(end='\n\n') 29 30 31 32 # step 3: Sort the numbers List insertion_sort (numbers) print() 33 34 35 36 37 # step 4: TODO: Output the number of comparisons and swaps performed 38 HN2 2222
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 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
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