Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search
Question
Write a program to read and store n + ve numbers in an array and search a number k provided by the user using:
a. Linear Search
b. Binary Search
Solution
Sure, here is a step-by-step guide to write a program that reads and stores positive numbers in an array and performs a linear search and binary search to find a number provided by the user:
-
Start by declaring the necessary variables and arrays. You will need an integer variable
n
to store the number of elements in the array, an integer variablek
to store the number to be searched, and an integer arrayarr
to store the positive numbers. -
Prompt the user to enter the value of
n
and read it from the input. -
Create a loop to read
n
positive numbers from the user and store them in thearr
array. Make sure to validate that the numbers entered are positive. -
Prompt the user to enter the number
k
to be searched and read it from the input. -
Implement the linear search algorithm to find
k
in thearr
array. Start from the first element and compare it withk
. If a match is found, print the index of the element. If the end of the array is reached without finding a match, print a message indicating thatk
is not found. -
Sort the
arr
array in ascending order using any sorting algorithm (e.g., bubble sort, insertion sort, etc.). -
Implement the binary search algorithm to find
k
in the sortedarr
array. Set the lower boundlow
to the first index of the array and the upper boundhigh
to the last index. Repeat the following steps untillow
is less than or equal tohigh
:- Calculate the middle index
mid
as the average oflow
andhigh
. - If the element at index
mid
is equal tok
, print the index and exit the loop. - If the element at index
mid
is greater thank
, sethigh
tomid - 1
. - If the element at index
mid
is less thank
, setlow
tomid + 1
.
- Calculate the middle index
-
If the loop in step 7 completes without finding a match, print a message indicating that
k
is not found in the array.
That's it! You have now implemented a program that reads and stores positive numbers in an array and performs a linear search and binary search to find a number provided by the user.
Similar Questions
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Suppose following numbers are sorted in an array A:32,51,26,84,63,21,11,54Using Binary search find the location of item 26,11 and 99.Answer text
Write the pseudocode of the binary search algorithm and generate the recurrence relation from it
Given an input a[]={5,3,2,1,0}, key=1, what is position of key element ( assume binary search)102Error
What happens if you perform a linear search on an empty array?It will find the target valueIt will return 0It will return -1It will cause an error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.