The program is the same as shown at the end of the Merge sort section, with the following changes: Numbers are entered by a user in a separate helper function, read_nums(), instead of defining a specific list. • Output of the list has been moved to the function print_nums (). • An output has been added to merge_sort(), showing the indices that will be passed to the recursive function calls. Add code to the merge sort algorithm to count the number of comparisons performed. Add code at the end of the program that outputs "comparisons: " followed by the number of comparisons performed (Ex: "comparisons 12") Hint: Use a global variable to count the comparisons. Note: Take special care to look at the output of each test to better understand the merge sort algorithm. Ex: When the input is: 3 2 1598 the output is: unsorted: 3 2 1 5 9 8 021 35 0 1 0 0 N 2 1 1 34155 3344 sorted: 1 2 3 5 8 9 comparisons: 8

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

fill out the python code

1 # Read integers into a list and return the list.
2 def read_nums ():
3
nums = input().split()
return [int (num) for num in nums]
4
5
6 # Output the content of a list, separated by spaces.
7 def print_nums (numbers):
for num in numbers:
8
print (num, end=' ')
9
10
11
12 def merge(numbers, i, j, k):
13
merged_size=ki + 1
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
46
47
48
49
print()
50
51
merged_numbers = [0] * merged_size
60
61
merge_pos= 0
left_pos
right_pos= j + 1
while left_pos <= j and right_pos <= k:
if numbers [left_pos] < numbers [right_pos]:
merged_numbers [merge_pos] = numbers [left_pos]
left_pos += 1
else:
merged_numbers [merge_pos] = numbers [right_pos]
right pos += 1
merge_pos += 1
while left_pos <= j:
merged_numbers [merge_pos]
left pos + 1
merge_pos += 1
while right pos <= k:
merged_numbers [merge_pos]
43 def merge_sort (numbers, i, k):
j = 0
44
45
if i < k:
right_pos += 1
merge pos += 1
52 if _name___ == '____main____':
53
54
55
56
57
58
59
=
for merge pos in range (merged_size):
numbers [i + merge_pos]
=
numbers = read_nums ()
print("unsorted:", end=' ')
=
j = (1 + k) // 2
print(i, j, "I", j+¹, k)
merge_sort (numbers, i, j)
merge_sort (numbers, j+1, k)
merge (numbers, i, j, k)
print('\nsorted:', end='')
print_nums (numbers)
numbers [left_pos]
numbers [right_pos]
merged_numbers [merge_pos]
print_nums (numbers)
print()
merge_sort (numbers, 0, len(numbers)-1)
Transcribed Image Text:1 # Read integers into a list and return the list. 2 def read_nums (): 3 nums = input().split() return [int (num) for num in nums] 4 5 6 # Output the content of a list, separated by spaces. 7 def print_nums (numbers): for num in numbers: 8 print (num, end=' ') 9 10 11 12 def merge(numbers, i, j, k): 13 merged_size=ki + 1 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 46 47 48 49 print() 50 51 merged_numbers = [0] * merged_size 60 61 merge_pos= 0 left_pos right_pos= j + 1 while left_pos <= j and right_pos <= k: if numbers [left_pos] < numbers [right_pos]: merged_numbers [merge_pos] = numbers [left_pos] left_pos += 1 else: merged_numbers [merge_pos] = numbers [right_pos] right pos += 1 merge_pos += 1 while left_pos <= j: merged_numbers [merge_pos] left pos + 1 merge_pos += 1 while right pos <= k: merged_numbers [merge_pos] 43 def merge_sort (numbers, i, k): j = 0 44 45 if i < k: right_pos += 1 merge pos += 1 52 if _name___ == '____main____': 53 54 55 56 57 58 59 = for merge pos in range (merged_size): numbers [i + merge_pos] = numbers = read_nums () print("unsorted:", end=' ') = j = (1 + k) // 2 print(i, j, "I", j+¹, k) merge_sort (numbers, i, j) merge_sort (numbers, j+1, k) merge (numbers, i, j, k) print('\nsorted:', end='') print_nums (numbers) numbers [left_pos] numbers [right_pos] merged_numbers [merge_pos] print_nums (numbers) print() merge_sort (numbers, 0, len(numbers)-1)
The program is the same as shown at the end of the Merge sort section, with the following changes:
Numbers are entered by a user in a separate helper function, read_nums(), instead of defining a specific list.
Output of the list has been moved to the function print_nums().
• An output has been added to merge_sort(), showing the indices that will be passed to the recursive function calls.
Add code to the merge sort algorithm to count the number of comparisons performed.
Add code at the end of the program that outputs "comparisons: " followed by the number of comparisons performed (Ex: "comparisons:
12")
Hint: Use a global variable to count the comparisons.
Note: Take special care to look at the output of each test to better understand the merge sort algorithm.
Ex: When the input is:
3 2 1 5 9 8
the output is:
unsorted: 3 2 1 5 9 8
0
2
3 5
2 2
1 1
0 1
0 0
3 4155
3 3 4 4
sorted: 1 2 3 5 8 9
comparisons: 8
Transcribed Image Text:The program is the same as shown at the end of the Merge sort section, with the following changes: Numbers are entered by a user in a separate helper function, read_nums(), instead of defining a specific list. Output of the list has been moved to the function print_nums(). • An output has been added to merge_sort(), showing the indices that will be passed to the recursive function calls. Add code to the merge sort algorithm to count the number of comparisons performed. Add code at the end of the program that outputs "comparisons: " followed by the number of comparisons performed (Ex: "comparisons: 12") Hint: Use a global variable to count the comparisons. Note: Take special care to look at the output of each test to better understand the merge sort algorithm. Ex: When the input is: 3 2 1 5 9 8 the output is: unsorted: 3 2 1 5 9 8 0 2 3 5 2 2 1 1 0 1 0 0 3 4155 3 3 4 4 sorted: 1 2 3 5 8 9 comparisons: 8
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Array
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
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