Section 12 Algorithms Flashcards

1
Q

linear search

A

traverses through every item 1 at a time until item is found

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

Binary search algorithm

A

Divide and conquer so list is split into smaller lists until it finds the item it’s searching for

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

Bubble sort algorithm

A

Passes through the list evaluating item pairs and ensuring the larger value is above the smaller value

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

size()

A

Returns the number of elements on the stack
Returns the value of the top pointer plus one

size()
return top + 1

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

isEmpty()

A

● Returns True if the stack is empty, otherwise returns False
● Works by checking whether the top pointer is less than 0

isEmpty()
if top < 0:
return True
else:
return False
endif

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

peek()

A

● Returns the item at the top of the stack, without removing it
● Returns the item at the position indicated by the top pointer
● Important to check that the stack has data in it before attempting to return anything
peek()
if isEmpty():
return error
else:
return A[top]
endif

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

push(element)

A

● Adds an item to a stack
● The new item must be passed as a parameter
● Firstly, the top pointer is updated accordingly
● Then the new element can be inserted at the position of the top pointer

push(element)
top += 1
A[top] = element

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

pop()

A

● Removes an item from a stack
● Element at the position of the top pointer is recorded before being removed
● Top pointer decremented by one
● The removed item is returned
● As with peek()​, it’s important to first check that the stack isn’t empty

pop()
if isEmpty():
return error
else:
toRemove = A[top]
A[top] = “”
top -= 1
return toRemove
endif

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

size()of queue

A

● Returns the number of elements in a queue
● Simply subtracts the value of front from back

size()
return back - front

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

isEmpty()
queue

A

● Returns True if a queue is empty, and False otherwise
● When a queue is empty, front and back point to the same position.

isEmpty()
if front == back:
return True
else:
return False
endif

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

peek()

A

peek()
● Returns the element at the front of the queue without removing it

peek()
return A[front]

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

enqueue(element)

A

● Adds an element to the back of a queue
● The new element is placed in the position of back
● Back is incremented by one

enqueue(element)
A[back] = element
back += 1

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

dequeue()

A

● Removes the item at the front of the queue
● Items are removed from a queue from the position of the front pointer
● Just as with stacks, it’s important to check that the queue isn’t empty
● After the element has been removed, the front pointer must be incremented

dequeue()
if isEmpty():
return error
else:
toDequeue = A[front]
A[front] = “”
front += 1
return toDequeue
endif

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

Describe bubble sort

A

Makes comparisons and swaps between pairs of elements
● The largest element in the unsorted part of the input is said to “bubble” to the top of
the data with each iteration of the algorithm
○ Starts at the first element in an array and compares it to the second
○ If they are in the wrong order, the algorithm swaps the pair
○ The process is then repeated for every adjacent pair of elements in the array,
until the end of the array is reached
● This is one pass of the algorithm
● For an array with n elements, the algorithm will perform n passes through the data
● After n passes, the input is sorted and can be returned

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

Bubble sort algorithm

A

A = ​Array of data
for i = 0 to A.length - 1:
for j = 0 to A.length - 2:
if A[j] > A[j+1]:
swap A[j] and A[j+1]
return A

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

Modify bubble sort for improved efficiency

A

● Can be modified to improve efficiency
● A flag recording whether a swap has occurred is introduced
● If a full pass is made without any swaps, then the algorithm terminates
● With each pass, one fewer element needs comparing as the n largest elements are
in position after the nth pass

17
Q

Describe insertion sort

A

● Places elements into a sorted sequence
● In the i​th iteration of the algorithm the first i​ elements of the array are sorted
○ Warning: although the i​ elements are sorted, they are not the i​smallest
elements in the input!
● Stars at the second element in the input, and compares it to the element to its left
● When compared, elements are inserted into the correct position in the sorted
portion of the input to their left
● This continues until the last element is inserted into the correct position, resulting in
a fully sorted array

18
Q

Insertion sort algorithm

A

A = ​Array of data
for i = 1 to A.length - 1:
elem = A[i]
j = i - 1
while j > 0 and A[j] > elem:
A[j+1] = A[j]
j = j - 1
A[j+1] = elem

19
Q

Binary search algorithm

A

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”

20
Q

Linear Search

A

● The most basic searching algorithm
● Looks at elements one at a time until the desired element is found
● Doesn’t require the data to be sorted
● A great deal of pot luck, but easy to implement
○ Sometimes gets lucky and finds the desired element almost immediately
○ In other situations, the algorithm is incredibly inefficient

21
Q

Linear search algorithm

A

A ​= Array of data
x ​= Desired element

i = 0
while i < A.length:
if A[i] == x:
return i
else:
i = i + 1
endif
endwhile
return “Not found in data”