Fundamental Array Algorithms: Summation, Decision, Selection, Linear Search, Counting, and Maximum Selection

Overview of Basic Algorithms and Pseudocode Notation

  • Array Representation:
    • The sequence or collection of elements is represented by array XX, where X=to¨mbX = \text{tömb} (array).
    • The array elements are indexed starting at index 11, denoted as X(I)X(I) for element at index II
  • General Variable Definitions:
    • NN: Total number of elements in array XX
    • II: Index counter used in iteration loops
    • T(X(I))T(X(I)): A logical property or boolean predicate tested on array element X(I)X(I)

Sequence Calculation: Summation (Sorozatszámítás - Összegzés)

  • Objective:
    • Calculate the sum (SS) of NN numerical elements contained within array XX
  • Procedure Header:
    • Összegzés(N, X, S)
  • Input Parameters:
    • NN: Total number of elements
    • XX: Array containing the numbers to be summed
  • Output Parameter:
    • SS: Accumulated total sum
  • Detailed Algorithmic Steps:
    • Initialization: Set the accumulator variable S:=0S := 0
    • Loop Execution: Ciklus I = 1-től N-ig (Loop variable II runs from 11 to NN)
    • Accumulation Step: S:=S+X(I)S := S + X(I)
    • Loop Termination: Ciklus vége
    • Procedure Exit: Eljárás vége
  • Key Characteristics:
    • Operates sequentially across every element in the array from index 11 to index NN

Decision Algorithm (Eldöntés)

Handwritten algorithm notes on Summation, Decision, and Selection

  • Objective:
    • Determine whether there exists (∃\exists) at least one element in array XX of size NN that satisfies condition/property T(X(I))T(X(I))
  • Procedure Header:
    • Eldöntés(N, X, VAN)
  • Input Parameters:
    • NN: Total element count
    • XX: Array of elements
  • Output Parameter:
    • VANVAN: Logical boolean variable indicating existence (true if at least one matching element exists, false otherwise)
  • Detailed Algorithmic Steps:
    • Initialization: Set starting index I:=1I := 1
    • Loop Execution: Ciklus amíg I <= N és nem T(X(I)) (Loop while I≤NI \le N and property T(X(I))T(X(I)) is not met)
    • Index Increment: I:=I+1I := I + 1
    • Loop Termination: Ciklus vége
    • Evaluation: VAN:=(I≤N)VAN := (I \le N)
    • Procedure Exit: Eljárás vége
  • Logic Analysis:
    • If a matching element is found, the loop terminates early with I≤NI \le N, yielding VAN=trueVAN = \text{true}
    • If no element matches, the loop finishes with I=N+1I = N + 1, resulting in VAN=falseVAN = \text{false}

Selection Algorithm (Kiválasztás)

  • Objective:
    • Locate the exact ordinal index (SORSZSORSZ) of the first element satisfying property T(X(I))T(X(I)) under the specific precondition that such an element is guaranteed to exist (∃ element\exists \text{ element}) in array XX
  • Procedure Header:
    • Kiválasztás(N, X, SORSZ)
  • Input Parameters:
    • NN: Total element count
    • XX: Array of elements
  • Output Parameter:
    • SORSZSORSZ: Index position of the first matching element
  • Detailed Algorithmic Steps:
    • Initialization: Set starting index I:=1I := 1
    • Loop Execution: Ciklus amíg nem T(X(I)) (Loop while property T(X(I))T(X(I)) is false)
    • Index Increment: I:=I+1I := I + 1
    • Loop Termination: Ciklus vége
    • Index Assignment: SORSZ:=ISORSZ := I
    • Procedure Exit: Eljárás vége
  • Precondition Caveat:
    • Because existence is guaranteed beforehand, the loop condition does not require an explicit upper bound check I≤NI \le N

Linear Search Algorithm (Lineáris Keresés)

Handwritten algorithm notes on Linear Search, Counting, and Maximum Selection

  • Objective:
    • Search array XX of size NN for an element satisfying property T(X(I))T(X(I)) when existence is unknown beforehand. It returns both an existence status (VANVAN) and the index position (SORSZSORSZ) if found
  • Procedure Header:
    • Keresés(N, X, VAN, SORSZ)
  • Input Parameters:
    • NN: Number of array elements
    • XX: Array of elements
  • Output Parameters:
    • VANVAN: Boolean indicating whether a matching element was found
    • SORSZSORSZ: Ordinal index of the matching element (assigned conditionally)
  • Detailed Algorithmic Steps:
    • Initialization: Set starting index I:=1I := 1
    • Loop Execution: Ciklus amíg I <= N és nem T(X(I)) (Loop while index I≤NI \le N and property T(X(I))T(X(I)) is false)
    • Index Increment: I:=I+1I := I + 1
    • Loop Termination: Ciklus vége
    • Existence Check: VAN:=(I≤N)VAN := (I \le N)
    • Conditional Assignment: Ha VAN akkor SORSZ := I (If VANVAN is true, set SORSZ:=ISORSZ := I)
    • Procedure Exit: Eljárás vége

Counting Algorithm (Megszámolás)

  • Objective:
    • Count the total quantity (DBDB) of elements within an array XX of size NN that satisfy a specified property T(X(I))T(X(I))
  • Procedure Header:
    • Megszámolás(N, X, DB)
  • Input Parameters:
    • NN: Total number of array elements
    • XX: Array of elements
  • Output Parameter:
    • DBDB: Count of elements satisfying property T(X(I))T(X(I))
  • Detailed Algorithmic Steps:
    • Initialization: Set counter variable DB:=0DB := 0
    • Loop Execution: Ciklus I = 1-től N-ig (Loop index II runs from 11 to NN)
    • Conditional Counter Increment: Ha T(X(I)) akkor DB := DB + 1 (If element at II satisfies TT, increment DBDB by 11)
    • Loop Termination: Ciklus vége
    • Procedure Exit: Eljárás vége

Maximum Selection Algorithm (Maximumkiválasztás)

  • Objective:
    • Identify both the position (MAXMAX) and value (MAXEˊRTMAXÉRT) of the maximum element in an array XX containing NN elements
  • Procedure Header:
    • Maximumkiválasztás(N, X, MAX, MAXÉRT)
  • Input Parameters:
    • NN: Number of array elements
    • XX: Array of elements
  • Output Parameters:
    • MAXMAX: Ordinal index position of the largest element
    • MAXEˊRTMAXÉRT: Value of the largest element
  • Detailed Algorithmic Steps:
    • Initial Assumptions:
    • Assume the first element is the maximum: MAX:=1MAX := 1
    • Store first element's value: MAXEˊRT:=X(1)MAXÉRT := X(1)
    • Loop Execution: Ciklus I = 2-től N-ig (Loop index II iterates from 22 to NN)
    • Comparison and Update: Ha MAXÉRT < X(I) akkor MAX := I : MAXÉRT := X(I) (If element X(I)X(I) exceeds MAXEˊRTMAXÉRT, update index MAX:=IMAX := I and new max value MAXEˊRT:=X(I)MAXÉRT := X(I))
    • Loop Termination: Ciklus vége
    • Procedure Exit: Eljárás vége