Question

In: Computer Science

Design a program which uses functions to sort a list and perform a binary search. Your...

Design a program which uses functions to sort a list and perform a binary search. Your program should:

Iinitialize an unsorted list (using the list provided)

Display the unsorted list

Sort the list

Display the sorted list.

Set up a loop to ask the user for a name, perform a binary search, and then report if the name is in the list. Use a sentinel value to end the loop.

Do not use the Python built in sort function to sort the list, instead write one or more functions to implement either the bubble sort or insertion sort (you choose the one you want to implement). The algorithms for the bubble sort and insertion sort are provided in the Word document attachment.

Implement the binary search algorithm as a function. Algorithms are provided in the Word document attachment for an iterative version or recursive version of the binary search. You choose the one you want to implement.

Use this test list: [ 'Paul', 'Aaron', 'Jacob', 'James', 'Bill', 'Sara', 'Cathy', 'Barbara', 'Amy', 'Jill' ]

Turn in your source code with IPO comments at the top of the file

A screen shot showing the results of your testing.

Solutions

Expert Solution

def binarySearch(arr, lower, upper, target):
    if upper >= lower:
        mid = lower + ((upper - lower) // 2)
        if arr[mid] == target:
            return mid
        elif arr[mid] > target:
            return binarySearch(arr, lower, mid - 1, target)
        else:
            return binarySearch(arr, mid + 1, upper, target)
    return -1


def bubbleSort(list):
    needNextPass = True
    k = 1
    while k < len(list) and needNextPass:
        # List may be sorted and next pass not needed
        needNextPass = False
        for i in range(len(list) - k):
            if list[i] > list[i + 1]:
                # swap list[i] with list[i + 1]
                temp = list[i]
                list[i] = list[i + 1]
                list[i + 1] = temp
                needNextPass = True  # Next pass still needed

# A test method
def main():
    list = [ 'Paul', 'Aaron', 'Jacob', 'James', 'Bill', 'Sara', 'Cathy', 'Barbara', 'Amy', 'Jill' ]
    bubbleSort(list)
    print("Sorted list: ")
    for v in list:
        print(v, end="\t")
    print()

    index = binarySearch(list,0,len(list)-1,'Bill')
    if(index == -1):
        print("Targer not found")
    else:
        print("Target found at index",index)

main()


Related Solutions

Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a)...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a) Suppose we are performing a binary search on a sorted list called numbers initialized as follows: #index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 numbers = [-2, 0, 1, 7, 9, 16, 19, 28, 31, 40, 52, 68, 85, 99] #search for the value 5 index = binary_search(numbers, 5) Write the indexes of the elements that would...
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted...
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted and use your variable names and testArrays.  
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT