Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Insertion in Order Write a function that receives a string of numbers lst and returns a new list with the same values but sorted in

Insertion in Order

Write a function that receives a string of numbers lst and returns a new list with the same values but sorted in ascending order. (*Note: You are not allowed to use predefined sorting functions!)

Sample Input

2 5 3 1 9 5

Sample Output

[1, 2, 3, 5, 5, 9]

def parse_list(string): result = [] for x in string.split(" "): result.append(int(x)) return result

def insert_sorted(lst, x): # add x at the end of lst lst.append(x) # your code here # initialize i to the last index of the lst i = len(lst) - 1 # repeat the loop while i is greater than 0 and # lst[i] is less than lst[i-1] while i > 0 and lst[i] < lst[i - 1]: # your code here # interchange the values in the positions i and i - 1 lst[i], lst[i - 1] = lst[i - 1], lst[i] i = i - 1

def sort(lst): # To implement the function use the following strategy: # create a new empty list result. result = [] # Traverse lst and insert each value in result using # the function insert_sorted return result

s = input() l = parse_list(s) x = int(input()) insert_sorted(l, x) print(l)

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

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Do method headers require parentheses?

Answered: 1 week ago

Question

Define language, and recognize its properties.

Answered: 1 week ago

Question

Connect with your audience

Answered: 1 week ago