Question

In: Computer Science

problem 1 Write a function to search an item named searchkey in an array of 20...

problem 1 Write a function to search an item named searchkey in an array of 20 elements. Assumed the array has all integer value and also the search item is an integer.

problem 2 Now consider the array is sorted. Modify the above script (from question 1) in a way so that dont have to search the item in every position of the array( not looking 20 times considering worst case scenario).

thanks in advance


use html thank you

simple as possible

Solutions

Expert Solution

1)

NOTE: I only initialized four array elements but you can initialize as many as you want just change loop and add elements in the array.

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function search(){
var a =[12,31,123,45];

var searchkey=31;

for(var i=0;i<20;i++){

if(a[i]==searchkey){

document.write("item found");
break;
}
}
}
search();
</script>
</head>
<body>
</body>
</html>

2)

NOTE: You should study more about linear and binary search. The second part uses binary search algorithm.

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function binary_Search(items, value){
var firstIndex = 0,
lastIndex = items.length - 1,
middleIndex = Math.floor((lastIndex + firstIndex)/2);

while(firstIndex < lastIndex)
{if(middleIndex==value){
document.write("item found");
break;
}
else if (value < items[middleIndex])
{
lastIndex = middleIndex - 1;
}
else if (value > items[middleIndex])
{
firstIndex = middleIndex + 1;
}
middleIndex = Math.floor((lastIndex + firstIndex)/2);
}

return
}
var items = [1, 2, 3, 4, 5, 7, 8, 9];
  
(binary_Search(items, 1));


</script>
</head>
<body>
</body>
</html>


Related Solutions

2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Write a member function named deleteItemAtIndex(). You will need to search till you get to the...
Write a member function named deleteItemAtIndex(). You will need to search till you get to the index'thed node in the list. But at that point you should remove the node (and don't forget to delete it, to free up its memory). If the asked for index does not exist, as usual you should thow a LinkedListItemNotFound exception. Here is the main and LinkedList cpp - https://paste.ofcode.org/37gZg5p7fiR243AD4sD5ttU Any help is appreciated, I don't have much time left
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT