1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vaselesa [24]
3 years ago
9

Suppose that a list contains integers that are in order of largest to smallest and an integer can appear repeatedly in this list

.
1) Devise an algorithm that locates all occurrences of an integer x in the list.
2) Estimate the number of comparisons used.
You must show/explain your work.
Computers and Technology
1 answer:
salantis [7]3 years ago
6 0

Answer:

(a) The algorithm is as follows:

count = 0

for i = 1 to n:

  if x = xi:

       count++

print(count)

(b) n comparisons

Explanation:

Solving (a):

Assume the integer to locate is x and the elements of the list are: x_1, x_2, x_3, x_4..... x_n

Such that: x_1 \ge x_2 \ge x_3 ...... \ge x_n

The algorithm is as follows:

<em>count = 0</em>

<em>for i = 1 to n:</em>

<em>   if x = xi:</em>

<em>        count++</em>

<em>print(count)</em>

<em />

The above iterates through the count of the list (i.e. n) and makes comparison with each element of the list (i.e. element 1 to element n).

When a match is found, the count variable is incremented by 1 and printed at the end of the loop

Furthermore:

If there are 3 elements in the list, the algorithm makes 3 comparisons.

It makes 10 comparisons if there are 10 elements in the list.

<em>So: it makes n elements if there are n elements in the list</em>

You might be interested in
Which network is the predecessor of the Internet?
disa [49]
The predecessor of the internet is ARPANET better known as Advanced Research Project Agency
4 0
3 years ago
Explain with a few sentences and using the terms sequencing, selections and loops how they
Sever21 [200]

Answer:

here is your answer

Explanation:

voting is electing people to be ruler

mar me as brainliest

5 0
4 years ago
Frances is rearranging her furniture. (1) Also, she is moving the heavy oak bookcase to the back bedroom. (2) She has decided to
lozanna [386]

The first sentence that seems out of the logical order is the very first sentence, i.e, Also, she is moving the heavy oak bookcase to the bedroom.

A.  1

<u>Explanation:</u>

The fact that the sentence uses words like "she" and "also" leads to this conclusion. The logical order should move in a forward direction with the sentences forming a logical sequence.

The first sentence's structure is adding details to the actions which are already in progress and hence it should not be the first sentence. It should appear after the process of moving furniture has been started and further details are being added to it.

3 0
3 years ago
Read 2 more answers
Choose the word that best completes this sentence. You should post safe load _________ for each floor and keep aisles and passag
erica [24]
You should post safe load limits for
3 0
3 years ago
Read 2 more answers
create your own min function that finds the minimum element in a list and use it in a separate function
schepotkina [342]

Answer:

def minfunction(mylist):

   min = mylist[0]

   for i in mylist:

       if i < min:

           min = i

   print("The minimum is: "+str(min))

   

def anotherfunction():

   mylist = []

   n = int(input("Length of list: "))

   for i in range(n):

       listelement = int(input(": "))

       mylist.append(listelement)

   minfunction(mylist)

   

anotherfunction()

Explanation:

This solution is implemented in Python

This defines the min function

def minfunction(mylist):

This initializes the minimum element to the first index element

   min = mylist[0]

This iterates through the list

   for i in mylist:

This checks for the minimum

       if i < min:

... and assigns the minimum to variable min

           min = i

This prints the minimum element

   print("The minimum is: "+str(min))

   

This defines a separate function. This  separate function is used to input items into the list

def anotherfunction():

This defines an empty list

   mylist = []

This prompts user for length of list

   n = int(input("Length of list: "))

The following iteration inputs elements into the list

<em>    for i in range(n):</em>

<em>        listelement = int(input(": "))</em>

<em>        mylist.append(listelement)</em>

This calls the minimum function

   minfunction(mylist)

   

The main starts here and this calls the separate function

anotherfunction()

5 0
3 years ago
Other questions:
  • Please answers the questions 1-15
    6·1 answer
  • Under extreme programming, ________ and ________ are intimately related parts of the same process
    13·2 answers
  • In this question, we give two implementations for the function: def intersection_list(lst1, lst2) This function is given two lis
    15·1 answer
  • The picture that graphically represents the items you use in Windows is called a/an
    12·2 answers
  • / List the seven basic internal components found in a computer tower.
    5·2 answers
  • Write a program that will find the smallest, largest, and average values in a collection of N numbers. Get the value of N before
    13·1 answer
  • Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the num
    13·1 answer
  • Plymouth Colony was originally founded by a group of people called Pilgrims. Since Plymouth did not have an official charter fro
    15·1 answer
  • Help me to solve please​
    8·2 answers
  • TCP and the User Datagram Protocol (UDP) provide _________ between processes on any two of those hosts. A. address translation B
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!