Algorithms

Cards (2)

  • Pseudocode for Binary Search

    function binary_search(list, target):
    left = 0
    right = length(list) - 1
    while left <= right:
    mid = (left + right) // 2
    if list[mid] == target:
    return mid
    elif list[mid] < target:
    left = mid + 1
    else:
    right = mid - 1
    return -1
  • Pseudocode for Linear search
    find = 2
    found = False
    length = list.length
    counter = 0
    while found == False and counter < length
    if list[counter] == find then
    found = True
    print ('Found at position', counter)
    else:
    counter = counter + 1
    endif
    endwhile
    if found == False then
    print('Item not found')
    endif