Algorithms Flashcards

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

Loop invariant

A

an assertion about the loop that is relevant to the purpose of the loop and that holds true before and after each iteration through the loop

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

Sequential Search vs. Binary Search

A

Sequential Search is faster when the list is NOT sorted in ascending or descending order. Binary search employs the “half-half-half” method to find the target. This works best if the list is already sorted

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

Iterations for Binary Search (sorted)

A

for 2^k - 1 elements, binary search requires at most k iterations. With an array of n elements, this is log(base2)n iterations

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

Iterations for Sequential Search

A

n/2 (elements) < # iterations < n (elements)

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

Selection Sort

A

number of required comparisons is about n^2. Iterate for k from n down to 2: find the largest among the first k elements and swap it with the kth element

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

Insertion Sort

A

number of required comparisons is about n^2. iterate for k from 2 up to n. Keep the first (k-1) elements sorted and insert the k-th element among them where it belongs

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

Insertion Sort vs. Selection Sort:

A

Insertion Sort, on average, requires more moves than selection sort. NEITHER, however, ALWAYS requires more moves than the other

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

While vs. if in recursion

A

if usually replaces while loops for recursion methods, such is the entire PURPOSE of recursion

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

Merge sort comparison

A

nlogn

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

Merge sort method:

A

divide the array into two approximately equal halves; sort recursively each half, then merge them together into one sorted array

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

Technique of Sequential Search:

A

consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.[1] (Almost always slower than binary search)

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

Insertion Sort Technique:

A

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. On a repetition, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. It does NOT switch the elements.

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

Selection Sort Technique:

A

This is the one that DOES switch the elements.

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

Insertion Sort Run Times and storage complexities

A

Wort number of comparisons: n^2
Best comparisons: n
Average: n^2

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

Selection Sort Run Times and storage complexities:

A

Worse number of comparisons: n^2
Best comparisons: n^2
Average: n^2

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

Merge Sort Technique:

A

divide and conquer (usually with recursion).

17
Q

Merge Sort run times and storage complexities:

A

worst AND best case run time = nlogn

Average = nlogn