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
yKpoI14uk [10]
3 years ago
13

Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorit

hm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a count of the number of comparisons it makes. Display these values on the screen.
Computers and Technology
1 answer:
PIT_PIT [208]3 years ago
4 0

Answer:

  1. def sequenceSearch(myList, target):
  2.    count = 0
  3.    for x in myList:
  4.        count += 1
  5.        if(x == target):
  6.            return count  
  7.    
  8.    return -1
  9. def binarySearch(myList, target):
  10.    minIndex = 0
  11.    maxIndex = len(myList) - 1
  12.    count = 0
  13.    while(maxIndex >= minIndex):
  14.        middleIndex = minIndex + (maxIndex - minIndex)//2
  15.        count += 1
  16.        if(target < myList[middleIndex]):
  17.            maxIndex = middleIndex - 1
  18.        elif(target > myList[middleIndex]):
  19.            minIndex = middleIndex + 1
  20.        else:
  21.            return count  
  22.    return -1
  23. myList = [4, 7, 9, 12, 15, 19, 21, 40, 57, 80, 91, 111, 123, 127, 145, 167, 178, 180, 210, 222]
  24. print(sequenceSearch(myList, 210))
  25. print(binarySearch(myList, 210))

Explanation:

Firstly, create a sequential search function that will take two parameter, list and target number (Line 1). In the function, create a counter variable, count and initialize it with zero (Line 2). Next create a for loop to start the sequential search from the first element of list and keep tracking of count (Line 4 -7). If target is found in the list, return the count (Line 7) otherwise return -1.

Next, create another function for binary search that will take the same input parameters as sequential search function (Line 11). In the binarySearch function, use the similar counter variable, count to track the number of searching (Line 14).  Next, use a while loop to implement the binary search algorithm that will keep comparing target against the list element in middle index (Line 20 - 25). If target is smaller than middle value adjust the maxIndex by having middleIndex -1. If target bigger than the middle value, set the minIndex to middleIndex - 1. Otherwise it will return the count. If the target is not found, return -1 (Line 27).

In the main program, create a sample list with 20 elements and then test the sequenceSearch and binarySearch function ny using the sample list and target = 210 as arguments. We shall get the output: 19 4

You might be interested in
The windows desktop contains a start button in its lower-right corner, which can be used to start the computer.
WITCHER [35]
If you are referring to the actual desktop interface inside windows, it goes down to what version you would have. But to my knowledge, there is no start button in the lower right corner. 
6 0
4 years ago
Purpose of this project is to increase your understanding of data, address, memory contents, and strings. You will be expected t
STALIN [3.7K]

Answer:

See explaination for the details

Explanation:

#Starting point for code/programm

main:

la $a0,newLine #Print a new line

li $v0,4

syscall

# Find the number of occurence of a string in the given sentence

la $a0,prompt1 # Prompt the user to enter the first string.

li $v0,4

syscall

li $v0, 8 # Service 8 = read input string

la $a0, fword

li $a1, 9

syscall

la $a0,prompt2 # Prompt the user to enter the second string.

li $v0,4

syscall

li $v0, 8 # Service 8 = read input string

la $a0, sword

li $a1, 9

syscall

# process first word

li $t4,0 # Intialize the couter to 0

la $t0,sstatement # Store the statement into $t0

nstart1: la $t1,fword # Store the search word into $t1

loop1: # loop1 finds the number of occurences

# of input word in the given statment

lb $t2,($t0) # Load the starting address(character) of

# sstatement into $t2

lb $t3,($t1) # Load the starting address of input word

# into $t3

beq $t3,'\n',inc_counter1

beqz $t3,inc_counter1 # If $t3 is null , exit loop and print output

beqz $t2,print_output1 # If $t2 is null , exit loop and print output

move $a0,$t2 # Convert $t2 to lower, if it is upper case

jal convert2lower

move $t2,$v0 # Store the return($v0) value into $t2

move $a0,$t3 # Convert $t3 to lower, if it is upper case

jal convert2lower

move $t3,$v0 # Store the return($v0) value into $t3

bne $t2,$t3,next_char1 # If both characters are not matched current

# character in the string, go to next character

addiu $t0,$t0,1 # otherwise, increment both indexes

addiu $t1,$t1,1

j loop1 # go to starting of the loop

next_char1:

la $t5,fword

bne $t5,$t1,nstart1

la $t1,fword # Store the input word into $t1

addiu $t0,$t0,1 # Increment the index to goto next character

j loop1 # go to starting of the loop

inc_counter1:

addi $t4,$t4,1 # Increment the frequency counter by 1

la $t1,fword # Store input word into $t1

j loop1 # go to starting of the loop

print_output1:

la $t0,fword

L1:

lb $a0,($t0)

beq $a0,'\n',exL1

jal convert2upper

move $a0,$v0

li $v0,11

