0% found this document useful (0 votes)
19 views1 page

Chapter II Searching: Exercise 1

Sequential search is a linear search that starts at the beginning of an array and compares each element to the search value until a match is found or all elements are checked. A recursive version would call itself, passing a subset of the array. Binary search works on a sorted array by comparing the search value to the midpoint element and recursively searching either the upper or lower half. It returns the index of a match or -1 if not found. A linked list version of sequential search would traverse the list node by node until a match or end is reached.

Uploaded by

Romeo Demanou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Chapter II Searching: Exercise 1

Sequential search is a linear search that starts at the beginning of an array and compares each element to the search value until a match is found or all elements are checked. A recursive version would call itself, passing a subset of the array. Binary search works on a sorted array by comparing the search value to the midpoint element and recursively searching either the upper or lower half. It returns the index of a match or -1 if not found. A linked list version of sequential search would traverse the list node by node until a match or end is reached.

Uploaded by

Romeo Demanou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter II Searching Exercise 1

1. Give a recursive version of the function


Organizing and retrieving information is at the sequentialSearch.
heart of most computer applications, and searching is 2. Propose a version of sequentialSearch that
surely the most frequently performed of all computing uses a linked list as data structure. (Provide for this
tasks. Search can be viewed abstractly as a process to question the data structure for linked list).
determine if an element with a particular value is a
member of a particular set. The more common view of 2. Binary search
searching is an attempt to find the record within a When the records you are searching through are
collection of records that has a particular key value, or sorted into order, you can perform a more efficient
those records in a collection whose key values meet search than the sequential search to find a value. This
some criterion such as falling within a range of values. search is called a binary search. A binary search
We can define searching formally as follows. algorithm is only applied on an ordered set (preferably
Suppose k1, k2 … kn are distinct keys, and that we in ascending order). The first steps in the algorithm are
have a collection L of n records of the form (k1, I1), to set the lower and upper bounds of the search.
(k2, I2) … (kn, In), where Ij is information
At the beginning of the search, this means the lower
associated with key kj for 1 ≤ j ≤ n. Given a
and upper bounds of the array. Then, we calculate the
particular key value K, the search problem is to locate
midpoint of the array by adding the lower and upper
the record (kj, Ij) in L such that kj = K (if one bounds together and dividing by 2. The array element
exists). Searching is a systematic method for locating stored at this position is compared to the searched-for
the record (or records) with key value kj = K. value. If they are the same, the value has been found and
A successful search is one in which a record with the algorithm stops. If the searched-for value is less than
key kj = K is found. An unsuccessful search is one in the midpoint value, a new upper bound is calculated by
which no record with kj = K is found (and no such subtracting 1 from the midpoint. Otherwise, if the
record exists). searched-for value is greater than the midpoint value, a
An exact-match query is a search for the record new lower bound is calculated by adding 1 to the
whose key value matches a specified key value. A midpoint. The algorithm iterates until the lower bound
range query is a search for all records whose key value equals the upper bound, which indicates the array has
falls within a specified range of key values. been completely searched. If this occurs, a -1 is
This chapter presents two common searching returned, indicating that no element in the array holds
algorithm: sequential and binary search. We only use the value being searched for.
array as data structure.
public static int binarySearch
1. Sequential search (int value){
The most obvious type of search is to begin at the int ub, lb, mid;
//ub=upper bound, lb=lower bound
beginning of a set of records and move through each
lb = 0;
record until you find the record you are looking for or ub = A.length - 1;
you come to the end of the records. This is called a while (lb <= ub) {
sequential search or linear search. mid = (lb + ub) / 2;
A sequential search (also called a linear search) is if(A[mid]==value){
return mid;
very easy to implement. Start at the beginning of the }else
array and compare each accessed array element to the if(value < A[mid]){
value you’re searching for. If you find a match, the ub = mid - 1;
search is over. If you get to the end of the array without }else{
generating a match, then the value is not in the array. lb = mid + 1;
}
}
Here is a function that performs a sequential search: return -1;
public
int static boolean }
sequentialSearch(int[]A, int value){
for (int i=0; i<A.length; i++){
if (value == A[i]){
Exercise 2
return true
true; 1. Propose a recursive version of binarySearch
} function. Hint: you can use the following signature
} binarySearch(int[]A,int lb,int ub,int val)
return false
false; - return -1 if lb>ub or if the value is not found
}
- return mid if the value is found

Searching
1

You might also like