Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#Please use python 1. Improve this version of the bubble sort code so that it better handles lists that are already sorted or mostly sorted.

image text in transcribed

image text in transcribedimage text in transcribed

#Please use python

1. Improve this version of the bubble sort code so that it better handles lists that are already sorted or mostly sorted. def bubble sort faster(values): a. count - 0 for i in range(len(values)-1): for j in range(1en (values)-1-i): count count 1 if values[j] values[j+1]: temp values values[j]values [j+1] values[j+1] - temp return count b. Modify this version of the bubble sort code so that it counts and returns both the number of comparisons and the number of swaps as one number. def bubble sort faster(values): count - 0 for i in range(len(values)-1): for j in range(1en (values)-1-i): count count 1 if values[j] values[j+1]: temp values values ilvalues [i+11 values J+1temp return count We wrote the following program to insert values into a single sorted list. Modify it to build and maintain two separate sorted lists, one of even numbers and another of odd numbers. c. import random def insert sorted (values, num): pos = len(values) count 0 for i in range(len(values)): countcount 1 If num values [i]: break values.append(-1) for j in range(len(values) -1,pos, -1): valuesril - valuesi-1 values[pos]num return count values -[] count for i in range (100): num=r int(e,seee ) count count insert sorted (values,num) print(values) print ("That took %d operations." % count) d. In class, we wrote code to add values to a linked list and print it out. That code appears below. Add one function to this code to find the maximum value in the list. You can assume the list is not empty. Then call that function to find the maximum, and then print it out. import random class Node: def init__(self val): self.val - val self.next None def print list(values): n - values while : None: print(n.val) n n . next valuesNone for i in range(10) num=r int(e,lee) valNode (num) val.next = values values -val print list(values)

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

Step: 3

blur-text-image

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

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books