Algorithms Flashcards

1
Q

binary search

A

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”

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

linear search

A

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

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

bubble sort

A

for i = 0 to A.length - 1:
for j = 0 to A.length - 2:
if A[j] > A[j+1]:
temp = A[j]
A[j] = A[j+1]
A[j+1] = temp
endif
return A

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

insertion sort

A

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

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