Top Banner
1 MSLee2002@Yes 計計計計計 - 計計 mslee@nctu Ming-Shang Lee CopyRight 2001 搜搜 (Searching) 計計計 計計
41

MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

1

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

Ming-Shang Lee CopyRight 2001

搜尋 (Searching)

李明山 編撰

Page 2: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

2

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

搜尋的意義

從很多資料錄 (Records) 中找出特定的資料錄 (Record) ,稱為搜尋 (Searching)

Page 3: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

3

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

搜尋的實例學生資料查詢─學號搜尋電話號碼簿─人名搜尋銀行交易─帳號搜尋‧‧‧‧‧‧

Page 4: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

4

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義平行陣列 (Parallel arrays)

NAME(1) JANE NUMBER(1) 742-8319NAME(2) JIM NUMBER(2) 591-4872NAME(3) LARRY NUMBER(3) 984-1265NAME(4) MARY NUMBER(4) 135-0465NAME(5) SUE NUMBER(5) 491-2031

Page 5: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

5

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義資料表 (Table)

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 6: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

6

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義資料錄 (Record)

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 7: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

7

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義欄位 (Field)

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 8: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

8

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義關鍵欄 (Key Field)

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 9: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

9

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料處理名詞釋義關鍵值 (Key)

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 10: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

10

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

搜尋的方法循序搜尋法 (Sequential Search)二元搜尋法 (Binary Search)赫序函數搜尋法 (Hashing Functio

n Search)

Page 11: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

11

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

循序搜尋法 (Sequential Search)

所謂循序搜尋法 (Sequential Search) ,就是從資料中的第一項或最後一項開始,逐項與所要找尋的特定資料做比較,若兩者相同即找到了。

這是最簡單的資料搜尋法,也是最沒有效率的一種方法。

Page 12: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

12

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

循序搜尋法 (Sequential Search)

INPUT DESIRED_NAMEI 1WHILE NAME(I) DESIRED_NAME DO I I +1END WHILEOUTPUT NUMBER(I)

NAME(1) JANE NUMBER(1) 742-8319NAME(2) JIM NUMBER(2) 591-4872NAME(3) LARRY NUMBER(3) 984-1265NAME(4) MARY NUMBER(4) 135-0465NAME(5) SUE NUMBER(5) 491-2031

Page 13: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

13

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

循序搜尋法 (Sequential Search)INPUT DESIRED_NAMENAME(6) DESIRED_NAMEI 1WHILE NAME(I) DESIRED_NAME DO I I +1END WHILEIF I 5 THEN OUTPUT NUMBER(I)ELSE OUTPUT ‘NOT FOUND!’ENDIF

Page 14: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

14

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search) 將資料表按鍵值大小次序排序 取資料表搜尋範圍最中間一筆資料錄比對 如果對上搜尋之關鍵值則搜尋結束 如果搜尋之關鍵值較小,則將搜尋範圍縮為前半段 如果搜尋之關鍵值較大,則將搜尋範圍縮為後半段 重複搜尋之步驟

Page 15: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

15

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

Ming-Shang Lee CopyRight 2001

二元搜尋法 (Binary Search)搜尋 8532009

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二8432092 100 88 95 89 ○ 90 758532001 100 95 80 90 ○ 90 858532002 100 90 90 95 ○ 80 858532003 93 90 75 95 △ 65 908532004 88 100 90 94 ○ 65 708532006 96 85 85 89 ○ 65 858532007 85 100 70 0 0 758532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 16: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

16

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二

8532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 658532011 85 0 75 89 65 1008532012 86 90 85 89 65 758532013 83 93 80 89 ○ 90 858532014 75 88 90 89 70 95

Page 17: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

17

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009

學號 作業二 作業三 作業四 作業五 作業六 練習一 練習二

8532008 86 98 90 95 90 808532009 83 85 80 89 60 808532010 90 83 80 95 ○ 80 65

Page 18: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

18

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)

搜尋範圍

Low

High

Middle

Page 19: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

19

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)

搜尋範

圍Low

Middle High

Page 20: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

20

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)

搜尋範

圍Low

Middle

High

Page 21: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

21

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search) (重複) 將資料表按鍵值大小次序排序 取資料表搜尋範圍最中間一筆資料錄比對 如果對上搜尋之關鍵值則搜尋結束 如果搜尋之關鍵值較小,則將搜尋範圍縮為前半段 如果搜尋之關鍵值較大,則將搜尋範圍縮為後半段 重複搜尋之步驟

Page 22: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

22

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009 作業四 (HW4) 成績

