2.3.1 - algorithms Flashcards

1
Q

algorithm

A

A set of rules or a sequence of steps specifying how to solve a problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2 searching algorithms

A
  • linear search
  • binary search
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

linear search pseudocode

A

function LinearSearch(alist,itemSought}
index = -1
i = 0
found = False
while i < length(alist) and found = False
if alist[iI = itemSought then
index = i
found = True
endif
i = i + 1
endwhile
return index
endfunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

linear search

A

If the items are not in any particular sequence, the data items have to be searched one by one until the required one is found or the end of the list is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

binary search

A

The algorithm works by repeatedly dividing in half the portion of the SORTED data list that could contain the required data item. This is continued until there is only one item in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

binary search pseudocode

A

function binarySearch(aList, itemSought}
found = False
index = -1
first = C
last = len (aList)-1
while first <= last and found = False
midpoint = Integer part of ((first + last)/2)
if aList[midpoint) = itemSought then
found = True
index = midpoint
else
if aList(midpoint) < itemSought then
first = midpoint + l
else
last = midpoint - i
endif
endif
endwhile
return index
endfunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

bubble sort

A
  • Go through the array, comparing each item with the one next to it. If it is greater, swap them.
  • The last element of the array will be in the correct place after the first pass
  • Repeat n-2 times, reducing by one on each pass the number of elements to be examined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

bubble sort pseudocode

A

numItems = len(numbers)
for i = 0 to numItems - 2
for j = 0 to(numItems - i - 2)
if numbers [j] > numbers [j + 1]
temp = numbers[j]
numbers [ j ] = numbers[ j + 1]
numbers [j+1] = temp
endif
next j
print (numbers)
next i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

insertion sort

A

The algorithm takes one data item
from the list and places It in the correct location in the list. This process is
repeated until there are no more unsorted data items in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

insertion sort pseudocode

A

procedure inaertionSort(alist)
n = len(alist)
for index = 1 to n - 1
currentvalue = alist[index]
position = index
while position > 0 and alist[position - l| >
currentvalue
alist(position] = alist[position - 1]
position = position - l
endwhile
alist(position) = currentvalue
next index
endprocedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

average time complexity of a binary search

A

O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

average time complexity of a bubble sort

A

O(n²)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

average time complexity of an insertion sort

A

O(n²)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

average time complexity of a merge sort

A

O(n log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

best case for bubble sort

A

O(n): Items are already in order, only one pass is needed. There would be n-1 comparisons for the inner for loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

worst case for bubble sort

A

O(n²): Values in list are in reverse order. There will be n-1 passes with n-1 comparisons made in each pass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

best case for insertion sort

A

O(n): Items are already in order. Outer loop will iterate n-1 times. Condition of the inner while loop will evaluate to false so loop won’t be executed.

18
Q

worst case for insertion sort

A

O(n²): Reverse order.

19
Q

Why is the best, average and worst case time complexity for a merge sort always O(n log n)?

A

The full split and merge process is always executed.

20
Q

best case for quick sort

A

O(n log n)

21
Q

worst case for quick sort

A

O(n²): When the pivot value is in the first or last position of the current partition. This will result in one partition with 0 elements and one with all the remaining.

22
Q

worst case for binary sort

A

O(log n): If the item to be found is found in the last comparison or not at all.

23
Q

best case for binary sort

A

O(1): Item to be found is found in the first comparison.

24
Q

worst case for linear search

A

O(n): Item is found at end of list. Operation is carried out n times.

25
best case for linear search
O(1): Item to found is first item in list.
26
code to implement a static array
public const int MaxSize = 100; // Use a constant string[] stack = new string[MaxSize]; int top = -1;
27
code to push an element onto a stack
public static bool IsFull(int top) { if (top == MaxSize - 1) return true; else return false; } public static int Push(string[] stack, int top, string data) { if (IsFull(top) == true) Console.WriteLine($"\nStack is full - {data} not added"); else { top = top + 1; stack[top] = data; } return top; }
28
code to pop item from stack
FUNCTION is_empty(top) IF top == -1 THEN RETURN True ELSE RETURN False ENDIF ENDFUNCTION FUNCTION pop(stack, top) IF is_empty(top) == True THEN PRINT ("Stack is empty - nothing to pop") popped_item = Null ELSE popped_item = stack[top] top = top - 1 ENDIF RETURN (popped_item, top) ENDFUNCTION
29
code to peek stack
FUNCTION peek(stack, top) IF is_empty(top) == True THEN PRINT("Stack is empty - nothing to peek") peeked_item = Null ELSE peeked_item = stack[top] ENDIF RETURN peeked_item ENDFUNCTION
30
code to implement a linear queue
public const int MaxSize = 100; // Use a constant string[] queue = new string[MaxSize]; int front = 0; int rear = -1;
31
code to enqueue to a linear queue
FUNCTION is_full(rear) IF (rear + 1) == MAX_SIZE THEN RETURN True ELSE RETURN False ENDIF ENDFUNCTION FUNCTION enqueue(queue, rear, data) IF is_full(rear) == True THEN PRINT ("Queue is full") ELSE rear = rear + 1 queue[rear] = data ENDIF RETURN rear ENDFUNCTION
32
code to dequeue from a linear queue
FUNCTION is_empty(front, rear) IF front > rear THEN RETURN True ELSE RETURN False ENDIF ENDFUNCTION FUNCTION dequeue(queue, front, rear) IF is_empty(front, rear) == True THEN PRINT("Queue is empty - nothing to dequeue") dequeued_item = Null ELSE dequeued_item = queue[front] front = front + 1 ENDIF RETURN (dequeued_item, front) ENDFUNCTION
33
code to initialise a circular queue
public const int MaxSize = 100; // Use a constant string[] circular_queue = new string[MaxSize]; int front = -1; int rear = -1;
34
code to enqueue to a circular queue
FUNCTION is_full(front, rear) IF (rear + 1) MOD MAX_SIZE == front THEN RETURN True ELSE RETURN False ENDIF ENDFUNCTION FUNCTION enqueue(queue, front, rear, data) IF is_full(front, rear) == True THEN PRINT("Queue is full") ELSE rear = (rear + 1) MOD MAX_SIZE queue[rear] = data IF front = -1 THEN // First item to be queued front = 0 ENDIF ENDIF RETURN (front, rear) ENDFUNCTION
35
code to dequeue from a circular queue
FUNCTION is_empty(front) IF front == -1 THEN RETURN True ELSE RETURN False ENDIF ENDFUNCTION FUNCTION dequeue(queue, front, rear) IF is_empty(rear) == True THEN PRINT("Queue is empty - nothing to dequeue") dequeued_item = Null ELSE dequeued_item = queue[front] // Check if the queue is empty IF front == rear THEN front = -1 rear = -1 ELSE front = (front + 1) MOD maxsize ENDIF ENDIF RETURN (dequeued_item, front, rear) ENDFUNCTION
36
code to implement a linked list
class NodeRecord { public string data; public NodeRecord next; } // Do not intialise yet as the linked list is empty NodeRecord head;
37
code to traverse a linked list
PROCEDURE traverse(my_list) // Set the current node as the head current = my_list.head // Repeat until there are no more linked nodes WHILE current != Null PRINT(current.data) current = current.next ENDWHILE ENDPROCEDURE
38
code to add a new node to a linked list
PROCEDURE insert_at_front(my_list, data) // Create a new node new_node = NodeRecord new_node.data = data // Check if the head node exists IF my_list.head == Null THEN my_list.head = new_node ELSE // Update the pointers so the new node is the head new_node.next = my_list.head my_list.head = new_node ENDIF ENDPROCEDURE
39
code to delete a node from a linked list
PROCEDURE delete(my_list, data) // Start at the head of the list current = my_list.head // Check if the head node is to be deleted IF current.data == data THEN // Update the head pointer my_list.head = current.next ELSE // Repeat until the node has been found WHILE current.next.data != data // Change the current node to be the next node current = current.next ENDWHILE // Set the pointer to be the next node's pointer current.next = current.next.next ENDIF ENDPROCEDURE
40
code for depth-first traversal (post order)
PROCEDURE post_order_traversal(node) // Check any nodes to the left of the current node IF node.left != Null THEN post_order_traversal(node.left) ENDIF // Check any nodes to the right of the current node IF node.right != Null THEN post_order_traversal(node.right) ENDIF // Output the data of the current node PRINT(node.data) ENDPROCEDURE
41