0% found this document useful (0 votes)
92 views8 pages

Searching

This document discusses different searching algorithms like linear search and binary search. It provides pseudocode to explain the step-by-step process of linear search and binary search. For linear search, it checks each element one by one to find the search element. Binary search is more efficient as it works only for sorted lists and divides the search space by half in each step to find the target element. The time complexity of linear search is O(n) while binary search has a complexity of O(log2n).

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views8 pages

Searching

This document discusses different searching algorithms like linear search and binary search. It provides pseudocode to explain the step-by-step process of linear search and binary search. For linear search, it checks each element one by one to find the search element. Binary search is more efficient as it works only for sorted lists and divides the search space by half in each step to find the target element. The time complexity of linear search is O(n) while binary search has a complexity of O(log2n).

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA

STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com

(C) GOYANI MAHESH 1


SEARCHING

(C) GOYANI MAHESH 2


LINEAR SEARCH

 Search Data from list in linear pattern


 Not efficient if data is nearer to the end of list

Index Element Index Element

1 99 67 1 99

2 100 2 100 67

3 1 3 1

4 67 4 67

5 44 5 44

… … … …

… … … …

n 21 n 21

(C) GOYANI MAHESH 3


LINEAR SEARCH

Index Element Index Element

1 99 1 99

2 100 2 100

3 1 67 3 1

4 67 4 67 67

5 44 5 44

… … … …

… … … …

n 21 n 21

(C) GOYANI MAHESH


SEARCH SUCCESSFUL 4
ALGORITHM Linear_Search (L, n, Element)
Where, L -> List of Elements
n -> Number of elements in list
Element -> Value to be searched

1. [initialize]
K = 0,Flag = 1
2. While (k<n) repeat step 3
3. If L[k] = element
i. Flag = 0
ii. “Search Successful :-> Element is
on Location K”
iii. K = K + 1
4. If (Flag)
“Element Not found in list ”
5. Exit

(C) GOYANI MAHESH 5


BINARY SEARCH

It works for sorted list and is a very efficient searching technique.

low
11 L[0] 11

22 22

33 33

44 mid 44

55 L[4] 55 low
66 66 L[5]

77 77
88 mid
88 88 L[7] 88
99 high 99 high
100 L[9] 100 L[9]

(C) GOYANI MAHESH 6


ALGORITHM Binary_Search (Element, n, L)
Where, Element -> Data to be searched
n -> No. of element in list
1. [initialize]
L -> List of Element
Low = 0, High = n-1, Flag = 1
2. While (low <= high) repeat step 3 & 4
3. Mid= (low + high) / 2
4. If (L[mid] == Element)
“DATA FOUND on mid Location”
flag = 0.
return
Else If (Element < L[mid])
high = mid – 1
Else
low = mid + 1
5. If (flag)
“Element not found in the List”.

(C) GOYANI MAHESH 7


COMPLEXITY

Linear Search:
Best case : O(1)
Average Case : O(n)
Worst Case : O(n)

Binary Search:
O(log2n)

(C) GOYANI MAHESH 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy