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

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") # -> []...
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...
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...
Create a scenario or recall a time when you had an educational interaction with a community...
Create a scenario or recall a time when you had an educational interaction with a community group. How receptive to the activity were the participants? Develop a plan for ways that the interaction could have been improved based on health educational principles. Be sure to include age-appropriate, culturally sensitive strategies that would enhance the activity that you described.?
python question A word is a palindrome if it the same read forwards and backwards. We...
python question A word is a palindrome if it the same read forwards and backwards. We will call a word a fuzzy palindrome if it is the same read forwards and backwards, except for possible differences in case. For example, both 'tattarrattat' and 'TaTtArRAttat' are fuzzy palindromes. Define a function is_fuzzy_palindrome that returns True if and only if its argument is a fuzzy palindrome. This method may be useful: S.lower() -> str : Return a copy of the string S...
QUESTION THREE This is a recall level type question that require you identify the various business...
QUESTION THREE This is a recall level type question that require you identify the various business tools available for decision- making. ESSAY-TYPE QUESTION How do business intelligence and business analytics support decision-making?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT