0% found this document useful (0 votes)
42 views4 pages

Computer 5

Chapter 8 discusses sorting and searching algorithms, highlighting foundational sorts like selection, bubble, and insertion sorts, which lead to more advanced methods. It explains searching in arrays, detailing sequential and binary search techniques, with a focus on the sequential search for unordered lists. The chapter also includes pseudocode for implementing a sequential search function that returns the index of the found target or indicates its absence.

Uploaded by

jat302004
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)
42 views4 pages

Computer 5

Chapter 8 discusses sorting and searching algorithms, highlighting foundational sorts like selection, bubble, and insertion sorts, which lead to more advanced methods. It explains searching in arrays, detailing sequential and binary search techniques, with a focus on the sequential search for unordered lists. The chapter also includes pseudocode for implementing a sequential search function that returns the index of the found target or indicates its absence.

Uploaded by

jat302004
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

m

Chapter 8 Arrays 501

Singleton ’s variation , Quickersort , which are considered the best general -


purpose sorts.3
Historically, however, these three sorts are the foundation of improved
and faster sorting methods that you will study in a data structures course .
I he selection sort is the foundation of a sorting method called the heap sort ;
the bubble sort is the foundation for Quicksort and Quickersort ; and the
insertion sort is the foundation of a sorting method called Shell Sort .

8.6 Searching
Another common operation in computer science is searching, which is the
process used to find the location of a target among a list of objects. In the
case of an array, searching means that given a value , we want to find the loca -
tion ( index) of the first element in the array that contains that value. The
search concept is shown in Figure 8- 27 .

Location wanted
(4)

a[ 0] a[ 1] a[ 2) a[3] a[4 ] a[ 5] a[6] a[7] a[8) a[9] a[ 10] a[11]


4 21 36 14 62 91 8 22 7 81 77 10

Target given
( 62 )

FIGURE 8 - 27 Search Concept

The algorithm used to search a list depends to a large extent on the struc-
ture of the list . Since our structure is currently limited to arrays , we will
study
searches that work with arrays . As we study other structures , we will study
different search techniques .
There are two basic searches for arrays: the sequential search and the
binary search . The sequential search can be used to locate an item in any
to be sorted .
arrav. The binary search , on the other hand, requires the list

Sequential Search
The sequential search is used whenever the list is not ordered
. Generally, we
lists lists that are not searched often. In
use the technique only for small or

Gilberg and Behrou / A. Forouzan , Data Structures:


3. For a discussion of Quicksort , see Richard F.
. Pacific Grove , CA: Brooks/Cole , 1998.
A Pseudocode Approach With C
I
502 Section 8.6 Searching

other cases we would first sort the list and then search it using the binary
search discussed later.
In the sequential search, we start searching for the target from the begin-
ning of the list, and we continue until we find the target or until we are sure
that it is not in the list . This gives us two possibilities; either we find it or we
reach the end of the list. Figure 8 - 28 traces the steps to find the value 62. We
.
first check the data at index 0 then 1, 2, and 3 before finding the 62 in the
fifth element ( index 4 ) .
location wanted'
4
index \
a(0] a[1] a[ 2] a[ 31 a[ 4 ] a [ 5 ] a[ 6] a [ 7 ] a(8 ] a[ 9 ] a [ 10 ) a [ 11 ]
4 l
21 36 14 62 91 8 22 7 81 77 10

1 Target given
62 != 4
index ^ < 621
afO] a[ 1) a[ 2] a[3] a[ 4 ] a[ 5] a[ 6] a( 7 ] a[ 8) a[ 9] a[10] a|11 ]
4 | 21 ; 36 14 62 91 8 22 7 81 77 10

