Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete
  • The binary search is a more efficient search method than linear search

  • The binary search compares the middle item to the searched for target item. If the values match then the index is returned. If not then the list is adjusted such that:

    •  the top half is ignored if the target item is less than the middle value

    • or the bottom half is ignored if the target item is larger than the middle value

  • Once the list has been adjusted, the search continues until the item is found or the item is not in the list

  • It is important to note that the list must be sorted for the binary search to work correctly

  • The implementation of the binary search is shown below

figure-2-performing-the-binary-search-revision-notes-computer-science

Figure 2: Performing the Binary Search

Examiner Tips and Tricks

  • When determining the rounding of the midpoint, rounding up or down are both valid provided consistent use of the same rounding is used throughout the algorithm

  • It is important to note that the binary search does not discard, delete or remove parts of the list, it only adjusts the start, end and mid pointers. This only gives the appearance that items have been discarded or deleted

  • The binary search is an example of a logarithmic time complexity O(logn) as it halves the search space with each iteration of the while loop. This approach is known as divide and conquer, where a problem is progressively reduced in size such that when each problem is at its smallest it is easiest to solve

  • The algorithm starts with n items before the first iteration. After the first there are n/2 items, after the second there are n/4 items, after the third there are n/8 items and so on, leading to n/2i after i iterations

  • The worst case scenario or maximum number of iterations to find one item is where n/2i = 1

    • Multiply both sides by 2i to get: n = 2i

    • Take the logarithm of both sides: logn = log2i

    • Rewrite with i at the front (using laws of logarithms): logn = ilog2

    • Log2 (assuming a base of 2 is used) becomes 1, giving: logn = i

    • The binary search is therefore O(logn)

  • The best case scenario would be O(1) where the item is found on the first comparison, while the average case would be O(logn/2) which would find the item somewhere in the middle of the search. Removing coefficients means the average case would therefore still be O(logn)

  • As the binary search requires no additional space, it is space complexity O(n), where n is the number of items in the list

  • Given the following list [3, 5, 9, 10, 14, 16, 17, 21, 24, 26, 28, 30, 42, 44, 50, 51], a trace table for the binary search is shown below

Trace Table for the Binary Search

item

index

found

start

end

mid

list[mid]

21

-1

False

0

15

8

24

 

 

 

0

7

4

14

 

 

 

5

7

6

17

21

7

True

7

7

7

21

Pseudocode

FUNCTION binarySearch(list : ARRAY OF INTEGER, item : INTEGER) RETURNS INTEGER DECLARE found : BOOLEAN DECLARE index : INTEGER DECLARE start : INTEGER DECLARE end : INTEGER DECLARE mid : INTEGER found ← FALSE index ← -1 start ← 0 end ← LENGTH(list) - 1 WHILE start <= end AND found = FALSE mid ← (start + end) DIV 2 IF list[mid] = item THEN found ← TRUE index ← mid ELSE IF list[mid] < item THEN start ← mid + 1 ELSE end ← mid - 1 ENDIF ENDIF ENDWHILE RETURN index
ENDFUNCTION
  • Function definition:
    FUNCTION binarySearch(list : ARRAY OF INTEGER, item : INTEGER) RETURNS INTEGER

    • The function searches a sorted array for a given item.

    • It returns the index of the item if found, or -1 if not found.

  • Variable declarations:

    • found → a flag to indicate if the item is found (initially FALSE)

    • index → the result to be returned (initially -1)

    • start → index of the start of the current search range (initially 0)

    • end → index of the end of the current search range (initially LENGTH(list) - 1)

    • mid → index of the middle element in the current range

  • Loop condition:
    WHILE start <= end AND found = FALSE

    • Keep searching while the search range is valid and the item hasn’t been found.

  • Middle element calculation:
    mid ← (start + end) DIV 2

    • Calculates the index of the middle element of the current search range.

  • Check for match:
    IF list[mid] = item THEN

    • If the middle element is equal to the target item:

      • Set found ← TRUE

      • Store the index ← mid

  • Adjust search range:

    • If the middle value is less than the item:

      • Search the right halfstart ← mid + 1

    • Otherwise:

      • Search the left halfend ← mid - 1

  • Loop ends:

    • The loop stops when the item is found or the search range becomes invalid.

  • Return value:
    RETURN index

    • Returns the index if found, or -1 if not found

Python

def binary_search(list, item): found = False index = -1 start = 0 end = len(list) - 1 while start <= end and not found: mid = (start + end) // 2 if list[mid] == item: found = True index = mid elif list[mid] < item: start = mid + 1 else: end = mid - 1 return index

Java

public class BinarySearch { public static int binarySearch(int[] list, int item) { boolean found = false; int index = -1; int start = 0; int end = list.length - 1; while (start <= end && !found) { // Corrected found condition int mid = (start + end) / 2; if (list[mid] == item) { found = true; index = mid; } else if (list[mid] < item) { start = mid + 1; } else { end = mid - 1; } } return index; }

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注