Python code pls! 14.9 LAB: 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] Code that I need to modify(this is the given format, and I have to edit it following the #directions): def read_nums():     """Read numbers from input and return them in a list"""     return [int(num) for num in input().split()] 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='') 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"""     # 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 > 0 and numbers[j] < numbers[j - 1]:             swap(numbers, j, j - 1)             j -= 1 if __name__ == '__main__':     # 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Topic Video
Question

Python code pls!

14.9 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.
  • 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]

Code that I need to modify(this is the given format, and I have to edit it following the #directions):

def read_nums():
    """Read numbers from input and return them in a list"""
    return [int(num) for num in input().split()]

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='')

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"""
    # 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 > 0 and numbers[j] < numbers[j - 1]:
            swap(numbers, j, j - 1)
            j -= 1

if __name__ == '__main__':
    # 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 2 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I have the exact same code as you have provided, with the indentation, but it is giving me this error right now.

1: Compare output
Output differs. See highlights below. Special character legend
Input
Your output
output
2: Compare output
3 2 1 5 98
3 2 1 5 98
comparisons:
swaps: 4
3 2 1 5 98
2 3 1
1 2 3
1 2 3
5 9 8
1 2 3 5
1 2 3
5
5
5
9 8
9
8
9 8
8 9
5
comparisons: 7
swaps: 4
Output differs. See highlights below. Special character legend
0/2
0/2
Transcribed Image Text:1: Compare output Output differs. See highlights below. Special character legend Input Your output output 2: Compare output 3 2 1 5 98 3 2 1 5 98 comparisons: swaps: 4 3 2 1 5 98 2 3 1 1 2 3 1 2 3 5 9 8 1 2 3 5 1 2 3 5 5 5 9 8 9 8 9 8 8 9 5 comparisons: 7 swaps: 4 Output differs. See highlights below. Special character legend 0/2 0/2
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY