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