Writing Pseudocode Algorithms Flashcards
Describe the pseudocode for a Bubble Sort
A = Array of data
Swapped = True
While Swapped = True Do
Swapped = False
for i = 0 to A.length - 1
if A[i] > A[i+1]
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
Swapped = True
End if
Next Count
End while
Return A
End function
Describe the pseudocode for an Insertion Sort
A = Array of data
for i = 0 to A.length - 1
currentData = A[i]
position = i
while position > 0 AND A[position - 1] > currentData
temp = A[position]
A[position] = A[position - 1]
A[position - 1] = temp
End While
A[position] = currentData
Next Count
return A
end function
Describe the pseudocode for a Linear Search
A = Array of data
x = Desired element
for i = 0 to A.length - 1
if A[i] == x then
position = i
return position
break
else
return “Position not found”
End if
next count
end function
Describe the pseudocode for a Binary Search
A = Array of data
x = Desired element
low = 0
high = A.length -1
while low <= high
mid = (low + high) / 2
if A[mid] == x
return mid
else if A[mid] > x
high = mid -1
else
low = mid + 1
endif
endwhile
return “Not found in data”
end function
Describe the Stack Operations and Names
Describe the Queue Operations and Names
Describe the pseudocode for a stack isEmpty
if(top == -1)
return true
else
return false
end if
end function
Describe the pseudocode for a stack isFull
if top = maxSize then
return True
else
return False
end if
end function
Describe the pseudocode for a stack push
If isFull() then
Print “The stack is full”
Else
top = top + 1
arrStack[top]=newValue
End if
End function
Describe the pseudocode for a stack pop
if isEmpty() then
Print “The stack is empty”
else
item = arrStack[top]
top = top -1
Return item
End if
End function
Describe the pseudocode for a stack peek
if isEmpty()
Print “Stack is empty”
Else
return arrStack[top]
endif
end function
Describe the pseudocode for a Queue Enqueue
if(isFull() then
Print “The queue is full”
Else
rear = (rear+1) % maxSize
arrQueue[rear] = newValue
size = size + 1
Endif
End function
Describe the pseudocode for a Queue Dequeue
int item
if(isEmpty()) then
Print “The queue is empty”
Else
item = arrQueue[front]
front = (front+1) % maxSize
size = size -1
Endif
End function
Explain the advantages of writing an application using a modular approach
- Work is easier to divide between a team
- Each team member just needs to know what values go into their subroutine and the expected functionality
- Saves time as work takes place in parallel
- Easier to test/ debug/ read
- Each subroutine can be tested before being passed on
- Code can be reused in the project/ future projects
What is a Stack
-A data structure that operates on a first in last out basis