Embed
Email

SEARCHING

Document Sample

Shared by: pptfiles
Categories
Tags
Stats
views:
3
posted:
11/10/2011
language:
pages:
28
1









SEARCHING







Chalermsup Sankavichitr





By Chalermsub Sangkavichitr

2 Searching

Reference:

สาขาวิชาคอมพิวเตอร์และเทคโนโลยี

อ.เลาขวัญ งามประสิทธิ ์



อัลกอริทมการค้ นหาข้ อมูล



Linear Searching

 ข้อมูลไม่มีการเรี ยงลาดับ

• Sequential Search

 ข้อมูลมีการเรี ยงลาดับ

• Indexed Sequential Search

• Binary Search

– Array หรื อ Linked list

– Binary Search Tree

Sequential Searching



• ั

เหมาะสาหรับใช้กบโครงสร้างข้อมูลที่เก็บข้อมูลแบบไม่เรี ยงลาดับ

• ั

ใช้กบโครงสร้างข้อมูลที่มีปริ มาณข้อมูลน้อย

• ั

มักใช้กบโครงสร้างข้อมูล array และ linked list

• มีประสิ ทธิภาพเป็ น O(n)

• กรณี ดีที่สุด (best case) เป็ น O(1)

Example Sequential Search



• การค้นหาแบบเรี ยงลาดับ กรณี ที่พบข้อมูล

Key = 54



k 3 7 12 25 32 48 54 78

0 1 2 3 4 5 6 7



i=0 i=i+1 i=i+1i=i+1 i=i+1 i=i+1 i=i+1



not not not not not not found

found foundfoundfoundfoundfound i = 6

Comparison 7 time(s)

Example Sequential Search





• การค้นหาแบบเรี ยงลาดับ กรณี ที่ไม่พบข้อมูลที่ตองการ



Key = 94 k 3 7 12 25 32 48 54 78

0 1 2 3 4 5 6 7



i=0 i=i+1 i=i+1i=i+1 i=i+1 i=i+1i=i+1 i=i+1



not not not not not not not not

found foundfoundfoundfoundfoundfound found

NOT FOUND…!!!

Comparison 8 time(s)

Pseudocode of Sequential

7

Search

For each item in the list:

if that item has the desired value,

stop the search and return the item's location.

Return index.

//java example

For (int i=0; i A[mid] then

min := mid + 1

else

max := mid - 1;

until (A[mid] = x) or (min > max);

Source Code of Binary Search

27

(Recursive)

BinarySearch(A[0..N-1], value, low, high) { //find “value”

if (high value)

return BinarySearch(A, value, low, mid-1)

else if (A[mid] < value)

return BinarySearch(A, value, mid+1, high)

else

return mid // found

}

28


Related docs
Other docs by pptfiles
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!