algorithms

Cards (8)

  • Write the pseudocode for the Insertion Sort algorithm

    function insertionSort(data)
    for i = 1 to data.length -1
    temp = data[i]
    j = i
    while (j > 0 and data[j-1] > temp)
    data[j] = data[j-1]
    j = j - 1
    endwhile
    data[j] = temp
    endfor
    return data
    endfunction
  • Write the pseudocode for the linear search algorithm
    function linearSearch (data, search)
    int rtnValue = -1
    for i = 0 to data.length - 1
    if data[i] == search
    rtnValue = i
    break
    endif
    endfor
    return rtnValue
    endfunction
  • Write the pseudocode for the binary search algorithm
    function binarySearch(data, search)
    rtnValue = -1
    lower = 0
    upper = data.length - 1
    while (lower <= upper AND NOT found)
    mid = (upper + lower) / 2
    if data[mid] == search then
    rtnValue = mid
    else if data[mid] < search then
    lower = mid + 1
    else
    upper = mid - 1
    endif
    endwhile
    return rtnValue
    endfunction
  • Write the pseudocode for the bubble sort algorithm
    function bubbleSort(data)
    j = data.length - 2
    swapped = true
    while(swapped)
    swapped = false
    for i = 0 to j
    if data[i] > data[i+1]
    temp = data[i]
    data[i] = data[i+1]
    data[i+1] = temp
    swapped = true
    endif
    endfor
    j = j - 1
    endwhile
    return data
    endfunction
  • Write the pseudocode for a push algorithm
    procedure push()
    if top == MAXSIZE then
    print("Stack is full")
    else
    top = top + 1
    arrStack[top] = value
    endif
    endprocedure
  • Write the pseudocode for a pop algorithm
    procedure pop()
    if top == 0 then
    print("Stack is empty")
    else
    arrStack[top] = value
    top = top - 1
    endif
    return value
    endfunction
  • Write the pseudocode for an enqueue algorithm
    procedure enqueue()
    if size == MAXSIZE then
    print("Queue is full")
    else
    size = size + 1
    rear = (rear + 1) MOD MAXSIZE
    arrQueue[rear] = value
    endif
    endprocedure
  • Write the pseudocode for a dequeue algorithm
    function dequeue()
    int rtnValue = -1
    if size == 0 then
    print("Queue is empty")
    else
    rtnValue = arrQueue[front]
    front = front + 1
    size = size - 1
    endif
    return rtnValue
    endfunction