0% found this document useful (0 votes)
6 views18 pages

(Alg-04) Searching Algorithms 2nd 23-24

The document discusses three searching algorithms: Linear Search, Binary Search, and Jump Search. It explains the mechanics of each algorithm, including their requirements (e.g., sorted arrays for Binary and Jump Searches) and their performance in terms of comparisons for successful and unsuccessful searches. Additionally, it provides examples and time complexity for Jump Search.

Uploaded by

5w4jmk5n2p
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)
6 views18 pages

(Alg-04) Searching Algorithms 2nd 23-24

The document discusses three searching algorithms: Linear Search, Binary Search, and Jump Search. It explains the mechanics of each algorithm, including their requirements (e.g., sorted arrays for Binary and Jump Searches) and their performance in terms of comparisons for successful and unsuccessful searches. Additionally, it provides examples and time complexity for Jump Search.

Uploaded by

5w4jmk5n2p
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/ 18

4

SEARCHING ALGORITHMS
‫ مالك بريك‬.‫د‬

2nd 23-24 1
• SEARCHING AN ARRAY OR OTHER DATA
STRUCTURE HAS TWO POSSIBLE
OUTCOMES:
• SUCCESSFUL SEARCH (VALUE FOUND)
• UNSUCCESSFUL SEARCH (VALUE NOT
SEARCHING FOUND)

• IT IS POSSIBLE FOR ONE SEARCHING


ALGORITHM TO PERFORM VERY WELL FOR
SUCCESSFUL SEARCHES, AND VERY POORLY
FOR UNSUCCESSFUL SEARCHES.
2nd 23-24 2
SUCCESSFUL AND UN SUCCESSFUL SEARCH

2nd 23-24 3
• WE WILL EXAMINE THREE SEARCHING ALGORITHMS:
• LINEAR SEARCH
• BINARY SEARCH
• JUMP SEARCH

SEARCHING
• A LINEAR SEARCH SEARCHES THE ARRAY FROM THE FIRST
INDEX POSITION TO THE LAST IN A LINEAR PROGRESSION.

• THIS SEARCH IS ALSO KNOWN AS A SEQUENTIAL SEARCH.

2nd 23-24 4
o IN A LINEAR SEARCH, IF THE NUMBER OF ENTRIES
IN THE ARRAY IS N, THERE WILL BE N
COMPARISONS FOR AN UNSUCCESSFUL SEARCH.

LINEAR o FOR A SUCCESSFUL SEARCH, THERE WILL BE A


SEARCH MINIMUM OF 1 AND A MAXIMUM OF N
COMPARISONS.

o ON AVERAGE, THERE WILL BE N/2


COMPARISONS.
2nd 23-24 5
LINEAR SEARCH-SEQUENTIAL SEARCH-
-UNSORTED ARRAY
Below is a linear search algorithm.

2nd 23-24 6
• IF THE VALUES IN AN ARRAY ARE
ARRANGED IN ASCENDING OR
DESCENDING ORDER, THE ARRAY IS
SORTED.

BINARY • FOR A BINARY SEARCH TO BE APPLIED,


SEARCH THE ARRAY MUST BE SORTED.

• THE IDEA BEHIND THE BINARY SEARCH IS


TO DIVIDE THE ARRAY ELEMENTS IN HALF
EACH TIME, UNTIL THE PARTICULAR
2nd 23-24 ELEMENT IS LOCATED. 7
BINARY SEARCH
• IF WE ARE SEARCHING FOR THE VALUE 77,
• WE FIRST FIND THE MIDDLE POSITION OF THE ARRAY.

• WE SEE THAT 77 IS NOT IN INDEX POSITION [4].


2nd 23-24 8
BINARY SEARCH
• WE KNOW THE ARRAY IS SORTED. SINCE 77 IS LARGER THAN 38, THE VALUE MUST BE IN THE
RIGHT HALF OF THE ARRAY.

• WE NEXT SEARCH THE MIDDLE POSITION OF THE RIGHT HALF OF THE ARRAY, WHICH IS INDEX
POSITION [6], AND LOCATE THE VALUE.

2nd 23-24 9
BINARY
SEARCH
E F F E C T O F O NE
C O M PA RISON I N B I NA RY
SEARCH.

2ND 23-24 10
• USING THE BINARY SEARCH, IN THE WORST
CASE, THE NUMBER OF COMPARISONS IS
THE NUMBER OF TIMES YOU CAN DIVIDE
THE ARRAY IN HALF.

BINARY • THE MAXIMUM NUMBER OF


COMPARISONS K IS DERIVED BY SOLVING
SEARCH THE EQUATION
• N = 2K

• LOG2N = K

2nd 23-24 11
BINARY SEARCH

H OW TH E U NS U C C ES SFUL
S E A R C H I S T E R M INAT E D I N
T H E B I NA RY S E A R C H
R O U T INE .

2nd 23-24 12
BINARY SEARCH-NON-RECURSIVE VERSION

2nd 23-24 13
BINARY SEARCH
• RECURSIVE VERSION OF
BINARY SEARCH ALGORITHM.

2nd 23-24 14
JUMP SEARCH:
A FAST SEQUENTIAL SEARCH
TECHNIQUE
input: A: sorted array,
x: key to find in A
output: index of x in A.
Note: For a Jump search to be
applied, the array must be
sorted.

2nd 23-24 15
EXAMPLE

• APPLY JUMP SEARCH TO FIND 55 IN ARRAY A:


• A = 0(2, 3,15, 5,2 6, 6,3 8, 13,
4 21,
5 34,6 55, 775, 80,
8 85,
9 87,1091,95).
11 12 13 14 15 16
2 3 5 5 6 6 8 13 21 34 55 75 80 85 87 91 95

• ARRAY SIZE N = 17. THE BLOCK SIZE IS M = 𝑛 = 4. X = 55


STEP 1: X>A[0] THEN JUMP FROM INDEX 0 TO INDEX 4;
STEP 2: X>A[4] THEN JUMP FROM INDEX 4 TO INDEX 8;
STEP 3: X>A[8] THEN JUMP FROM INDEX 8 TO INDEX 12;
STEP 4: X<A[12] THEN PERFORM LINEAR SEARCH IN PREVIOUS BLOCK FROM INDEX 8 TO INDEX 12 .
• STEP 5: RETURN X AT POSITION 10.

2nd 23-24 16
2nd 23-24 17
JUMP SEARCH TIME COMPLEXITY

Best case Ω(1)


Worst case Ο( 𝑛)

2nd 23-24 18

You might also like