# StuID HW1 HW2 HW3 HW4 HW5 Ex1 Ex21 8432092 100 88 95 89 ○ 90 752 8532001 100 95 80 90 ○ 90 853 8532002 100 90 90 95 ○ 80 854 8532003 93 90 75 95 65 90△5 8532004 88 100 90 94 ○ 65 706 8532006 96 85 85 89 ○ 65 857 8532007 85 100 70 0 0 758 8532008 86 98 90 95 90 809 8532009 83 85 80 89 60 8010 8532010 90 83 80 95 ○ 80 6511 8532011 85 0 75 89 65 10012 8532012 86 90 85 89 65 7513 8532013 83 93 80 89 ○ 90 8514 8532014 75 88 90 89 70 95

Low

High

Mid

Page 23: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

23

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009 作業四 (HW4) 成績

# StuID HW1 HW2 HW3 HW4 HW5 Ex1 Ex21 8432092 100 88 95 89 ○ 90 752 8532001 100 95 80 90 ○ 90 853 8532002 100 90 90 95 ○ 80 854 8532003 93 90 75 95 65 90△5 8532004 88 100 90 94 ○ 65 706 8532006 96 85 85 89 ○ 65 857 8532007 85 100 70 0 0 758 8532008 86 98 90 95 90 809 8532009 83 85 80 89 60 8010 8532010 90 83 80 95 ○ 80 6511 8532011 85 0 75 89 65 10012 8532012 86 90 85 89 65 7513 8532013 83 93 80 89 ○ 90 8514 8532014 75 88 90 89 70 95

Low

High

Mid

Page 24: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

24

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009 作業四 (HW4) 成績

# StuID HW1 HW2 HW3 HW4 HW5 Ex1 Ex21 8432092 100 88 95 89 ○ 90 752 8532001 100 95 80 90 ○ 90 853 8532002 100 90 90 95 ○ 80 854 8532003 93 90 75 95 65 90△5 8532004 88 100 90 94 ○ 65 706 8532006 96 85 85 89 ○ 65 857 8532007 85 100 70 0 0 758 8532008 86 98 90 95 90 809 8532009 83 85 80 89 60 8010 8532010 90 83 80 95 ○ 80 6511 8532011 85 0 75 89 65 10012 8532012 86 90 85 89 65 7513 8532013 83 93 80 89 ○ 90 8514 8532014 75 88 90 89 70 95

Low

High

Mid

Page 25: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

25

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

二元搜尋法 (Binary Search)搜尋 8532009 作業四 (HW4) 成績

Low = 1 ; High = Record_Count

Repeat

Mid = INT((Low + High) / 2)

If StuID(Mid) < “8532009” Then

Low = Mid +1

ElseIf StuID(Mid) > “8532009” Then

High = Mid - 1

End If

Until StuID(Mid) = “8532009” or Low > High

If Low <= High Then

Output HW4(Mid)

Else

Output “ 無學號 8532009 之學生成績“End If

Page 26: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

26

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

LOW 1HIGH NREPEAT

MIDDLE INT( (LOW +HIGH) / 2 )IF DESIRED_NAME <NAME(MIDDLE) THEN

HIGH MIDDLE - 1ELSE IF DESIRED_NAME >NAME(MIDDLE) THEN

LOW MIDDLE + 1END IF

UNTIL (DESIRED_NAME =NAME(MIDDLE) ) OR (LOW >HIGH)

二元搜尋法 (Binary Search)

Page 27: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

27

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

SUBROUTINE BinarySearch(Key, X(), LowBound, UpperBound, Index)Low LowBoundHigh UpperBoundREPEAT

Middle INT( (Low + High ) / 2 )IF Key <X(Middle) THEN

High Middle - 1ELSE IF Key >X(Middle) THEN Low Middle + 1END IF

UNTIL (Key =X(Middle) ) OR (Low > High)IF Low >High THEN Index =0 ELSE Index =Middle

END SUBROUTINE

二元搜尋法 Subroutine - Nonrecursive version

Page 28: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

28

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

SUBROUTINE BinarySearch(Key, X(), LowBound, UpperBound, Index)Middle INT( (LowBound + UpperBound) / 2 )IF Key <X(Middle) THEN

BinarySearch(Key, X(), LowBound, Middle -1, Index)ELSE IF Key >X(Middle) THEN BinarySearch(Key, X(), Middle +1, UpperBound, Index)ELSE IF LowBound >UpperBound THEN

Index =0 ELSE

Index =MiddleEND IF

END SUBROUTINE

二元搜尋法 Subroutine - Recursive version

Page 29: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

29

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

資料存取技術 --赫序函數搜尋法(Hashing Function Search)

赫序 (Hashing) 是一種相當有效率的資料存取技術,從檔案存取到資料庫管理系統 (Database Management System) ,高階程式語言的翻譯程式與編譯程式,計算機的作業系統 (Operating System) ,以致於許多系統程式為求簡單快速地運用有限的儲存資源達成資料運作 (Data manipulate) 的功能,都是應用這種技術。

Page 30: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

30

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數搜尋法的意義

所謂「赫序函數」,簡單地說,就是可以將鬆散而無序的資料換算成密集的、有對應關係的整數值之簡單函數。

有人稱赫序函數為一種由關鍵字至位址 (Key-to-address) 的轉換函數。

Page 31: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

31

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數搜尋法之資料儲存與擷取首先,欲將資料儲存在檔案時,用赫序函數我們可以計算出每一資料錄之關鍵字所對應的整數值,而該整數即為預備作為存放該資料錄之位址。

其次,在以赫序函數儲存資料的檔案中,欲找尋特定之資料錄,即可以該資料錄之關鍵字,運用相同之赫序函數計算出對應的整數值,而該整數即為存放該資料錄之位址。

Page 32: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

32

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

學號 姓名8521088 林弋真8721089 張幼洵8723120 蔡坤賢8732021 許洛賓8732034 林惠雯8733028 王景敏8633046 邱雅欣8753025 李馨儀8854027 莫芬蘭8730020 萬鶴倫

# 學號 姓名1 8723120 蔡坤賢2 8732021 許洛賓345 8732034 林惠雯678 8632087 許雅欣9 8521088 林弋真10 8721089 張幼洵

赫序函數搜尋法─範例 (餘數法 )

Page 33: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

33

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數搜尋法之要領

設定一種以資料錄關鍵值決定儲存位址的赫序函數。

每一筆資料錄依設定的赫序函數決定儲存位址。 搜尋時,由特定的資料錄之關鍵值求算赫序函數值即可推算儲存位址,找出特定的資料錄。

赫序函數的計算時間得遠小於在資料表中比對資料錄的時間。

Page 34: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

34

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

1. 餘數法將資料錄關鍵值除以固定數之餘數為儲存位址

H(K) = (K mod N) + 1K 代表資料錄關鍵值N 代表儲存位址容納資料錄的數量

{10, 13, 19, 52, 31} 之儲存位址分別是1, 4, 5, 3, 2

幾種簡單的赫序函數

Page 35: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

35

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

2. 平方取中間數法計算出鍵值 K 的平方,然後截取中間

N 位儲存位址H(K) = H(1234)

= G(1234 ×1234)= G(1522756)= 227

幾種簡單的赫序函數

Page 36: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

36

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

3. 折疊法將鍵值 K 以 L 為長度切割成許多段落,然後將這些段落相加得儲存位址

K = 1234567, L = 2H(1234567) = 12 + 34 + 56 + 7 = 109

幾種簡單的赫序函數

Page 37: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

37

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數的碰撞 (Collision) 當兩個以上的資料錄藉由赫序函數計算而得

的整數值相同時,意即兩者都將使用相同的儲存位置,此時,即產生碰撞問題。

{10, 13, 19, 53, 31} 之儲存位址分別是 1, 44, 5, 44, 2

“13” 與“ 53” 都要選擇 44號位置作儲存碰撞問題

處理碰撞問題的方法 -- 基本上,「先入為主」,後來者即需往後面的位置找尋空位!1.線性移位法2.跳蛙式移位法

Page 38: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

38

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數的碰撞─線性移位法

若預備作為存放資料錄之位址已先有資料佔用,則往下找尋空的位置,將該資料錄存入。

若是到達儲存位置的末端依舊無空位,則轉向儲存位置的前端繼續找尋空的位置。

Page 39: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

39

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數的碰撞─線性移位法K=43 N=10H(K) = (K mod N) + 1 = 4

50 71 13 24 75 27 8

1 2 3 4 5 6 7 8 9 10

43

Page 40: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

40

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數的碰撞─跳蛙式移位法

若預備作為存放資料錄之位址已先有資料佔用,則改試下一個位置(意即下移一步),若也被佔用,則跳過新位置之下一個,而以其下下一個(意即下移二步)為新位置,若這個新位置也被佔用,則下移四步為新位置,依此類推。

若是到達儲存位置的末端依舊無空位,則依步數轉向儲存位置的前端繼續找尋空的位置。

Page 41: MSLee2002@Yes 計算機概論 - 搜尋 mslee@nctu 1 Ming-Shang Lee CopyRight 2001 搜尋 (Searching) 李明山 編撰.

41

MSLee2002@Yes

計算機概論 - 搜尋

mslee@nctu

赫序函數的碰撞─跳蛙式移位法K=43 N=10

H(K) = (K mod N) + 1 = 4

50 71 13 24 75 27 8

1 2 3 4 5 6 7 8 9 10

43