Types of Algorithms Flashcards
Features of a bubble sort
- Uses an incremental approach
- Slow and inefficient
- Not for large lists
- Simple to understand
n = items.length
swapped = True
while n > 0 AND swapped - False
n = n - 1
for index = 0 to n - 1
if items [index] > items [index + 1] then
swap (items[index], items[index+1])
swapped = True
end if
end for
end while
Bubble Sort Psuedocode
Features of an insertion sort
- Uses an incremental approach
for index = 1 to items.length:
current = items[index]
index2 = index
while index2 > 0 AND items[index2 - 1] > current
items[index2] = items[index2 - 1]
index2 = index2 = 1
end while
items[index2] = current
end for
Insertion Sort Pseudocode
Features of a linear search
- Start at the beginning of the list
- Compare search value to 1st item
- Can be an unordered list
found = False
index = 0
while found == False AND index < items.length
if items[index] == item_to_find then
found = True
else
index = index + 1
end if
end while
Linear Search Pseudocode
Features of binary search
- List must be ordered
- Split list in half
found = False
first = 0
last = items.length - 1
while first <= last AND found == False
midpoint = (first + last) DIV 2
if items[midpoint] == item_to_find then
found = True
else
if items[midpoint] < item_to_find then
first = midpoint + 1
else
last = midpoint - 1
end if
end if
end while
Binary Search Pseudocode