syscall

addiu $t0,$t0,1

j L1

exL1:

la $a0,colon

li $v0,4

syscall

la $a0, dash

li $v0, 4

syscall

move $a0,$t4

li $v0,1

syscall # print new line

la $a0,newLine

li $v0,4

syscall

# process second word

li $t4,0 # Intialize the couter to 0

la $t0,sstatement # Store the statement into $t0

nstart2: la $t1,sword # Store the search word into $t1

loop2: # loop1 finds the number of occurences

# of input word in the given statment

lb $t2,($t0) # Load the starting address(character) of

# sstatement into $t2

lb $t3,($t1) # Load the starting address of input word

# into $t3

beq $t3,'\n',inc_counter2

beqz $t3,inc_counter2 # If $t3 is null , exit loop and print output

beqz $t2,print_output2 # If $t2 is null , exit loop and print output

move $a0,$t2 # Convert $t2 to lower, if it is upper case

jal convert2lower

move $t2,$v0 # Store the return($v0) value into $t2

move $a0,$t3 # Convert $t3 to lower, if it is upper case

jal convert2lower

move $t3,$v0 # Store the return($v0) value into $t3

bne $t2,$t3,next_char2 # If both characters are not matched current

# character in the string, go to next character

addiu $t0,$t0,1 # otherwise, increment both indexes

addiu $t1,$t1,1

j loop2 # go to starting of the loop

next_char2:

la $t5,sword

bne $t5,$t1,nstart2

la $t1,sword # Store the input word into $t1

addiu $t0,$t0,1 # Increment the index to goto next character

j loop2 # go to starting of the loop

inc_counter2:

addi $t4,$t4,1 # Increment the frequency counter by 1

la $t1,sword # Store input word into $t1

j loop2 # go to starting of the loop

print_output2:

la $t0,sword

L2:

lb $a0,($t0)

beq $a0,'\n',exL2

jal convert2upper

move $a0,$v0

li $v0,11

syscall

addiu $t0,$t0,1

j L2

exL2:

la $a0,colon

li $v0,4

syscall

la $a0, dash2

li $v0, 4

syscall

move $a0,$t4

li $v0,1

syscall

exit:

# Otherwise, end the program

li $v0, 10 # Service 10 = exit or end program

syscall

############################ subroutine - convert2lower #################################

convert2lower: # Converts a character(stored in $a0) to

# its lower case, if it is upper case

# and store the result(lower case) in $v0

move $v0,$a0

blt $a0,'A',return

bgt $a0,'Z',return

subi $v0,$a0,-32

return: jr $ra # Return the converted(lower case) character

############################## subroutine - convert2upper ##################################

convert2upper: # Converts a character(stored in $a0) to

# its upper case, if it is lower case

# and store the result(upper case) in $v0

move $v0,$a0

blt $a0,'a',return2

bgt $a0,'z',return2

addiu $v0,$a0,-32

return2: jr $ra # Return the converted(lower case) character

4 0
3 years ago
What is the approximate area of the figure? A. 129 square inches B. 113 square inches C. 110 square inches D. 44 square inches
Mice21 [21]
Make a new question showing the figure.
7 0
3 years ago
What is the basic unit for storing data in exel
vaieri [72.5K]
<span>the basic unit for storing data in exel is </span><span>The intersection point between a column and a row is a small rectangular box known as a cell. A cell is the basic unit for storing data in the spreadsheet. Because an Excel spreadsheet contains thousands of these cells, each is given a cell reference or address to identify it.</span>
7 0
4 years ago
Assume that the int variables i, j and n have been declared, and n has been initialized. Write code that causes a "triangle" of
makvit [3.9K]

Answer:

The code that will cause the triangle of  asterisks is given by;

for (i=1; i<=n; i++){

for (j=1; j<=i; j++)

cout << "*";

cout << "\n";

}

Explanation:

6 0
4 years ago
Other questions:
  • What does ACCU stand for?
    13·2 answers
  • Fill in the function shopSmart(orders,shops) in shopSmart.py, which takes an orderList (like the kind passed in to FruitShop.get
    6·1 answer
  • Write an if-else statement with multiple branches. If givenYear is 2101 or greater, print "Distant future" (without quotes). Els
    15·2 answers
  • Which set of technologies that support knowledge management is characterized by mashups, blogs, social networks, and wikis? a. X
    15·1 answer
  • To generate a report with exact results based on specific criteria it is best to base the report on a(n) ____________________ cr
    6·1 answer
  • At a minimum, your vehicle insurance policy must provide coverage of
    10·1 answer
  • To delete a record, tap or click the record selector for the record, and then press the ____ key(s).
    12·1 answer
  • Complete the code to finish this program to analyze the inventory for a store that sells purses and backpacks.
    7·1 answer
  • Python program
    9·1 answer
  • How to get an object from a container in java.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!