Question
Python question: Using Bubble sort using recursion, Create a function recursive () which has as its parameters, sort_list and n . n should have a
Python question: Using Bubble sort using recursion,
Create a function recursive() which has as its parameters, sort_list and n. n should have a default value of zero, and refers to whether the list should be sorted by the first or second value in the tuple. to_sort()should call itself as part of the process.
The sorted part of the list will be growing from the end of the list. Each time you call recursive_sort, you should be calling with a smaller list. You can use slicing for this.
The assisgnment is that the list is sorted, and have to call itself with a smaller list, and output should be full sorted list. Can't use list.sort().
########################### Can't change bold
def recursive(sort_list,n=0): new_list = sort_list.copy() for i in range(len(new_list)-1): print(new_list) if new_list[i][n] > new_list[i+1][n]: (new_list[i+1], new_list[i]) = (new_list[i], new_list[i + 1]) if len(new_list) > 1: print("") recursive(new_list[:-1]) else: return new_list to_sort = [('49', 'S'), ('48', 'F'), ('47', 'C'), ('46', 'W'), ('45', 'T'), ('G', 'G')] print(recursive(to_sort, 0))
######################
Sample output should be:
[('49', 'S'), ('48', 'F'), ('47', 'C'), ('46', 'W'), ('45', 'T'), ('G', 'G')] [('48', 'F'), ('49', 'S'), ('47', 'C'), ('46', 'W'), ('45', 'T'), ('G', 'G')] [('48', 'F'), ('47', 'C'), ('49', 'S'), ('46', 'W'), ('45', 'T'), ('G', 'G')] [('48', 'F'), ('47', 'C'), ('46', 'W'), ('49', 'S'), ('45', 'T'), ('G', 'G')] [('48', 'F'), ('47', 'C'), ('46', 'W'), ('45', 'T'), ('49', 'S'), ('G', 'G')]
[('48', 'F'), ('47', 'C'), ('46', 'W'), ('45', 'T'), ('49', 'S')] [('47', 'C'), ('48', 'F'), ('46', 'W'), ('45', 'T'), ('49', 'S')] [('47', 'C'), ('46', 'W'), ('48', 'F'), ('45', 'T'), ('49', 'S')] [('47', 'C'), ('46', 'W'), ('45', 'T'), ('48', 'F'), ('49', 'S')]
[('47', 'C'), ('46', 'W'), ('45', 'T'), ('48', 'F')] [('46', 'W'), ('47', 'C'), ('45', 'T'), ('48', 'F')] [('46', 'W'), ('45', 'T'), ('47', 'C'), ('48', 'F')]
[('46', 'W'), ('45', 'T'), ('47', 'C')] [('45', 'T'), ('46', 'W'), ('47', 'C')]
[('45', 'T'), ('46', 'W')]
[('45', 'T'), ('46', 'W'), ('47', 'C'), ('48', 'F'), ('49', 'S'), ('G', 'G')]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started