SLR 25 - Algorithms 1 Flashcards

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

What are some of the techniques and methods you could use to help you design and write algorithms?

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

What are the two standard sorting algorithms you need to know?

A
  • Bubble sort
  • Insertion sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two standard searching algorithms you need to know?

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

How does a linear search work?

A

The program will go through every item in a list in order checking if its the item youre looking for

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

What is the code for a linear search?

A

found = False
index = 0
While found == False and index < len(items):
if items[index] == item_to_find:
found = True
else:
index = index + 1

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

How does a binary search work?

A

This search starts at the middle of the list and repeatedly divides the list in half until it reaches the desired item

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

What is the code for a binary search?

A

found = False
first = 0
last = len(items) - 1
While first <= last and found == False:
midpoint = (first + last) // 2
if items[midpoint == item_to_find:
found = True
else:
if items[midpoint] < item_to_find:
first = midpoint + 1
else:
last = midpoint - 1

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

How does a bubble sort work?

A

The program will run through the list item by item and compare adjacent items and swap them if they arent in the required order.

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

What is the code for a bubble sort?

A

n = len(items)
swapped = true
while n > 0 and swapped == True:
swapped = False
n = n - 1
for index in range(0,n):
if items[index] > items[index+1]:
temp = items[index]
items[index] = items[index=1]
items[index+1] = temp
swapped = True

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

How does an insertion sort work?

A

An insertion sort inserts each item into its correct position in a data set one at a time

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

What is the code for an insertion sort?

A

for index in range(1,n):
current = items[index]
index2 = index
while index2 > 0 and items[index2 - 1] > current:
items[index2] = items[index2 -1]
index2 = index2 - 1
items[index2] = current

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