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 linear search is a standard algorithm used to find elements in an unordered list

  • The list is searched sequentially and systematically from the start to the end one element at a time

  • Comparing each element to the value being searched for

    • If the value is found the algorithm outputs where it was found in the list

    • If the value is not found it outputs a message stating it is not in the list

  • An example of using a linear search would be looking for a specific student name in a list or searching for a supermarket item in a shopping list

figure-1--performing-the-linear-search-revision-notes-computer-science

Figure 1: Performing the Linear Search

  • To determine the algorithm’s execution time as measured by the number of instructions or steps carried out, Big-O notation must be used

  • The loop is executed n times, once for each item. There are two instructions within the loop, the if statement and an assignment, hence the Big-O for the loop is O(2n)

  • There are three initial statements which are O(1)

  • The overall Big-O notation is therefore 2n + 3, which when coefficients are factored out, becomes O(n) as linear time dominates constant time. The constant term and coefficients contribute significantly less as the input size n grows larger

  • It is important to remember here that O(n) is the worst case scenario. The best case scenario O(1) would find the item the first time, while the average case would be O(n/2) which when we remove coefficients still becomes O(n)

  • As the linear 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 [5, 4, 7, 1, 3, 8, 9, 2], a trace table for the linear search is shown below

Trace Table of the Linear Search

item

index

i

list[i]

found

8

-1

0

5

False

 

 

1

4

 

 

 

2

5

 

 

 

3

1

 

 

 

4

3

 

8

5

5

8

True

Pseudocode

FUNCTION linearSearch(list : ARRAY OF STRING, item : STRING) RETURNS INTEGER DECLARE index : INTEGER DECLARE i : INTEGER DECLARE found : BOOLEAN index ← -1 i ← 0 found ← FALSE WHILE i < LENGTH(list) AND found = FALSE IF list[i] = item THEN index ← i found ← TRUE ENDIF i ← i + 1 ENDWHILE RETURN index
ENDFUNCTION
  • Function definition:
    FUNCTION linearSearch(list : ARRAY OF STRING, item : STRING) RETURNS INTEGER

    • The function takes a list and a target item.

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

  • Variable declarations:
    index, i, and found are declared:

    • index stores the result (default is -1 if not found).

    • i is the loop counter.

    • found is a boolean used to stop the search once the item is found.

  • Initialisation:

    • index ← -1 → start with “not found”

    • i ← 0 → start from the first index of the list

    • found ← FALSE → the item hasn’t been found yet

  • Loop condition:
    WHILE i < LENGTH(list) AND found = FALSE

    • Continue looping while there are items left to check and the item hasn’t been found.

  • Comparison inside loop:
    IF list[i] = item THEN

    • Check if the current element equals the search item.

  • If a match is found:

    • index ← i → save the current index

    • found ← TRUE → stop searching

  • Increment:

    • i ← i + 1 → move to the next item

  • End of loop:

    • Once the loop finishes (either item is found or end of list is reached), return the result.

  • Return value:
    RETURN index

    • Returns the index where the item was found.

    • If it wasn’t found, it returns -1.

Python

def linear_search(list, item): index = -1 for i in range(len(list)): if list[i] == item: index = i break # Stop the loop once the item is found return index

Java

public class LinearSearch { public static int linearSearch(int[] list, int item) { int index = -1; for (int i = 0; i < list.length; i++) { if (list[i] == item) { index = i; break; // Stop the loop once the item is found } } return index; }

Responses

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