“while” loop:
A while loop is a control flow statement whose basic purpose is to execute the block of statements repeatedly as long as the conditional statement is true.
- If the condition is false then it does not execute the block of statement.
Insertion sort
The algorithm is basically designed to organize and sort the array or list of elements in specific manner, that enables the system and user to get the final sorted array or a list of elements.
Generally, the insertion sort technique divides the list into two parts: sorted or unsorted list.
In the sorted list, the method assumes that one element of the list is already sorted and then it performs the operation on the unsorted list by inserting elements in its appropriate position in the sorted list.
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- Split - Filter - Sort romeo = 'But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief Split the long string romeo into a list of words using the split function. For each word, check to see if the word is already in a list. If the word is not in the list, add it to the list. When the program completes, sort and print the resulting words in alphabetical order. The result will be like this: ['Arise', 'But', 'It', 'Juliet', "Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', "'ight', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']arrow_forwardFix the code below properly with no errors: Module.py #Defination to sort the list def sort(listNum): sortedList = [] #While loop will run until the listNum don't get null while(len(listNum) != 0 ): #Set the min as first element in list min = listNum[0] #iterate over the list to compare every element with num for ele in listNum: #If element is less than min if ele < min: #Then set min as element min = ele #append the sorted element in list sortedList.append(min) #Remove the sorted element from the list listNum.remove(min) return sortedList #Function to find the sum of all elements in list def SumOfList(listNum): #Set the sum as zero sum =0 #Iterate over the list to get every element for ele in listNum: #Keep adding the element to the sum variable sum = sum + ele #returnt the sum return sum…arrow_forward# This function takes a list of points and returns a new list of points, # beginning with the first point and then at each stage, moving to the closest point that has not yet been visited.# Note: the function should return a new list, not modify the existing list.# Hint: use a while loop to repeat while the list of remaining points is not emptyarrow_forward
- True/False 9. The add method can be used to add an item to the end of a list.arrow_forwardPython: 2048 games Board = list[list[str]]# Checks whether a given board has any# possible move left. If no more moves,# return True. Otherwise return False.def isGameOver(board: Board) -> bool:return False# Returns a tuple (changed, new_board)# where:# changed - a boolean indicating if# the board has changed.# new_board - the board after the user# presses the 'Up' key.def doKeyUp(board: Board) -> tuple[bool, Board]:return False, board# Returns a tuple (changed, new_board)# where:# changed - a boolean indicating if# the board has changed.# new_board - the board after the user# presses the 'Down' key.def doKeyDown(board: Board) -> tuple[bool, Board]:return False, board# Returns a tuple (changed, new_board)# where:# changed - a boolean indicating if# the board has changed.# new_board - the board after the user# presses the 'Left' key.def doKeyLeft(board: Board) -> tuple[bool, Board]:return False, board# Returns a tuple (changed, new_board)# where:# changed - a boolean…arrow_forwardpython help Q5: Compose Write the procedure composed, which takes in procedures f and g and outputs a new procedure. This new procedure takes in a number x and outputs the result of calling f on g of x. (define (composed f g) 'YOUR-CODE-HERE ) Q6: Remove Implement a procedure remove that takes in a list and returns a new list with all instances of item removed from lst. You may assume the list will only consist of numbers and will not have nested lists. Hint: You might find the filter procedure useful. (define (remove item lst) 'YOUR-CODE-HERE ) ;;; Tests (remove 3 nil) ; expect () (remove 3 '(1 3 5)) ; expect (1 5) (remove 5 '(5 3 5 5 1 4 5 4)) ; expect (3 1 4 4)arrow_forward
- List operations: Add something to the end of a list, remove something from the end of a list, add something/remove something from the middle of the list, get/set values by indexing into the list. How long does each of these operations take for each kind of list? Find the size of a list, check if it is empty Repeatedly remove the last value in a list until the list is empty Print out each element of the list Swap every pair of values in the list Which data structure is best for • random access • insertion/removal at a cursor? when a cursor needs to move forward and backward? • insertion/removal at the front? • insertion removal at the end?arrow_forwardThis chapter's bubble sort method is less efficient than it could be. If a pass through the list is done without swapping any components, the list is sorted and there is no reason to continue. Change this algorithm to terminate as soon as it recognises that the list is sorted. A break statement should not be used.This chapter's bubble sort method is less efficient than it could be. If a pass through the list is done without swapping any components, the list is sorted and there is no reason to continue. Change this algorithm to terminate as soon as it recognises that the list is sorted. A break statement should not be used.This chapter's bubble sort method is less efficient than it could be. If a pass through the list is done without swapping any components, the list is sorted and there is no reason to continue. Change this algorithm to terminate as soon as it recognises that the list is sorted. A break statement should not be used.This chapter's bubble sort method is less efficient…arrow_forward**Cenaage Python** Question: A sequential search of a sorted list can halt when the target is less than a given element in the list. Modify the program to stop when the target becomes less than the current value being compared. In a sorted list, this would indicate that the target is not in the list and searching the remaining values is unnecessary. CODE: def sequentialSearch(target, lyst): """Returns the position of the target item if found, or -1 otherwise. The lyst is assumed to be sorted in ascending order.""" position = 0 while position < len(lyst): if target == lyst[position]: return position position += 1 return -1 def main(): """Tests with three lists.""" print(sequentialSearch(3, [0, 1, 2, 3, 4])) print(sequentialSearch(3, [0, 1, 2])) # Should stop at second position. print(sequentialSearch(3, [0, 4, 5, 6])) if __name__ == "__main__": main()arrow_forward
- Several of the programming exercises in this section ask you to find the mean and median value in a list. Recall that the mean is the arithmetic average and that that median is the middle value in an ordered list. When a list contains an odd number of values, the median is the value in the middle position. When a list contains an even number of values, the median is the mean of the values in the two middle positions. Chapter 2 described the remainder operator which you can use to determine whether a number is even or odd. When a number is divided by 2, the remainder is 0 if the number is even. Design an application that accepts 10 numbers and displays them in descending order.arrow_forwardWrite the following function that returns true if the list is already sorted in increasing order:def isSorted(lst):Write a test program that prompts the user to enter a list and displays whether the list is sorted or not.arrow_forwardcheck_equivalency(tokens): Takes a list as input. Returns True if the list contains three elements, the second element is an equals sign ('='), and the first and third elements are equal to each other. Returns False otherwise.arrow_forward
- 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