0% found this document useful (0 votes)
3 views

Chapter 9 Searching

The document discusses two searching algorithms: linear search and binary search. Linear search checks each element in a list sequentially until a match is found or the list is exhausted, while binary search operates on a sorted array, repeatedly dividing the search interval in half to locate the target value. Examples are provided for both algorithms to illustrate their processes.

Uploaded by

Nur Tra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 9 Searching

The document discusses two searching algorithms: linear search and binary search. Linear search checks each element in a list sequentially until a match is found or the list is exhausted, while binary search operates on a sorted array, repeatedly dividing the search interval in half to locate the target value. Examples are provided for both algorithms to illustrate their processes.

Uploaded by

Nur Tra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structure and

Algorithm
Chapter 9: Searching
Search
 Linear search is also known as sequential search.
 Linear search or sequential search is a method for
finding an element within a list.
 It sequentially checks each element of the list until a
Linear match is found or the whole list has been searched.
 Step through array of records, one at a time.
Search  Look for record with matching key.
 Search stops when
 record with matching key is found
 or when search has examined all records without
success.
Linear search can be broken down into the following
steps:

1. Start: Begin at the first element of the collection of


elements.
2. Compare: Compare the current element with the
desired element.
3. Found: If the current element is equal to the desired
Steps element, return true or index to the current
element.
4. Move: Otherwise, move to the next element in the
collection.
5. Repeat: Repeat steps 2-4 until we have reached the
end of collection.
6. Not found: If the end of the collection is reached
without finding the desired element, return that the
desired element is not in the array.
For example: Consider the array arr[] = {10, 50,
30, 70, 80, 20, 90, 40} and key = 30

Example
Example
 Binary search is a search algorithm that finds the
position of a target value within a sorted array.
 Binary search compares the target value to the
middle element of the array.

Binary  Binary search is a search algorithm used to find the


position of a target value within a sorted array.
Search  It works by repeatedly dividing the search interval in
half until the target value is found or the interval is
empty.
 The search interval is halved by comparing the target
element with the middle value of the search space.
 Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56,
72, 91}, and the target = 23.

Example
Example
END

You might also like