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

Searching arrays

  • A linear search starts with the first value in a dataset and checks every value one at a time until all values have been checked

  • A linear search can be performed even if the values are not in order

Step

Instruction

1

Check the first value

2

IF it is the value you are looking for

  • STOP!

3

ELSE move to the next value and check

4

REPEAT UNTIL you have checked all values and not found the value you are looking for

  • A linear search can be used to find elements in an array

  • Each element in the array is checked in order, from the lower bound to the upper bound, until the item is found or the upper bound is reached

  • An example pseudocode algorithm to perform a linear search on a 1D array could be:

DECLARE List : ARRAY[0:4] OF INTEGER
DECLARE Target : INTEGER
DECLARE Found : BOOLEAN
DECLARE Index : INTEGER // Initialise array values
List[0] ← 12
List[1] ← 25
List[2] ← 37
List[3] ← 42
List[4] ← 56 // Input the value to search for
OUTPUT "Enter the value to search for:"
INPUT Target Found ← FALSE
Index ← 0 WHILE Index <= 4 AND Found = FALSE DO IF List[Index] = Target THEN Found ← TRUE ELSE Index ← Index + 1 ENDIF
ENDWHILE IF Found = TRUE THEN OUTPUT "Item found at index ", Index
ELSE OUTPUT "Item not found"
ENDIF
  • Works on a fixed-size array List[0:4]

  • Uses a WHILE loop for flexibility (early exit if found)

  • Clearly separates input, processing, and output

Identifier table

Identifier

Data type

Description

List

ARRAY[0:4] OF INTEGER

Stores the list of numbers to search through

Target

INTEGER

The number the user wants to search for

Found

BOOLEAN

Tracks whether the target value has been found

Index

INTEGER

The current position being checked in the array

Sorting arrays

What is a bubble sort?

  • A bubble sort is a simple sorting algorithm that starts at the beginning of a dataset and checks values in ‘pairs’ and swaps them if they are not in the correct order

  • One full run of comparisons from beginning to end is called a ‘pass‘, a bubble sort may require multiple passes‘ to sort the dataset

  • The algorithm is finished when there are no more swaps to make

How do you perform a bubble sort?

Step

Instruction

1

Compare the first two values in the dataset

2

IF they are in the wrong order…

  • Swap them

3

Compare the next two values

4

REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)

5

IF you have made any swaps…

  • REPEAT from the start (pass 2,3,4…)

6

ELSE you have not made any swaps…

  • STOP! the list is in the correct order

Example

  • Perform a bubble sort on the following dataset

5

2

4

1

6

3

Step

Instruction

1

Compare the first two values in the dataset

5

2

4

1

6

3

2

IF they are in the wrong order…

  • Swap them

2

5

4

1

6

3

3

Compare the next two values

2

5

4

1

6

3

4

REPEAT step 2 & 3 until you reach the end of the dataset

  • 5 & 4 SWAP!

2

4

5

1

6

3

  • 5 & 1 SWAP!

2

4

1

5

6

3

  • 5 & 6 NO SWAP!

2

4

1

5

6

3

  • 6 & 3 SWAP!

2

4

1

5

3

6

  • End of pass 1

5

IF you have made any swaps…

  • REPEAT from the start

  • End of pass 2 (swaps made)

2

1

4

3

5

6

  • End of pass 3 (swaps made)

1

2

3

4

5

6

  • End of pass 4 (no swaps)

1

2

3

4

5

6

6

ELSE you have not made any swaps…

  • STOP! the list is in the correct order

  • A bubble sort can be used to put elements in an array in order

  • A pseudocode algorithm for a bubble sort on a 1D array could be:

DECLARE Numbers : ARRAY[0:5] OF INTEGER
DECLARE Temp : INTEGER
DECLARE Pass : INTEGER
DECLARE Index : INTEGER
DECLARE Swapped : BOOLEAN // Initialise the array
Numbers[0] ← 5
Numbers[1] ← 2
Numbers[2] ← 4
Numbers[3] ← 1
Numbers[4] ← 6
Numbers[5] ← 3 // Bubble sort algorithm
FOR Pass ← 1 TO 5 Swapped ← FALSE FOR Index ← 0 TO 4 IF Numbers[Index] > Numbers[Index + 1] THEN Temp ← Numbers[Index] Numbers[Index] ← Numbers[Index + 1] Numbers[Index + 1] ← Temp Swapped ← TRUE ENDIF NEXT Index IF Swapped = FALSE THEN // List is already sorted EXIT ENDIF
NEXT Pass // Output the sorted array
FOR Index ← 0 TO 5 OUTPUT Numbers[Index]
NEXT Index
  • Uses a fixed array of size 6

  • Includes an optimisation to exit early if no swaps were made on a pass

Identifier table

Identifier

Data type

Description

Numbers

ARRAY[0:5] OF INTEGER

Stores the list of numbers to be sorted

Temp

INTEGER

Temporary variable used for swapping two elements in the array

Pass

INTEGER

Tracks the number of passes through the array

Index

INTEGER

Tracks the current position being compared in the array

Swapped

BOOLEAN

Indicates whether a swap occurred during a pass (used to optimise sorting)

Responses

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