Question

In: Computer Science

Write a program including  binary-search and merge-sort in Python. it has to output the following: NeArr range(0,...

Write a program including  binary-search and merge-sort in Python.

it has to output the following:

NeArr range(0, 20)                                                                                                          

result for searching 6 True                                                                                                 

result for searching 16 False  

                                                                                                                          

for-loop's function                                                                                                         

arr range(0, 15)                                                                                                            

for-loop's func result for searching 6 1                                                                                    

result for searching 16 0

it has to be a simple code and please!! put the whole code together.

Solutions

Expert Solution

Question is not clear .

i am writing with my understanding.

please leave a comment if you need anything else.

COde:

def merge(arr, l, m, r):
    n1 = m - l + 1
    n2 = r- m

    L = [0] * (n1)
    R = [0] * (n2)

    # Copy data to temp arrays L[] and R[]
    for i in range(0 , n1):
        L[i] = arr[l + i]

    for j in range(0 , n2):
        R[j] = arr[m + 1 + j]

    i = 0     # Initial index of first subarray
    j = 0     # Initial index of second subarray
    k = l     # Initial index of merged subarray

    while i < n1 and j < n2 :
        if L[i] <= R[j]:
            arr[k] = L[i]
            i += 1
        else:
            arr[k] = R[j]
            j += 1
        k += 1

    while i < n1:
        arr[k] = L[i]
        i += 1
        k += 1

    while j < n2:
        arr[k] = R[j]
        j += 1
        k += 1

def mergeSort(arr,l,r):
    if l < r:

        # Same as (l+r)//2, but avoids overflow for
        # large l and h
        m = (l+(r-1))//2

        # Sort first and second halves
        mergeSort(arr, l, m)
        mergeSort(arr, m+1, r)
        merge(arr, l, m, r)
def binarySearch (arr, l, r, x):

    # Check base case
    if r >= l:

        mid = l + (r - l) // 2

        # If element is present at the middle itself
        if arr[mid] == x:
            return 1

        # If element is smaller than mid, then it
        # can only be present in left subarray
        elif arr[mid] > x:
            return binarySearch(arr, l, mid-1, x)

        # Else the element can only be present
        # in right subarray
        else:
            return binarySearch(arr, mid + 1, r, x)

    else:
        # Element is not present in the array
        return 0
Nearr=[range(0,15)]
mergeSort(Nearr,0,len(Nearr)-1)
r=binarySearch(Nearr,0,len(Nearr),6)
print(bool(r))
r=binarySearch(Nearr,0,len(Nearr),16)
print(bool(r))

This code first sort the array using mergesort and searches the array using binary search.

OUTPUT:


Related Solutions

ASAP Use python please!! Write a program including   binary-search and merge-sort in Python. You also need to  modify...
ASAP Use python please!! 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.  
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.  
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...
write a python program that will search through a range of launch angles (this will require...
write a python program that will search through a range of launch angles (this will require a loop statement) the target is 500 meters away and the projectile was launched at 100m/s, the initial height of the projectile can be between 2 and 30 meters the print statement will state the initial launch angle required to hit the target from the projectile height. ***explain each step or a rating will not be given***
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a...
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a selection sort to organize an array, then find the value using a binary search. Input the array, and a value to find. Output the sorted array and the value if contained in the array. /* * File: main.cpp * Author: * Created on: * Purpose: Binary Search */ //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Random Functions #include <ctime> //Time Library using namespace...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
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...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT