Using the programming language of your choice, implement the Binary Search algorithm for a target value = 9 on the Array A: [9, 11, 70, 25, 20, 0, 36, 24]. What is the primary condition to implement a Binary Search Algorithm? Explain the growth rate of the algorithm

Answers

Answer 1

Answer:

myArray  = [9, 11, 70, 25, 20, 0, 36, 24]

myvalue = 20

def binary_search(mylist, value):

   sorted(mylist)  

   mid = mylist[round(len(mylist) / 2)]

   if value == mid:

       return mylist.index(mid)

   elif value < mid:

       for index, s_one in enumerate(mylist[ : (mylist.index(mid))]):

           if s_one == value:

               return index

   elif value < mid:

       for index, s_two in enumerate(mylist[(mylist.index(mid)) : ]):

           if s_two == value:

               return index

   else:

       return "searched value not in list/array"

result = binary_search( myArray, myvalue)

print(f"Index of the searched value {myvalue} is: {result}")

Explanation:

The programming language used above is python. It is used to implement a binary search in a list and finally returns the index of the searched value.


Related Questions

When would it be necessary to edit the information shown on an electronic business card?

Answers

Answer:

It's D dear

Explanation:

Answer:

D. to show only relevant information to people inside your organization

Explanation:

hope this helps :)

what are the advantages of using a folder?​

Answers

Answer:

1. easy access to files

2. better organization

Explanation:

Which is a conditional statement?
If it is sunny, we can play ball.
It is sunny today.
It is sunny or rainy.
It is sunny across the street, but not sunny here.

Answers

i believe it is “if it is sunny, we can play ball”

Answer:

it is 1, I know this because i got 100% on my quiz

Explanation:

If an app asks for a user's age, it may be because the app requires a user to be over a certain age to use some of the services it provides. Write a function called checkAge that takes one parameter of type String. The function should try to convert this parameter into an Int value and then check if the user is over 18 years old. If he/she is old enough, print "Welcome!", otherwise print "Sorry, but you aren't old enough to use our app." If the String parameter cannot be converted into an Int value, print "Sorry, something went wrong. Can you please re-enter your age?" Call the function and pass in userInputAge below as the single parameter. Then call the function and pass in a string that can be converted to an integer.
Go back and update your function to return the age as an integer. Will your function always return a value? Make sure your return type accurately reflects this. Call the function and print the return value.
func checkage(age: String)->Int?
{
if let age_type = Int(age)
{
if age_type > 18 {
return "Welcome!"
}
else if age_type < 18 {
return"Sorry, but you aren't old enough to use our app."
}
}
else {
return "Sorry, something went wrong. Can you please re-enter your age?"
}
return age_type
}

Answers

Answer and Explanation:

Here the programming language swift is being used. There is a slight error in the program shown above:

var userInputAge=9

func checkage(age: String)->int?

{

if let age_type = Int(age)

{

if age_type > 18 {

return "Welcome!"

}

else if age_type < 18 {

return"Sorry, but you aren't old enough to use our app."

}

}

else {

return "Sorry, something went wrong. Can you please re-enter your age?"

}

return age_type

}

The program should be revised :

func checkage(age: int?)->String

{

if let age_type = Int(age)

{

if age_type > 18 {

return "Welcome!"

}

else if age_type < 18 {

return"Sorry, but you aren't old enough to use our app."

}

}

else {

return "Sorry, something went wrong. Can you please re-enter your age?"

}

return age_type

}

We call the functions :

checkage(userInputAge)

checkage("15")

Note: we revised the program for errors in the first line of the code where the int optional parameter(int?) was supposed to be used instead of the String parameter in the function. We then called the function using the userInputAge variable defined as the parameter and then we now also used a String as the parameter for calling the function the second time.

Answer:

def checkage(age: "String")->int:

   if age >= 18:

       return "Welcome!"

   return "Sorry, but you aren't old enough to use our app."

   

for _ in iter(list,0):

   myage = int(input("Please enter your age: "))

   if myage is int(myage):

       result = checkage(myage)

       print(result)

       break

   print("Sorry, something went wrong. Enter integer value as age.")

   

Explanation:

The python code above lets the user input the age value for continuous comparison. If the age is an integer, it checks to know if the age is greater than 18 or not. If yes, it returns "Welcome!" else "Sorry, but you aren't old enough to use our app". But if the age is not an integer, it displays the message "Sorry, something went wrong. Can you please re-enter your age?" then prompts the user again for the age.

Note: the ': "String" ' and "->int" is for documentation purposes. They are used to describe the type of parameters (for later) and return value (the former).

what is the norm that psychoanalysis of Freud deviates?

Answers

Answer:

Freud deviates from the norm of current mainstream psychology which has more or less lost its way - if it ever had one - under the influence of the same pressures which produce our dysfunctional politics and our troubled societies at large (technological change, globalisation, and so on).

Explanation:

Creds to Quora

Other Questions
8 more than the quotient of a number and 2 is 14. find the number Which of the following might you use a thesaurus to find?A synonym for the word sadA description of the English languageThe definition of the word theatreThe definition of the word industrious The boy is tall. The girls are smart. The boy is short. The girls are pretty. El chico es bajo. Las chicas son bonitas. El chico es alto. Las chicas son inteligentes. Drag & Drop! C How is mass related to acceleration? A) There is no relationship. B) The greater the mass of an object, the greater its acceleration. C) The greater the mass of an object, the lesser the acceleration. D) It is always different. IF CORRECT I WILL GIVE BRAINLIEST!!! which plate is the eurasian plate? Shawn goes to the library to pick up a copy of a common tax form used file taxes. Which of the following is MOST likely the form that Shawn got?1099W-217761040 what did the Magna Carta influence governments in the colonies? (04.04 LC)On a coordinate grid, point T is at (2,-4) and point Sis at (2,6). The distance (in units) between points T and Sisnumbers, such as 2.) (5 points)(Input only whole 1Robert climbed 775 steps in 12 minutes.2How many steps did he average per minute? Your brother who is in the third year in another school has written to confide in you that he is about to stop schooling and go in to business. Write to him advising him against his decision Verify that the function satisfies the three hypotheses of Rolle's Theorem on the given interval. Then find all numbers c that satisfy the conclusion of Rolle's Theorem. (Enter your answers as a comma-separated list.)f(x) = 3 48x + 4x2, [5, 7] Help help help help help help help How do you halt an infinite loop?by pressing the Control key and thekey How does the structure of bone allow it to function with the immune system? (PLEASE HELP QUICK THEYRE COMING) Just for some fun during veterans day: 2416(5+3)2 A private not-for-profit entity is working to create a cure for a disease. The charity starts the year with one asset, cash of $700,000. Net assets without donor restrictions are $400,000. Net assets with donor restrictions are $300,000. Of the restricted net assets, $160,000 is to be held and used to buy equipment, $40,000 is to be used for salaries, and the remaining $100,000 must be held permanently. The permanently held amount must be invested with 70 percent of any subsequent income used to cover advertising for fundraising purposes. The rest of the income is unrestricted.During the current year, this health care entity has the following transactions:1. Receives unrestricted cash gifts of $210,000.2. Pays salaries of $80,000, with $20,000 of that amount coming from purpose-restricted donated funds. Of the total salaries, 40 percent is for administrative personnel. The remainder is divided evenly among individuals working on research to cure the disease and individuals employed for fundraising purposes.3. Buys equipment for $300,000 by signing a long-term note for $250,000 and using restricted funds for the remainder. Of this equipment, 80 percent is used in research. The remainder is split evenly between administrative activities and fundraising. The donor of the restricted funds made no stipulation about the reporting of the equipment purchase.4. Collects membership dues of $30,000 in cash. Members receive a reasonable amount of value in exchange for these dues including a monthly newsletter that describes research activities. By the end of the year, 112/112 of this money had been earned.5. Receives $10,000 in cash from a donor. The money must be conveyed to a separate charity doing work on a related disease.6. Receives investment income of $13,000 from the permanently restricted net assets.Pays $2,000 for advertising. The money comes from the income earned in (f).Receives an unrestricted pledge of $100,000 that will be collected in three years. The entity expects to collect the entire amount. The pledge has a present value of $78,000. Related interest (considered contribution revenue) of $5,000 is earned prior to the end of the year.7. Computes depreciation on the equipment bought in (c) as $20,000.8. Spends $93,000 on research supplies that are used up during the year.9. Owes salaries of $5,000 at the end of the year. None of this amount will be paid from restricted net assets. Half of the salaries are for individuals doing fundraising, and half for individuals doing research.10. Receives a donated painting that qualifies as a museum piece being added to the entitys collection of art work that is being preserved and displayed to the public. The entity has a policy that the proceeds from any sold piece will be used to buy replacement art. Officials do not want to record this gift if possible..A. Prepare a statement of financial position for this not-for-profit entity for the end of the current year.B. Prepare a statement of activities for this not-for-profit entity for this year. Which hourly interval had the least rate of change? DUE 10:00 PM HELP ASAP. When Sarah left your house this morning her cell phone was 80% charged and then it started to lose 8% charge for each hour there after write an equation for B in terms of t representing the charge remaining in series battery as percentage t hours after Sara left her house Think about how you can use absolute value notation to express the distance between two points on a coordinate graph. For each pair of points below, find the distance between the given points and express your work using absolute value symbols. Show your work.(4, 9) and ( 5, 9)(7, 1) and (7, 4)(0, 9) and (0, 7)(4, 8) and (10, 8) Answer the best you can please :)