Pick an answer to check
HCI 8. Searching and Sorting Algorithms (2022)
Uploaded by adrianwang2003 · 28 May 2024
Preview
Text from the first pagesHwa Chong Institution H2 Computing 1 8 Searching and Sorting Algorithms Learning Outcome In the last chapter, we learnt t o search and sort a list using the methods provided in Python. Here, we will look closer into the various algorithms, explaini ng how the search and sort are done behind the scene and how efficient the algorithms are. 8.1 Searching Algorithms A searching algorithm intends to find a particular element in a list. This targeted element may not exist in the list, may appear once or multiple times. We call this targeted element the key. 8.1.1 Linear Search As the name implies, linear search basically searches the items in the list one-by-one. The function LinearSearch requires two parameters: the list and the key. The algorithms begins at index 0, scans every element in the list until the key is found or the list is exhausted. If the key is found, the function returns the index of the matched item in the list; otherwise, the value −1 is returned. Here are two examples for illustration. A: 8 3 6 2 6 1. key = 6 Search the list from the beginning, returning the index of the first occurrence of element 6. key = 6 A[0] A[1] A[2] A[3] A[4] return value = 2 8 3 6 2 6 Fundamental Algorithms Implement sort algorithms: insertion sort, bubble sort, quicksort, merge sort Use examples to explain sort algorithms Implement search algorithms: linear search, binary search, hash table search Use examples to explain search algorithms Compare and describe the efficiencies of the sort and search al gorithms using Big-O notation for time complexity (worst case). Exclude: space complexity Implementing Algorithms Implement sort programs: insertion sort, bubble sort, quicksort, merge sort Implement search programs: linear search, binary search, hash table search
Hwa Chong Institution H2 Computing 2 def LinearSearch (A, key): #Search the list A for a match with key #Return the position of the key if found, or -1 otherwise. pos = 0 # start position to search found = False while ( not found and pos < len(A) ): if A[pos] == key: found = True else: pos = pos + 1 if found: # return index of matching item return pos else: # search failed, return -1 return -1 2. key = 9 , start = 0, n = 5. Start at the first element and search the list for the number 9. Since it is not found, return the value −1. key = 9 n = 5 A[0] A[1] A[2] A[3] A[4] return value = −1 Linear Search Implementation 8.1.2 Binary Search The linear search applies to any list. If the list is ordered, an algorithm, called the binary search, provides an improved search technique. Your experience in looking up a number in a large phone directory is a model for the algorithm. Given the name, you move to an earlier or later page in the pho ne book depending on the relative location of the person’s name in the alphabet. For instance, if the person’s name begin with “R”, and you are in the “T’s” you move to an earlier page. The process continues until you get a match or discover that the name is not in the book. A related idea applies to searching an ordered list. We go to the middle of the list and look for a quick match of our key with the midpoint value. If we fail to find a match, we look at the relative size of the key and the midpoint value and then move to the lower or upper half of the list. In general, if we know how the data are ordered, we can u se that information to shorten the search time. 8 3 6 2 6
Hwa Chong Institution H2 Computing 3 The following steps describe the algorithm. The indices at the ends of the list are low = 0 and high = n − 1 , where n is the number of elements in the list. 1. Compute the index of the array’s midpoint. mid = ( low + high ) / 2 2. Compare the value at this midpoint with the key. If a match occurs, return the index mid to locate the key. key low mid high If ( A[ mid] = = key ) r e t u r n mid If key < A[ mid], the key must lie within the lower range (left half) since th e list is ordered. The new boundaries are low and high = mid − 1. key low mid high Search Lower Range, index from low to mid − 1 If key > A[mid], the key must lie within the upper range (right half) since t he list is ordered. The new boundaries are low = mid + 1 and high. key low mid high Search Upper Range, index from mid + 1 to high 3. The algorithm refines the location of a match by halving the le ngth of the interval in which key can exist and then executing the same search algorithm on the smaller sublist. 4. Eventually, if the key is not in the list, low will e xc ee d high and the algorithm returns the failure indicator of −1 ( match not found ).
Hwa Chong Institution H2 Computing 4 Let’s illustrate with an array A and search for the key = 33. 0 1 2 3 4 5 6 7 8 A −7 3 5 8 12 16 23 33 55 A −7 3 5 8 12 16 23 33 55 low = 0 high = 8 0 1 2 3 4 5 6 7 8 mid mid = (0+8)/2 = 4 33 > A[mid] A −7 3 5 8 12 16 23 33 55 low = 5 high = 8 0 1 2 3 4 5 6 7 8 mid mid = (5+8)/2 = 6 33 > A[mid] A −7 3 5 8 12 16 23 33 55 low = 7 high = 8 0 1 2 3 4 5 6 7 8 mid mid = (7+8)/2 = 7 33=A[mid] Match! Note that binary search requires 3 comparisons while linear search would take 8 comparisons. Another example for the same array but search for key = 34. The first two comparison will be exactly the same with the above example. Let’s look at the last comparison A −7 3 5 8 12 16 23 33 55 low = 7 high = 8 0 1 2 3 4 5 6 7 8 mid mid = (7+8)/2 = 7 34 > A[mid] A −7 3 5 8 12 16 23 33 55 low = 8 high = 8 0 1 2 3 4 5 6 7 8 mid = (8+8)/2 = 8 34 < A[mid] Now we have key < A[mid] and this leads to high = mid − 1 = 7. Here low exceeds high and the algorithm will return the failure indicator of −1 ( match not found ). Note that binary search requires 4 comparisons while linear search would take 9 comparisons.
Hwa Chong Institution H2 Computing 5 Binary Search Implementation 8.1.3 Hash Table Search In an array or list, items are inserted subsequently into the data structure, starting from the first index. Linear search and binary search compares the targeted it em by a sequence of trial-and- error comparisons. Here we introdu
Content continues in the PDF. Download PDF
Newest in H2 Computing
- computing 2026 jpjc prelim p1 ansSchool answers
- computing 2026 hci prelim p2 qnQuestion paper
- computing 2026 hci prelim p1 qnQuestion paper
- computing 2026 dhs prelim p2 qnQuestion paper
- computing 2026 dhs prelim p1 qnQuestion paper
- computing 2026 asrjc prelim p2 qnQuestion paper
- computing 2026 asrjc prelim p1 qnQuestion paper
- computing 2026 acjc prelim p2 qnQuestion paper
- computing 2026 acjc prelim p1 qnQuestion paper
- NYJC 2026 Prelim P2Question paper
- NYJC 2026 Prelim P1Question paper
- DHS 2026 Y6 H2 Computing Prelim Paper 2 finalQuestion paper
