Searches and Sorts Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the pseudocode for the binary search?

A
found = False
first = 0
last = items.Length - 1 
while (first <= last and found == false)
          midpoint = (first + last) DIV 2
          if (items[midpoint] == itemToFind then
                    found = True
          else
                    if (items[midpoint] < itemToFind then
                              first = midpoint + 1
                    else
                               last = midpoint - 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the pseudocode for the linear search?

A
found = False
index = 0
while (found == false and index < items.Length)
          if (items[index] == itemToFind
                    found = true
          else
                    index = index + 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the pseudocode for the bubble sort?

A
n = items.Length
swapped = true

while (n > 0 and swapped == true)
swapped = false
n = n - 1
for (index = 0 TO n - 1)
if (items[index] > items[index+1]
Swap(items[index], items[index+1])
swapped = true

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

What is the pseudocode for the insertion sort?

A

for (i = 1 TO items.Length)
current = items[i]
j = i

      while (j > 0 AND items[j - 1] > current
                items[j] = items[j - 1]
                j = j - 1
      end while
      items[j] = current end for
How well did you know this?
1
Not at all
2
3
4
5
Perfectly