Question

In: Computer Science

write a Linear Search algorithm using sentinel approach? How can the sentinel linear search improve the...

write a Linear Search algorithm using sentinel approach? How can the sentinel linear search improve the performance over the standard linear search?

Solutions

Expert Solution

Linear Search :
The Concept of linear search tells us to compare the search key with the
elements in the array one by one (using a loop) and end the loop as soon as
we get the first copy of the search key in the array.

Now lets take a look at the worst case of linear search in which the search
key that is not present in the array of size N then the Simple Linear Search
will take a total of 2N+1 comparisons

(N comparisons against every element in the search array, the if condition
in for loop, and N+1 comparisons to test the for loop condition,
where we check the index of the array).


//n+1 comparisons for checking whether i < l
for (int i = 0; i < length; i++) {

   //n comparisons of checking if element found
if (array[i] == element) {
return i; // I found the position of the element requested
}
}


Sentinel Linear Search :

Here the idea is to reduce the number of comparisons required to find an
element in a array.

Here we replace the last element of the
array with the search key itself and run a while loop to see if
there exists any element equal to the search key in the array and quit
the loop as soon as we find the search key.

int item = array[N-1];   
// Here item is the search element.
int i = 0;
//only one comparison is required for each iteration
//so in worst case n comparisons are required.
while(array[i]!=item)   
{
i++;   
}
if( (i < N-1))
{
cout << " Item Found @ "<<i;
}
else
{
cout << " Item Not Found";   
}


Related Solutions

Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers)...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers) i := 1 while (i ≤ n and x 6= ai) i := i + 1 if i ≤ n then location:= i else location:= 0 return location Apply the linear search algorithm to search for the number 3 in the list 1, 5, 3, 9. (a) In this application of the algorithm, what is the integer n? (b) What is the initial value...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures....
Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures. 1a. Write a Java method called stringyLinear that performs linear search on an array of String’s, so it is defined as follows. Your code appears in place of the three dots ‘‘⋅⋅⋅’’. Do not write a class, only a method. If a String equal to key appears in keys, then stringyLinear must return an index in keys where it appears. If no String equal...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
write an algorithm and python program using the following information. also can y How much should...
write an algorithm and python program using the following information. also can y How much should I study outside of class?                         Issue: Your fellow students need help. This is their first year in college and they need to determine how many hours they need to study to get good grades. Study Hours Per Week Per Class                    Grade 15                                                           A 12                                                           B 9                                                             C 6                                                             D 0                                                             F Project Specifications: The user enters their full name and the number...
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT