# Main Index - PowerPoint PPT Presentation

1 / 29

Lecture 14 – Search Algorithms. 1. Main Index. Contents. Search Algorithms Illustrating the Binary Search - Successful - Unsuccessful Binary Search Alg. Hash Search. 1.Sequential Search Algorithms.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Main Index

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

Lecture 14

– Search Algorithms

1

Main Index

Contents

• Search Algorithms

• Illustrating the Binary Search

• -Successful

• -Unsuccessful

• Binary Search Alg.

• Hash Search

### 1.Sequential Search Algorithms

• Search algorithms start with a target value and employ some strategy to visit the elements looking for a match.

• If target is found, the index of the matching element becomes the return value.

3

Main Index

Contents

4

Main Index

Contents

### Search Algorithms- Sequential Search Algorithm

int seqSearch(const int arr[], int first, int last, int target)

{

int i = first;

while(i != last && arr[i] != target)

i++;

return i;

}//O(n)

### 2. Binary Search Algorithms

Case 1.

A match occurs. The search is complete and mid is the index that locates the target.

if (midValue == target)// found match

return mid;

### Binary Search Algorithms

Case 2.

The value of target is less than midvalue and the search must continue in the lower sublist. Reposition the index last to the end of the sublist (last = mid).

// search the lower sublist

if (target < midvalue)<reposition last to mid><search sublist arr[first]…arr[mid-1]

### Binary Search Algorithms

Case 3.

The value of target is greater than midvalue and the search must continue in the upper sublist . Reposition the index first to the front of the sublist (first = mid+1).

// search upper sublist

if (target > midvalue)

<reposition first to mid+1>

<search sublist arr[mid+1]…arr[last-1]>

8

Main Index

Contents

### Illustrating the Binary Search- Successful Search

1.Search for target = 23

Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.

9

Main Index

Contents

### Illustrating the Binary Search- Successful Search

Step 2:

Indices first = 5, last = 9, mid = (5+9)/2 = 7.

Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.

10

Main Index

Contents

### Illustrating the Binary Search- Successful Search

Step 3:Indices first = 5, last = 7, mid = (5+7)/2 = 6.

Since target = midvalue = 23, a match is found at index mid = 6.

### Illustrating the Binary Search- Unsuccessful Search

Search for target = 4.

Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4.

Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.

### Illustrating the Binary Search- Unsuccessful Search

Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2.

Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.

### Illustrating the Binary Search- Unsuccessful Search

Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1.

Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

14

Main Index

Contents

### Binary Search Algorithm

int binSearch(const int arr[], int first, int last, int target)

{int mid;

int midvalue;

int origLast = last;

while (first < last){

mid = (first+last)/2;

midvalue = arr[mid];

if (target == midvalue)return mid;

else if (target < midvalue)last = mid;

elsefirst = mid+1;

}

return origLast;

}//O(log2n)

15

Main Index

Contents

12

5

23

3

55

16

-7

33

16

Main Index

Contents

散列函数。h(x)=i

h(x)=x mod k=i

### 散列函数/冲突解决

h(k)=k mod p ，p 最好是一个大素数。

p 的约数越多，发生这种分布不均匀的情况就越频繁，冲突几率越高。质数的约数最少，因此应选用大质数。

q mode p=21mode12=21-12*[21/12]=21-12*[7/4]

### 散列函数/冲突解决

（1）如何构造使数据分布均匀的散列函数；

（2）一旦发生冲突，用什么方法来解决；

19

Main Index

Contents

### Hash Example

20

Main Index

Contents

1、除余法（除留余数法）

2、平方取中法

因为一个数平方后中间几位数和该数的每一位都相关，由此得到的

散列地址比较随机。

3、数字分析法

关键字中某些数字可能在某些位上分布均匀，在某些位上分布不均

匀。可选取其中各种符号分布均匀的若干位作为散列地址，或进行

叠加后作散列地址，如电话号码。

4、折叠法

+ 4220 + 0224

+ 04　　　　　　　　　＋　04

10088 6092

５、随机数法　　H(key)=random(key)

21

Main Index

Contents

22

Main Index

Contents

separate chaining )

23

Main Index

Contents

25

Main Index

Contents

1、线性探查法

2、二次探查法

3、随机探查法

4、双散列探查法

26

Main Index

Contents

### 双散列探测方法

h1(x)=x%31

h2(x)=17-(x%17)

h2(64)=4

h2(467)=17-(467%17)=17-8=9

h2(777)=17-(777%17)=17-12=5

### 散列检索效率分析

AVL=(1*5+2*1+3*1+7*1)=17/8

### 散列检索效率分析

AVL=(1*6+2*2)=10/8