
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![Implement 5 functions in Python that would sort an unsorted list, i.e
def bubble_sort(my_list):
def selection_sort(my_list):
def insertion_sort(my_list):
def quick_sort(mylist):
def insertion_sort(mylist, left, right):
Provide a main that uses each of the above functions to sort a list of length 100 and make sure it works as expected
Step 2
Add lines of code to the above functions so that apart from sorting the received list, it calculates T(n) and returns it. This means that each of the above functions will have a
return value which is the exact number of operations executed to perform the sort.
Use a main to test yout T(n) calculation. A good way of testing your T(n) is this. Send a best case scenario, a worst case scenario and an average scenario to your sort
function and see what numbers come out for your T(n). Explain what best, worst and average case scenarios will be for a sorting algorithm.
Step 3
Use the sort functions with T(n) calculation feature to plot T(n) vs. n for a wide range of list sizes. Say 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000, 1000000,
10000000. Make sure you use a WORST CASE scenario for your list so that your T(n) is a good reflection of O(n). Do you see your curves aligned with what we learnt
about the performance of the sort algorithms
Step 4
Using time library in python, time your sort algorithms for the same list sizes that you plotted in step 3 and this time plot algrithm completion time vs n. Interpret the results
while compared to the step 3 plot and compared your knowledge of the algorithm's performances. Don't forget to use WORST CASE scenario.
Example for Step 2 in bubble sort:
def bubble_sort (my_list):
steps = 0
for i in range (0, len (my_list) - 1):
for j in range (0, len (my_list) - 1 - i):
if my_list[j] > my_list[j + 1]:
steps += 4 # 4 operations, 3 for the swap and one for the comparison
my_list[j], my_list[j + 1] = my_list[j+1], my_list[j]
return steps
Code to create a random list of a specific size for steps 3 and 4:
import random
listSize = 50
rand_list = [random.randint (0,101) for val in range (listSize)]
rand_list.sort (reverse=True)
print("steps needed for sorting: {}". format (bubble_sort (rand_list)))](https://content.bartleby.com/qna-images/question/d99806c6-42e8-450c-a219-a2d748677dac/18d10616-d82e-4278-9a72-e1e63b5fa39e/ombhfd3_thumbnail.png)
Transcribed Image Text:Implement 5 functions in Python that would sort an unsorted list, i.e
def bubble_sort(my_list):
def selection_sort(my_list):
def insertion_sort(my_list):
def quick_sort(mylist):
def insertion_sort(mylist, left, right):
Provide a main that uses each of the above functions to sort a list of length 100 and make sure it works as expected
Step 2
Add lines of code to the above functions so that apart from sorting the received list, it calculates T(n) and returns it. This means that each of the above functions will have a
return value which is the exact number of operations executed to perform the sort.
Use a main to test yout T(n) calculation. A good way of testing your T(n) is this. Send a best case scenario, a worst case scenario and an average scenario to your sort
function and see what numbers come out for your T(n). Explain what best, worst and average case scenarios will be for a sorting algorithm.
Step 3
Use the sort functions with T(n) calculation feature to plot T(n) vs. n for a wide range of list sizes. Say 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000, 1000000,
10000000. Make sure you use a WORST CASE scenario for your list so that your T(n) is a good reflection of O(n). Do you see your curves aligned with what we learnt
about the performance of the sort algorithms
Step 4
Using time library in python, time your sort algorithms for the same list sizes that you plotted in step 3 and this time plot algrithm completion time vs n. Interpret the results
while compared to the step 3 plot and compared your knowledge of the algorithm's performances. Don't forget to use WORST CASE scenario.
Example for Step 2 in bubble sort:
def bubble_sort (my_list):
steps = 0
for i in range (0, len (my_list) - 1):
for j in range (0, len (my_list) - 1 - i):
if my_list[j] > my_list[j + 1]:
steps += 4 # 4 operations, 3 for the swap and one for the comparison
my_list[j], my_list[j + 1] = my_list[j+1], my_list[j]
return steps
Code to create a random list of a specific size for steps 3 and 4:
import random
listSize = 50
rand_list = [random.randint (0,101) for val in range (listSize)]
rand_list.sort (reverse=True)
print("steps needed for sorting: {}". format (bubble_sort (rand_list)))
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

Knowledge Booster
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
- Write a function called remove_odd that takes a list of numbers that have both even and odd numbers mixed.# Function should remove all the odd numbers and return a compact list which only contains the even numbers. Example1: Function Call:remove_odd ([21, 33, 44, 66, 11, 1, 88, 45, 10, 9])Output:[44, 66, 88, 10]arrow_forwardHow do I make a program that keeps track of a phone speed dialer directory which has 5 fixed slots, without changing the starter code. # Program to simulate a speed dialer# Complete the functions listed below that work with# two lists that manage a dialer with 5 fixed slots.def printList(names,numbers):'''function to show the current dialer data as shown in sample runs'''#Implement this functionpassdef updateSlot(names,numbers):'''Function to handle update by slot number command whichprompts the user for the slot number to be updatedUses try-except to handle error cases:if slot number not within valid rangeif user enters non-integer inputIf slot number is valid, thenprompts the user for the new name and new numberand updates entries for the slot number.prints "Updated slot number X" where X is the slot number user entered.'''#Implement this functionpassdef dialByName(names,numbers):'''Function to handle dial by name command whichprompts the user for the namechecks if the name is…arrow_forwardWrite a python program that takes two lists, merges thetwo lists, sorts the resulting list, and then finds the median of theelements in the two lists. You cannot use python build-in sort() function #todo#You can use math functions like math.floor to help the calculationimport math #todo#You can use math functions like math.floor to help the calculation import math def task7(list_in_1, list_in_2): # YOUR CODE HERE return median please use these variables no print function pleasearrow_forward
- In pythonWrite a function called compter_les_votes that accepts a list of ballots as input (of arbitrary length) and produces a report as a dictionary. The ballots have the form of a couple (valid, condidat) where candidate is the name of one of the candidates in the election, and valid is a boolean that indicates whether or not the ballot is valid. For example, for the following list of bulletins: [(True, 'Pierre'), (False, 'Jean'), (True, 'Pierre'), (True, 'Jacques')]Your function must return the following dictionary: { "number of ballots": 4, "invalid ballots": 1, "results": { "Stone": 2, "Jacques": 1 }}Note that you do not have to display the dictionary, only return it.arrow_forwardThis is for Python version 3.8.5 and please include pseudocode. nums = [8, 15, 6, 17, 33, 20, 14, 9, 12]Write Python code that: uses a function to print the size of the list named nums uses a function to print the largest number in nums prints the element 14 by using its index makes a slice named subnums containing only 33, 20, and 14arrow_forwardWrite a function called find_duplicates which accepts one list as a parameter. This function needs to sort through the list passed to it and find values that are duplicated in the list. The duplicated values should be compiled into another list which the function will return. No matter how many times a word is duplicated, it should only be added once to the duplicates list. NB: Only write the function. Do not call it. For example: Test Result random_words = ("remember","snakes","nappy","rough","dusty","judicious","brainy","shop","light","straw","quickest", "adventurous","yielding","grandiose","replace","fat","wipe","happy","brainy","shop","light","straw", "quickest","adventurous","yielding","grandiose","motion","gaudy","precede","medical","park","flowers", "noiseless","blade","hanging","whistle","event","slip") print(find_duplicates(sorted(random_words))) ['adventurous', 'brainy', 'grandiose', 'light', 'quickest', 'shop', 'straw', 'yielding']…arrow_forward
- PYTHON!!! Write a function that will filter course grades that are stored in a list, creating and returning a new list. Your implementation must have the following rules: - The function must accept two (and only two) parameters: The first is: the maximum integer value for any grade to remain in the list (any value above the maximum will be filtered and removed from the list). The second is: the list of grades (you can assume the list contains only integer values) - The function must return a new list and not modify the existing list. - The function must use a list comprehension to filter the existing list and create the new list that is returned. - DO NOT USE THE: built-in-"filter" function.arrow_forwardIN PYTHON PLEASE Write a function def sameSet(a, b) that checks whether two lists have the same ele- ments in some order, ignoring duplicates. For example, the two lists 149 16 9 7 4 9 11 and 11 11 7 9 16 4 1 would be considered identical. You will probably need one or more helper functions.arrow_forwardIn pythonarrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education