2
62 != 21
index
afO) a[l ) a[ 2 ) a[ 3 ] a[ 4 ] a[ 5 ] a (6 ) a[ 7 ] a [ 8 ] a [ 9] a [ 10 ) a [ 11 )
4 21 [ 36» 14 62 91 8 22 7 81 77 10

3
62 != 36
index
a [ 0] a[ 1 ] a[ 2 j a[ 3] a [ 4 ] a[ 5) a [ 6 ] a [ 7 ] 3( 8 ] a[ 9 ] a[ 10] a [ 11 ]
4 21 36 14 62 91 8 22 7 81 77 10

4
62 != 14
index
a[0] a[ 1 ] a[2] a[3T a[ 4 ] a[ 5 ] a[ 6] a[7 ] a|8) a[ 9 j a[ 10) a [ 11 ]
4 21 36 14 62 | 91 8 22 7 81 77 10

62 == 62

FIGURE 8 - 28 Locating Data in Unordered List

But what il the target is not in the list ? Then we have to examine each cle-
ment until we reach the end ol the list . Figure 8 - 29 traces the search fora target
ol 2. W hen we detect the end ol the list, we know that the target does not exist.
Let s write the sequential search function. A search function needs to tell
the calling function two things: Did it find the data it was looking lor? If it
did. what is the index at which the data were found?
But a function can return only one value. For search functions, we use
the ietuin value to designate whether we found the
target or not. To ‘‘ return
the index location where the data were found, we will use call by-address
- .
I he search Junction requires four parameters: the
list we are searching,
the index to the last element in the list, the
target, and the address where the
found element 's index location will he stored. Althougl1 we could write it
without passing the index to the last element, that
would mean the search
Chapter 8 Arrays 503 1
would have to know how many elements are in the list. To make the function
as flexible as possible, therefore, we pass the index of the last data value in
the array. I his is also a good structured design technique. With this informa-
tion, we are now ready to create the design. It is shown in Figure 8- 30.
\

I Target

index
.a[0]
A
a( 1) a( 2] a[3] a[ 4) a[ 5 ] a[6 ] a[ 71 a[ 01 a[9 ] a( 10 ) a [ 11 ]
4 21 36 14 62 91 8 22 7 81 77 10

1 72 1= 4
index
a[0Pa[ 1 ) a|2] a[ 3] a[ 4 ] a|5 ] a [ 6 ] a[7 ] a(8) a(9) a[10 ] a[ 11 ]
4 21 ] 36 14 62 91 0 22 7 81 77 10

5 72 != 21
index
a(0) a|i) a|2) a [ 3 ] a]
^
a[ 5) a [ 6 ] a { 7) a[8 ] a[ 9) a|10] a[ll]
4 I 21 I 36 I 14 I 62 I 91 I 0 | 22 I 7 I 81 1 77 I 10
12 72 != 91
index
a (0 ] a ( 1 ] a [ 2 ] a [ 3! a( 4 ] a[ 5 j a(h ] n • r
4 21 36 14 62 91 8 22 7 81 77 10

Index off
Note : Not all test points are shown end of list

FIGURE 8 - 29 Unsuccessful Search in Unordered List

( SeqSearch

looker = 0
^
/
/ looker < last
&& target !=
\\
\ list[ looker ] M

looker++

* locn = looker
set found

( Return
J
FIGURE 8 - 30 Sequential Search Design

The code is shown in Program 8 - 13.


504 Section 8.6 Searching

PROGRAM 8 - 13 Sequential Search


1 ==== seqSearch ===
2 Locate target in an unordered list of size elements ,
3 Pre list must contain at least one item
4 last is index to last element in list
5 target contains the data to be located
6 locn is address for located target index
7 Post Found: matching index stored in locn
8 return true (found )
9 Not Found: last stored in locn
10 return false ( not found )
11 */
12 bool seqSearch ( int list[ ], int last ,
13 int target , int * locn)
14 {
15 // Local Declarations
16 int looker;
17 bool found ;
18
19 // Statements
20 looker = 0 ;
21 while (looker < last && target != list[ looker])
22 looker++ ;
23
24 *locn = looker ;
25 found = ( target == list[ looker]);
26 return found ;
27 > // seqSearch

Program 8 - 1 3 Analysis Program 8 - 13 is simple, but it does merit some discussion. First, why did we use a
while statement? Even though we know the limits of the array, it is still an event-
controlled loop. We search until we find what we are looking for or reach the end of
the list. Finding something is an event, so we use an event loop.
Next, note that there are two tests in the limit expression of the loop. We have
coded the test for the end of the array first. In this case, it doesn' t make any difference
which test is first from an execution point of view, but in other search loops it might. You
should get in the habit of coding the limit test first because it doesn' t use an indexed
value and is therefore safer.
The call-by - address use for locn also merits discussion. Since we need to pass the
found location back to the variable in the calling program, we need to pass its address
to the function. A typical call to the search would look like the
statement shown below.

found = seqSearch (stuAry , lastStu , stuID, &locn);

Notice how succinct this function is. In fact, there are more lines of documentation
than there are lines of code. The entire search is contained in one while statement. With
this short code, you might be tempted to ask, "Why write the function at all? Why not

You might also like