Algorithms Flashcards

1
Q

What is linear search?

A

In linear search, the idea of the algorithm is to iterate across the array from left to right, searching for a specified element.

In pseudocode:
*Repeat, starting at the first element:
* If the first element is what you’re looking for (the target), stop.
*Otherwise, move to the next element.

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

What is big O notation

A

Big O Notation is a tool used to describe the time complexity of algorithms. It calculates the time taken to run an algorithm as the input grows. In other words, it calculates the worst-case time complexity of an algorithm. Big O Notation in Data Structure describes the upper bound of an algorithm’s runtime

It helps programmers calculate the scalability of an algorithm or count how many steps it must execute to give output based on data the program works on

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

What is the runtime complexity of linear search?

A

Runtime Complexity for Linear Search = O(n)
It’s, as the name says, linear
one extra ‘item’ adds one extra ‘unit of time’

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

What is binary search?

A

the idea of the algorithm is to divide and conquer, reducing the search area by half each time, trying to find a target number.

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

What is needed to do binary search?

A

In order to leverage the power of binary search, our array must first be sorted, else we cannot make assumptions about the array’s contents.

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

What is the pseudocode of Binary search

A

Repeat until the (sub)array is of size 0:
*Calculate the middle point of the current (sub)array.
*If the target is at the middle, stop.
*Otherwise, if the target is less than what’s at the middle, repeat,
changing the end point to be just to the left of the middle.
*Otherwise, if the target is greater than what’s at the middle, repeat, changing the start point to be just to the right of the middle.

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

What is the runtime complexity of binary search?

A

Runtime Complexity for binary = O(log n)

this means: for every item added the number of operations grows very slowly.

log(n) is 2^N = value (N = the power is needed to reach that value

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

Name 3 ways to sort an array

A

Selection sort, Bubble sort, Merge sort

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

What is Bubble sort?

A

In bubble sort, the idea of the algorithm is to move higher valued elements generally towards the right and lower value elements generally towards the left.

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

What is de pseudocode of Bubble Sort

A

*Set swap counter to a non-zero value
*Repeat until the swap counter is 0:
* Reset swap counter to 0
* Look at each adjacent pair
* If two adjacent elements are not in order, swap them and
add one to the swap counter

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

Worst case scenario when using bubble sort?

A

The array is in reverse order; we have to “bubble” each of the n-elements all the way across the array, and since we can only fully bubble one element into position per pass, we must do this n times.

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

Best case scenario when using bubble sort?

A

The array is already perfectly sorted, and we make no swaps on the first pass.

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

What is the runtime complexity of bubble sort?

A

Runtime Complexity for bubble sort = O(N²)

Best case is O(n) = when array is already sorted

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

What is selection sort?

A

In selection sort, the idea of the algorithm is to find the smallest unsorted element and add it to the end of the sorted list.

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

What is de Pseudocode of selection sort

A

*Repeat until no unsorted elements remain:
*Search the unsorted part of the data to find the smallest value
*Swap the smallest found value with the first element of the unsorted part

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

What is the runtime complexity of selection sort?

A

Runtime Complexity for selection sort = O(N²)

Runtime for best- and worse case are exactly same because you need to check every item for each item

16
Q

What is merge sort?

A

In merge sort, the idea of the algorithm is to sort smaller arrays and then combine those arrays together (merge them) in sorted order.

17
Q

What is merge sort in Pseudocode?

A

*Sort the left half of the array (assuming n> 1)
*Sort the right half of the array (assuming n> 1)
*Merge the two halves together

18
Q

What is the runtime complexity of merge sort?

A

Runtime Complexity for selection sort =O(n log n)

Worst-case scenario: We have to split n elements up and then recombine them, effectively doubling the sorted subarrays as we build them up. (combining sorted 1-element arrays into 2-element arrays, combining sorted 2-element arrays into 4-element arrays…)

19
Q

What is recursion?

A

*We might describe an implementation of an algorithm as being particularly “elegant” if it solves a problem in a way that is both interesting and easy to visualize.

*The technique of recursionis a very common way to implement such an “elegant” solution.

*The definition of a recursive function is one that, as part of its execution, invokes itself.