Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The script has four steps: Read a list of integers ( no duplicates ) . Output the numbers in the list. Perform an 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]
Ex: When the input is:
321598
the output is:
321598
231598
123598
123598
123598
123589
comparisons: 7
swaps: 4
I keep on getting an error on my program, please help in python: def read_nums():
"""Read numbers from input and return them as a list of integers."""
return [int(num) for num in input().split()]
def print_nums(nums):
"""Output numbers, separating each item by a space; no space or newline before the first number or after the last."""
print(''.join([str(n) for n in nums]))
def swap(nums, n, m):
"""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, counting comparisons and swaps, and printing the list at each outer loop iteration."""
global comparisons, swaps
comparisons =0
swaps =0
for i in range(1, len(numbers)):
j = i
# Insert numbers[i] into the sorted part, stopping once numbers[i] is in the correct position
while j >0 and numbers[j] numbers[j -1]:
comparisons +=1
swap(numbers, j, j -1)
swaps +=1
j -=1
if j >0:
comparisons +=1 # Count the comparison where the while loop condition fails
print_nums(numbers)
print() # Newline for clarity
if __name__=='__main__':
# Step 1: Read numbers into a list
numbers = read_nums()
# Step 2: Output the numbers list
print_nums(numbers)
print("
") # Print a newline for separation before sorting begins
# Step 3: Sort the numbers list
insertion_sort(numbers)
# Step 4: Output the number of comparisons and swaps performed
print(f"
comparisons: {comparisons}")
print(f"swaps: {swaps}") Output is nearly correct, but whitespace differs. See highlights below.
Special character legend
Input
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

Show that P(C c ) = 1 P(C).

Answered: 1 week ago