Question

In: Computer Science

Intro to python question: #Recall in the lesson on sorts that we had you complete the...

Intro to python question:

#Recall in the lesson on sorts that we had you complete the
#Bubble and Selection sort, and we showed you Merge sort.
#We didn't show any of insertion sort, and I bet you can
#guess why.
#
#Implement insertion sort below.
#
#Name your function 'insertion'. insertion should take as
#input a list, and return as output a sorted list. Note that
#even though technically a sorting method does not have to
#return the sorted list, yours should.
#
#If you're stuck on where to start, or having trouble
#visualizing or understanding how exactly insertion sort
#works, check out this website - https://visualgo.net/sorting
#It provides a visual representation of all of the sorting
#algorithms as well as pseudocode you can base your own code
#off of.


#Write your code here!

#The code below will test your function. If your function
#works, it will print: [1, 2, 3, 4, 5].
print(insertion([5, 1, 3, 2, 4]))

Solutions

Expert Solution

Please find the code below and also attached execution output screen shot below.

Added all the comments for your understanding in each ine of the code.


#This below program is for insertion sort and function will return sorted array

#  this below is the function where we implemented insertion sort logic
def insertion(input_array): 

        # we will start looping and we will traverse through each element  from index value 1 of array (i.e index = 1 ) to the end of  complete array length 
  
  # array length will be obtained by len funtcion 
        for each_elem in range(1, len(input_array)): 
    
    #here we are extracting the current indexed array value and storing it in current_element
                current_element = input_array[each_elem] 

                # Now we will move those elements of input_array[0..each_elem-1], which are greater than current_element in such a way that
    # to 1 position ahead of their current position which we store this index in iter_elem

    #all the below code which is in while loop is for moving elements on position ahead which have values greater than current_element value
                iter_elem = each_elem-1
                while iter_elem >=0 and current_element < input_array[iter_elem] : 
                                input_array[iter_elem+1] = input_array[iter_elem] 
                                iter_elem -= 1
                input_array[iter_elem+1] = current_element 
  
  #we will return now sorted array i.e input_array
        return input_array


# We will use input_array where we required to sort 
input_array = [5, 1, 3, 2, 4] 

#you can call function insertion by below line also

#print(insertion(input_array))

#this below statement is from your problem statement
print(insertion([5, 1, 3, 2, 4]))

Screenshot:


Related Solutions

WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Case Study: To recall or not to recall? That is the question You are part of...
Case Study: To recall or not to recall? That is the question You are part of the executive team of Nature Only, LLC, a small business that manufactures wholesome organic snacks, such as granola bars, trail mix, and popcorn. One of your suppliers sent an email to your CEO stating that the last stock of oats sent to Nature Only may have been contaminated with Listeria, which can either be completely harmless or cause serious and sometimes fatal infections in...
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years....
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years. Your favorite uncle has promised to help you with your school expenses by giving you the following amounts for Christmas: Year 2020 2021 2022 2023 2024 Cash flow 1,000 1,600 1,800 2,000 2,200 Your uncle is fairly wealthy and very reliable. You currently have $10,000 in a savings account paying an annual interest rate of 6%. Attempt 1/5 for 10 pts. Part 1 Create...
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years....
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years. Your favorite uncle has promised to help you with your school expenses by giving you the following amounts for Christmas: Year 2020 2021 2022 2023 2024 Cash flow 1,000 1,600 1,800 2,000 2,200 Your uncle is fairly wealthy and very reliable. You currently have $10,000 in a savings account paying an annual interest rate of 6%. Attempt 1/5 for 10 pts. Part 1 Create...
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years....
Intro It is the end of 2019. You plan to complete a Ph.D. in 5 years. Your favorite uncle has promised to help you with your school expenses by giving you the following amounts for Christmas: Year 2020 2021 2022 2023 2024 Cash flow 1,000 1,600 1,800 2,000 2,200 Your uncle is fairly wealthy and very reliable. You currently have $10,000 in a savings account paying an annual interest rate of 10%. Attempt 1/5 for 10 pts. Part 1 Create...
how do you write a bubble sort function in python that sorts a linked list and...
how do you write a bubble sort function in python that sorts a linked list and if any duplicate are placed bext ti each other?
Discussion Question: Recall three on-the-job incidents in which you had difficulty listening effectively. For each incident,...
Discussion Question: Recall three on-the-job incidents in which you had difficulty listening effectively. For each incident, describe which of the following factors interfered with your listening effectiveness: Environmental barriers Physiological barriers Psychological factors Develop a list of ways you could overcome the greatest barriers that prevent you from listening more effectively Write in your own word. No copy/paste please.
Complete all of your lesson materials and assigned readings. Make sure that you are focusing on:...
Complete all of your lesson materials and assigned readings. Make sure that you are focusing on: Communication techniques that can be used to promote safety within the healthcare facility. How communication can assist in providing optimal patient care. You should be using complete sentences to answer the questions. Ensure that you are using correct grammar. In addition, support your answers using your textbook, course materials, credible internet resources, and scholarly journals. SkyScape is a great suggestion for assistance in completion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT