งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Week 13 Basic Algorithm 2 (Searching)

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Week 13 Basic Algorithm 2 (Searching)"— ใบสำเนางานนำเสนอ:

1 Week 13 Basic Algorithm 2 (Searching)

2 Searching Arrays การค้นหาแบบเชิงเส้น (Linear search) Small arrays
Unsorted arrays การค้นหาแบบไบนารี (Binary search) Large arrays Sorted arrays *From: web.cs.wpi.edu/~cs1005/c03/elliott/handouts/Lecture15.ppt

3 Linear Search Algorithm
ใช้กับข้อมูลที่มีจำนวนไม่มาก สามารถใช้ได้กับข้อมูลที่ยังไม่ถูกเรียงลำดับ (unsorted) การทำงานของการค้นหาแบบเชิงเส้น เริ่มจากสมาชิกตัวแรกของตัวแปรชุด ทำการเปรียบเทียบค่าของสมาชิกกับข้อมูล (key) ที่ต้องการสืบค้น ทำการเปรียบเทียบกับสมาชิกตัวถัดไป จนกระทั่งพบข้อมูลที่ต้องการ หรือ... จนกระทั่งถึงสมาชิกตัวสุดท้ายในตัวแปรชุด โดยเฉลี่ยแล้ว เราจะพบข้อมูลที่ต้องการเมื่อทำการเปรียบเทียบกับ ข้อมูลจำนวนประมาณครึ่งหนึ่งของจำนวนสมาชิกทั้งหมด Start at first element of array. Compare value to value (key) for which you are searching Continue with next element of the array until you find a match or reach the last element in the array. Note: On the average you will have to compare the search key with half the elements in the array.

4 Linear Search C++ Code Ex 1: โปรแกรมแสดงการค้นหาแบบเชิงเส้น

5 Linear Search Results Ex 1: ผลการทำงานของโปรแกรม
int a[13] = {1,100,2,66,55,44,88,77,12,23,45,9,87};

6 Binary Search Algorithm
สามารถใช้งานได้กับข้อมูลที่ถูกเรียบลำดับแล้วเท่านั้น ภายหลังจากการเปรียบเทียบข้อมูลแต่ละครั้ง จะมีการตัดข้อมูลที่สนใจออกไปครึ่งหนึ่ง การทำงานของการค้นหาแบบไบนารี หาสมาชิกในตัวแปรชุดที่ถูกเรียงไว้อยู่ในตำแหน่งกึ่งกลางของสมาชิกที่สนใจ เปรียบเทียบค่าของสมาชิกดังกล่าวกับค่าของข้อมูล (key) ที่ต้องการสืบค้น หากมีค่าเท่ากัน ให้จบการทำงานและรายงานผลการค้นพบข้อมูลที่ต้องการ หากมีค่าไม่เท่ากัน ให้เลือกครึ่งหนึ่งของสมาชิกทั้งหมดที่น่าจะมีข้อมูล (key) ที่ต้องการไว้เพื่อใช้ทำการค้นหาต่อไป ทำซ้ำกระบวนการ หาสมาชิกกึ่งกลาง เปรียบเทียบ และเลือกสมาชิกครึ่งหนึ่ง ไปเรื่อยๆ จนกระทั่ง ค้นพบข้อมูลที่ต้องการ หรือ... ไม่เหลือสมาชิกเหลือให้เปรียบเทียบ May only be used on a sorted array. Eliminates one half of the elements after each comparison. Locate the middle of the array Compare the value at that location with the search key. If they are equal - done! Otherwise, decide which half of the array contains the search key. Repeat the search on that half of the array and ignore the other half. The search continues until the key is matched or no elements remain to be searched

7 Binary Search Algorithm
search key = 19 1 2 3 4 5 6 7 8 9 10 11 12 1 5 15 19 25 27 29 middle of the array compare a[6] and 19 19 is smaller than 29 so the next search will use the lower half of the array 31 33 45 55 88 100

8 Binary Search Algorithm
search key = 19 1 2 3 4 5 1 5 15 use this as the middle of the array Compare a[2] with 19 15 is smaller than 19 so use the top half for the next pass 19 25 27

9 Binary Search Algorithm
search key = 19 3 4 5 a 19 use this as the middle of the array Compare a[4] with 19 25 is bigger than 19 so use the bottom half 25 27

10 Binary Search Algorithm
search key = 19 3 a 19 use this as the middle of the array Compare a[3] with 19 Found!!

11 Binary Search Algorithm
search key = 18 1 2 3 4 5 6 7 8 9 10 11 12 1 5 15 19 25 27 29 middle of the array compare a[6] and 18 18 is smaller than 29 so the next search will use the lower half of the array 31 33 45 55 88 100

12 Binary Search Algorithm
search key = 18 1 2 3 4 5 1 5 15 use this as the middle of the array Compare a[2] with 18 15 is smaller than 18 so use the top half for the next pass 19 25 27

13 Binary Search Algorithm
search key = 18 3 4 5 a 19 use this as the middle of the array Compare a[4] with 18 25 is bigger than 18 so use the bottom half 25 27

14 Binary Search Algorithm
search key = 18 3 a 19 use this as the middle of the array Compare a[3] with 18 Does not match and no more elements to compare. Not Found!!

15 Binary Search C++ Code Ex 2: โปรแกรมแสดงการค้นหาแบบเชิงเส้น

16 Binary Search Results Ex 2: ผลการทำงานของโปรแกรม
int a[13] = {1,5,15,19,25,27,29,31,33,45,55,88,100};

17 Searching Efficiency หากต้องการค้นข้อมูลจากสมาชิก 1,024 ตัว จะใช้จำนวนรอบในการทำงานเพียง 10 รอบเพื่อให้พบ(หรือไม่พบ)ข้อมูลที่ต้องการ จะมีการคัดสมาชิกที่สนใจในแต่ละรอบการทำงานเป็น 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 ตามลำดับ ในกรณีที่มีจำนวนสมาชิก 1 พันล้านตัว จะมีการเปรียบเทียบอย่างมาก ที่สุด 30 ครั้ง ยิ่งมีจำนวนสมาชิกมากขึ้นเท่าใด ประสิทธิภาพในการค้นหาของการ ค้นหาแบบไบนารีจะมีมากขึ้นเมื่อเปรียบเทียบกับประสิทธิภาพของการ ค้นหาของการค้นหาแบบเชิงเส้น Searching and array of 1024 elements will take at most 10 passes to find a match or determine that the element does not exist in the array. 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 An array of one billion elements takes a maximum of 30 comparisons. The bigger the array the better a binary search is as compared to a linear search

18 Summary ในหัวข้อนี้ ได้แนะนำถึงการเขียนโปรแกรมสำหรับการค้นหาข้อมูล (Searching) เพื่อฝึกทักษะในการเขียนโปรแกรมที่ใช้งานข้อมูลในตัวแปรชุดที่มีความซับซ้อนมากกว่าการเรียงข้อมูล ได้แนะนำวิธีการค้นหาข้อมูล 2 รูปแบบคือ การค้นหาแบบเชิงเส้น (Linear Search) การค้นหาแบบไบนารี (Binary Search) Linear Search จะทำการค้นหาจากตัวแรกของตัวแปรชุดไปเรื่อยๆ ทีละตัว สามารถทำงานได้กับข้อมูลที่ไม่ได้เรียงลำดับมาก่อน

19 Summary Binary Search จะทำการค้นหาข้อมูลในตัวแปรชุดซึ่งต้องเป็นข้อมูลที่ได้รับการเรียงลำดับแล้ว และเปรียบเทียบกับค่ากลางของข้อมูลและตัดช่วงข้อมูลที่ค้นหาทีละครึ่งไปเรื่อยๆ Binary Search เป็นวิธีการค้นหาที่มีประสิทธิภาพกว่า Linear Search แต่ต้องมีการเรียงข้อมูลก่อน

20 Labs Lab 13.1: จากโปรแกรม Binary Search ในตัวอย่างที่ 2 ให้ทำการแก้ไขให้โปรแกรมดังนี้ ถ้าพบข้อมูลที่ต้องการ ให้โปรแกรมแสดงข้อความว่า “Could not find element <key> in array a” ในลักษณะเดียวกับกรณีโปรแกรมในตัวอย่างที่ 1 ตัวอย่างการทำงาน

21 Labs Lab 13.2: จาก lab 13.1 ให้ทำการแก้ไขให้โปรแกรมดังนี้
เพิ่มการแสดงผลในแต่ละรอบการทำงาน โดยแสดงเครื่องหมาย ‘[]’ ล้อมรอบสมาชิกที่อยู่ในขอบเขตการค้นหาในรอบนั้น ตัวอย่างการทำงาน 1: Seach key = 19

22 Labs Lab 13.2: จาก lab 13.1 ให้ทำการแก้ไขให้โปรแกรมดังนี้
เพิ่มการแสดงผลในแต่ละรอบการทำงาน โดยแสดงเครื่องหมาย ‘[]’ ล้อมรอบสมาชิกที่อยู่ในขอบเขตการค้นหาในรอบนั้น ตัวอย่างการทำงาน 2: Seach key = 18

23 Labs Lab 13.3: Buble Sort + Binary Search ให้นศ.เขียนโปรแกรมดังต่อไปนี้ สร้างตัวแปรชุดสำหรับเก็บเลขจำนวนเต็ม 10 จำนวน ทำการสุ่มค่าระหว่าง ให้กับสมาชิกทั้ง 10 ตัว ทำการเรียงข้อมูลด้วยวิธี Bubble sort ทำการค้นหาข้อมูลที่ต้องการด้วยวิธีไบนารี (Binary search) Note: ข้อควรระวัง ค่า index จากตัวอย่างในเรื่อง Bubble sort จะเริ่มที่ 1 ในขณะนี่ค่า index จากตัวอย่างที่ใช้ในเรื่อง search จะเริ่มที่ 0 [Bubble Sort+Binary Search] จากโปรแกรม Binary Search จากข้อ 2 ให้ทำการแก้ไขดังนี้คือ ให้ทำการสร้าง array ของ int ชื่อ a โดยมีค่า random อยู่ในช่วง จำนวน 10 จำนวน แล้วทำการเรียงข้อมูล โดย Bubble Sort ก่อน แล้วให้ทำการถาม Search Key ที่ต้องการค้นหาและทำการค้นหาด้วย Binary Sort (*ข้อควรระวัง Bubble Sort จากในหัวข้อที่แล้ว จะทำงานเริ่มที่ index ของ array ที่ 1 ส่วนในหัวข้อนี้ การค้นหา Binary Search จะทำงานเริ่มจาก index ของ array ที่ 0 จึงควรระมัดระวังในการนำทั้งสองส่วนมาทำงานร่วมกัน)

24 Labs Lab 13.3: ตัวอย่างการทำงานของ Buble Sort + Binary Search
Bubble Sort Binary Search


ดาวน์โหลด ppt Week 13 Basic Algorithm 2 (Searching)

งานนำเสนอที่คล้ายกัน


Ads by Google