0% found this document useful (0 votes)
22 views36 pages

Unit 1

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

Unit 1

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

Searching Techniques

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 1


S e a rc h in g

In te re n a l s e a rc h in g E x te rn a l s e a rc h in g

B tre e s e a rc h in g

B + tre e s e a rc h in g

S e a rc h w ith k e y -c o m p a ris o n S e a rc h w ith o u t k e y -c o m p a ris o n

A d re s s c a lc u la tio n s e a rc h

L in e a r s e a rc h N o n -lin e ra s e a rc h

S e q u e n tia l s e a rc h T re e s e a rc h

B in a ry s e a rc h B in a ry s e a rc h tre e

In te rp o la tio n s e a rc h A V L tre e s e a rc h

R e d -b la c k tre e s e a rc h

S p la y tre e s e a rc h

D ig ita l s e a rc h

M u lti-w a y tre e s e a rc h

m -w a y tre e s e a rc h

B -tre e s e a rc h

G ra p h s e a rc h

D e p th firs t s e a rc h

B re a d th firs t s e a rc h

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 2


Linear Search

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 3


Sequential Search with Arrays

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 4


Flowchart: Sequential Search with Array
Start

i=0

Yes No
K = A[i]?

i = i+1

No
Print "Successful" i≥n
Yes

Print "Unsuccessful"

Stop

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 5


Example: Sequential Search with Array
int main()
{
int A[10], i, n, K, flag = 0;
printf("Enter the size of an array: ");
scanf("%d",&n);

printf("Enter the elements of the array: ");


for(i=0; i < n; i++)
scanf("%d",&A[i]);
printf("Enter the number to be searched: ");
scanf("%d",&K);
for(i=0;i<n;i++){
if(a[i] == K){
flag = 1; break;
}
}
if(flag == 0)
printf("The number is not in the list");
else
printf("The number is found at index %d",i);
return 0;
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 6


Complexity Analysis

• Case 1: The key matches with the first element


• T(n) = 1
• Case 2: Key does not exist
• T(n) = n
• Case 3: The key is present at any location in the array
n
T ( n)  p
i 1
i i

1
p1  p 2     pi     p n 
n
1
T ( n) 
n
i
i 1
n

n 1
T ( n) 
2

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 7


Complexity Analysis : Summary

Number of key Asymptotic


Case Remark
comparisons complexity

Case 1 T(n) = 1 T(n) = O(1) Best case

Case 2 T(n) = n T(n) = O(n) Worst case

Case 3 n 1 T(n) = O(n) Average case


T ( n) 
2

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 8


Binary Search

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 9


The Technique

m id
m id

l l u m= idm id
= -1
l(l+
= um)/2
id + 1 uu
S erach this half the sam e w ay S erach th is h alf th e sam e w ay
if K < A [m id] if K > A [m id ]

(c ) S(a)
e(ba rc
)AShneathrde ered
o rch e nthtireenlist
e arraytiretuolist
frnelem
s tuinrntonsets
thinetowseith
tha ercinsea
hdinexrch
g voin
f grig
alu o fhl,t-h
es uaan
lf dalf
left-h o nmlyoidn ly

CS 11001 : Programming and Data


Lecture #11: © DSamanta 10
Structures
Flowchart: Binary Search with Array
Start

mid = (l+u)/2

YES NO
K = A[mid]?

YES NO
K < A[mid]?

Search is successful
u = mid-1 l = mid+1

Stop
NO
(l>u)?

YES

Search is unsuccessful

Start

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 11


Binary Search (with Iteration)
#include <stdio.h>

int main()
{
int i, l, u, mid, n, K, data[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers in sorted order\n", n);

for (i = 0; i < n; i++)


scanf("%d",&array[i]);

printf("Enter value to find\n");


scanf("%d", &K);

l = 0;
u = n - 1; Contd…
mid = (l+u)/2;

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 12


Binary Search (with Iteration)
while (l <= u) {
if (data[mid] < K)
l = mid + 1;
else if (data[mid] == K) {
printf("%d found at location %d.\n", search, mid+1);
break;
}
else
u = mid - 1;

mid = (l + u)/2;
}
if (l > u)
printf("Not found! %d is not present in the list.\n", K);

return 0;
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 13


Binary Search (with Recursion)
#include<stdio.h>
int main(){

int data[100],i, n, K, flag, l, u;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements of the array in sorted order: " );


for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Enter the number to be search: ");


scanf("%d",&K);

l=0,u=n-1;
flag = binarySearch(data,n,K,l,u);
if(flag==0)
printf("Number is not found.");
else
printf("Number is found.");
Contd…
return 0;
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 14


Binary Search (with Recursion)
int binary(int a[],int n,int K,int l,int u){

int mid;

if(l<=u){
mid=(l+u)/2;
if(K==a[mid]){
return(1);
}
else if(m<a[mid]){
return binarySearch(a,n,K,l,mid-1);
}
else
return binarySearch(a,n,m,mid+1,u);
}
else return(0);
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 15


Complexity Analysis

5
< = >

2 8
< = > < = >

1 3 6 9
< = > < = > < = > < = >

F F F 4 F 7 F 10
< = > < = > < = >

F F F F F F

CS 11001 : Programming and Data


Lecture #11: © DSamanta 16
Structures
Complexity Analysis: Binary Search
5
< = >

2 8
< = > < = >

1 3 6 9
< = > < = > < = > < = >

F F F 4 F 7 F 10
< = > < = > < = >

F F F F F F

Let n be the total number of elements in the list under search and there exist an integer k such that:-
• For successful search:-
• If2  n  2 , then the binary search algorithm requires at least one comparison and at most k
k 1 k

comparisons.
• For unsuccessful
n  2 k 1 search:-
• If2 k 1  n ,then
2 k the
1 binary search algorithm requires k comparisons.
• If , then the binary search algorithm requires either k-1 or k number of comparisons.

CS 11001 : Programming and Data


Lecture #11: © DSamanta 17
Structures
Complexity Analysis: Binary Search

• Best case
T(n) = 1

• Worst case
log =2 n  1
T(n)

CS 11001 : Programming and Data


Lecture #11: © DSamanta 18
Structures
Complexity Analysis: Binary Search

• Average Case
• Successful search:-
I
T ( n)  1
n
T (n)  log 2 n   log 2 n  2  1
n n

• Unsuccessful search:-
E
T ' ( n) 
n 1

2
T ' (n)  log 2 n  
n 1

CS 11001 : Programming and Data


Lecture #11: © DSamanta 19
Structures
Interpolation Search

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 20


Interpolation Search
1. l = 1, u = n // Initialization: Range of searching
2. flag = FALSE // Hold the status of searching
3. While (flag = FALSE) do
 K  A[l ] 
4. loc     u  l   l
 A[u ]  A[l ] 
5. If ( l  loc  u ) then // If loc is within the range of the list
6. Case: K < A[loc]
7. u = loc -1
8. Case: K = A[loc]
9. flag = TRUE
10. Case: K > A[loc]
11. l = loc +1
12. Else
13. Exit()
14. EndIf
15. EndWhile
16. If (flag) then
17. Print “Successful at” loc
18. Else
19. Print “Unsuccessful”
20. EndIf
21. Stop

CS 11001 : Programming and Data


Lecture #11: © DSamanta 21
Structures
Complexity Analysis:
Interpolation Search

Best case Worst case Average case

Successful 1 n log 2 log 2 n 

Interpolation search

Unsuccessful n
n n

CS 11001 : Programming and Data


Lecture #11: © DSamanta 22
Structures
Comparision: Successful Search

100

80 S earch array
Time (  s)

60
B inary search
40
Interpolation search
20

0 S earch list
10 100 500 1000 5000 10000
Input Size

CS 11001 : Programming and Data


Lecture #11: © DSamanta 23
Structures
Comparision: Unsuccessful Search

100

80 S earch A rray
Time (  s)

60
B inary S earch
40
Interpolation search
20

0 S earch list
10 100 500 1000 5000 10000
Input Size

CS 11001 : Programming and Data


Lecture #11: © DSamanta 24
Structures
Sequential Search with Linked List

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 25


Sequential Search with Linked
List

DATA L IN K

(a) S tru ctu re o f a n o d e in th e lin ked list

H ead er
S earch at an in term ed iate n od e:
S earch b egin s S earch stop s h ere if k ey m atch es S earch u n su ccessfu lly en d s h ere
h ere else m ove to its im m ed iate n ext n od e

(b ) L in ear search o n a lin ked list

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 26


Flow Chart: Sequential Search with LL
Start

temp = header

while
Print Unsuccessful
temp != NULL
temp = temp->next
T
N temp->data == Y
key

Print Success
Return temp

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 27


Example: Sequential Search with LL
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

int main()
{
struct node *header = NULL;
int K, n;

printf("Enter the number of nodes: ");


scanf("%d", &n);
printf("\nDisplaying the list\n");
generate(header, num);
printf("\nEnter key to search: ");
scanf("%d", &key);
searchBinary(header, K);
delete(header);

return 0;
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 28


Example: Linear Search with LL
void generate(struct node *head, int n)
{
int i;
struct node *temp;

for (i = 0; i < num; i++)


{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = rand() % n;
if (*header == NULL)
{
*header = temp;
temp->next = NULL;
}
else
{
temp->next = header;
header = temp;
}
printf("%d ", temp->data);
}
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 29


Example: Linear Search with LL
void searchBinary(struct node *temp, int K)
{
while (temp != NULL)
{
if (temp->data == K)
{
printf("key found\n");
return;
}
else temp = temp->next;
}
printf("Key not found\n");
}

void delete(struct node *header)


{
struct node *temp;
temp = header;
while (temp != NULL)
{
temp = temp->next;
free(header);
header = temp;
}
}

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 30


Complexity Analysis

Number of key
Case Asymptotic complexity Remark
comparisons

Case 1 T(n) = 1 T(n) = O(1) Best case

n 1
Case 2 T ( n)  T(n) = O(n) Average case
2

Case 3 T(n) = n T(n) = O(n) Worst case

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 31


Sentinel Linear
Search

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 32


• Sentinel Linear Search as the name suggests is a type
of Linear Search where the number of comparisons is
reduced as compared to a traditional linear search.

• In a traditional linear search, only N comparisons are


made, and in a Sentinel Linear Search, the sentinel
value is used to avoid any out-of-bounds comparisons,
but there is no additional comparison made specifically
for the index of the element being searched.

33
STEPS FOR SENTINEL LINEAR SEARCH ALGORITHM:

Initialize the search index variable i to 0.


Set the last element of the array to the search key.
While the search key is not equal to the current element of the array (i.e.,
arr[i]), increment the search index i.
If i is less than the size of the array or arr[i] is equal to the search key,
return the value of i (i.e., the index of the search key in the array).
Otherwise, the search key is not present in the array, so return -1 (or any
other appropriate value to indicate that the key is not found).

Lecture #11: © DSamanta CS 11001 : Programming and Data Structures 34


Exponential Search
• The name of this searching algorithm may be misleading
as it works in O(Log n) time. The name comes from the
way it searches an element.

• Exponential search involves two steps:

1.Find range where element is present

2.Do Binary Search in above found range.


• Time Complexity : O(Log n)
Applications of Exponential
Search:
1.Exponential Binary Search is particularly useful for
unbounded searches, where size of array is infinite.
Please refer Unbounded Binary Search for an
example.
2.It works better than Binary Search for bounded
arrays, and also when the element to be searched is
closer to the first element.

You might also like