Algorithms Flashcards

1
Q

What is insertion sort?

A

The natural question is, how can we place into our array while still keeping it sorted?

This operation is known as an insertion

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

What are the steps for insertion sort?

A

Place x at the end of the array.
Compare to the element on its left. There are three possibilities:

If there is no element to the left, then we are done, since x will be the smallest element and it is already at the start of the array.

If x is greater than or equal to it, then we are done; x is on the right of all smaller elements and on the left of all larger elements, so the array is once again sorted.

If x is smaller, then it should be before the other element. Switch the two to put x on front.

Repeat step two until finished.

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

What are the steps to the FULL insertion sort algorithm?

A

Designate the leftmost element of the array as the only element of the sorted side. This side is guaranteed to be sorted by default, since it now contains only one element.

Insert the first element of the unsorted side into the sorted side, increasing the number of sorted elements by one.

Repeat step two until there are no unsorted elements left.

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

What must be tracked during a full insertion sort?

A

All we have to do is keep track of how much of the original array is sorted.

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

What is the formula for the number of comparisons in the worst case for an array?

A

n( n - 1 ) / 2

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

What is the formula for the number of comparisons in the best case for an array?

A

n - 1

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

What is the goal of the divide and conquer method?

A

The goal of this method is to

divide a problem into equal sized sub-problems recursively;

conquer each problem separately, solving them recursively;

combine results.

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

What are the three steps of a merge sort algorithm?

A

1) Divide: Split the list into two (approximately) equally sized lists.
2) Conquer: Sort each of the two lists separately (using mergesort itself!).
3) Combine: Given two sorted lists of approximately the same size, merge them into one big sorted list.

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

What is the code for mergesort?

A

def merge(left, right):
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
# change the direction of this comparison to change the direction of the sort
if left[left_idx] <= right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1

    if left:
        result.extend(left[left_idx:])
    if right:
        result.extend(right[right_idx:])
    return result
def merge_sort(m):
    if len(m) <= 1:
        return m
middle = len(m) // 2
left = m[:middle]
right = m[middle:]
    left = merge_sort(left)
    right = merge_sort(right)
    return list(merge(left, right))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the advantage of mergesort?

A

The mergesort algorithm takes advantage of the fact that two sorted lists can be merged into one sorted list very quickly.

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

What is the best runtime on average?

A

It turns out that the runtime of O(NlogN) is the best we can do, on average, for a comparison-based sorting algorithm.

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

What is Quicksort?

A

Quicksort, just like mergesort, is a “divide-and-conquer” sorting algorithm that runs in O(NlogN) time in the average case.

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

What is the worst case for Quicksort?

A

Although it has a worst case runtime of O(n**2), overall quicksort tends to outperform mergesort in the real world, making it an attractive choice for many programmers.

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

What is partitioning in Quicksort?

A

Pick a pivot element (typically the last item in the array). All numbers lower than this value go to the left and all elements higher to the right. For quicksort, this step is commonly known as partitioning.

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

What is the key to Quicksort?

A

The key to a successful sort is picking the right pivot point.

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

What is the “median of three” method in Quicksort?

A

“Median of three” method–pick out the first, middle, and last elements and choose the median of the three.

pivot = sorted([arr[0],arr[arr_length // 2],arr[-1]])[1]

17
Q

What is the code for Quicksort?

A
def quickSort(arr):
    less = []
    pivotList = []
    more = []
    arr_length = len(arr)
    if arr_length <= 1:
        return arr
    else:
        pivot = arr[0] # Change this line
        for i in arr:
            if i < pivot:
                less.append(i)
            elif i > pivot:
                more.append(i)
            else:
                pivotList.append(i)
        less = quickSort(less)
        more = quickSort(more)
        return less + pivotList + more