Ch 2: Algorithm Analysis & Lists Flashcards

1
Q

what is linear search?

A

a search algorithm that starts from the beginning of a list and checks each element until the search key is found or the end of the list is received
- will compare each element if the key is not found

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

explain linear search algorithm

A

a single counter starts from the first item and compares it against the search key, if it matches the item is returned otherwise the counter moves to the next item and repeats. if the search key is not found then the counter will have compared each item and “null” is returned

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

what is binary search?

A

a faster algorithm for searching a list if the list’s elements are sorted & directly accessible (array)

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

explain binary search algorithm

A

first, the middle element of the list is checked, if the search key is found then it is returned. otherwise, if the search key is less than the middle, the middle of the first half of the list is checked for the search key or if the search key is more than the middle, the middle of the second half of the list is checked for the search key. continue to check middle by splitting the list in half

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

what is the equation to find the middle

A

0+(N-1) / 2

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

what are constant time operations?

A

an operation that for a given processor, always operates in the same amount of time, regardless of input value

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

what is an example of a constant time operation?

A

multiplication because the same amount of numbers being multiplied will always have the same time
ex. 12 takes same time as 301000

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

what is not an example of a constant time operation?

A

string concatenation because more characters must be copied for larger strings

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

what affects CTO?

A

programming language and hardware running the code

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

what is lower and upper bound?

A
  • lower bound: a function f(N) that is <= the best case T(N) for all values N >= 1
  • upper bound: a function f(N) that is >= the worse case T(N) for all values N >= 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is an asymptotic notation?

A

classification of runtime complexity that uses functions that indicate the growth rate of a bounding function

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

what are the three asymptotic notations that are commonly used in complexity analysis?

A

O notation: provides a growth rate for an algorithm’s upper bound
Ω notation: provides a growth rate for an algorithm’s lower bound
Θ notation: provides a growth rate that is both an upper and lower bound

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

what are the rules for Big O notation?

A
  1. if f(N) is a sum of several terms, the highest order term ( the one with tte fastest growth rate ) is kept and others are discarded
  2. if f(N) has a term that is a product of several factors, all constants ( those that are not in terms of N ) are omitted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

explain the binary search recursive algorithm

A

outer function: takes in list
inner function: takes in list (subset), low, high, key
- base case: checks if the low is greater than high, then
- gets mid
- checks if the middle number is less than or greater than the key and makes recursive call with either first half or second half of list
- else returns the middle number

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

what is a recurrence relation?

A

a function f(N) that is defined in terms of the same function operating on a value < N

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

what is a recursion tree?

A

a visual diagram of an operation done by a recursive function, that separates operations done directly by the function and operations done by recursive calls

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

explain the different parts of a recursion tree

A
  • root node represents k operations inside the first function call
  • recursive operations are represented below the node
  • tree’s height corresponds to the number of recursive calls
  • an algorithm that performs N operations and then 2 recursive calls will have N/2, continues to divide by 2 as more recursive calls occur
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what is a singly-linked list?

A

a data structure for implementing a list ADT, where each node has data and a pointer to the next node

19
Q

what is a positional list?

A

a list where elements contain pointers to the next and/or previous elements in the list

20
Q

explain append operation for a singly-linked list

A
  • the node that is to be added is entered into the function
  • the tails->next is set to the new node, then tail is set to the new node
  • if head is null, then both head and tail point to the node
21
Q

explain prepend operation for a singly-linked list

A
  • the node that is to be added is entered into the function
  • new node->next is set to head, then head points to the new node
  • if head is null, then head and tail point to the new node
22
Q

explain insertafter operation for a singly-linked list

A
  • the node that is to be added is entered into the function
  • if the new node is to be the first node, then prepend
  • if new node is to be the last node, then append
  • if new node is to be inserted in the middle, then a pointer to the node of the location before is created, then new node’s next is set to before pointer’s next, and before pointer’s next is set to the new node
23
Q

explain removeafter operation for a singly-linked list

A
  • the node that is to be removed is entered into the function
  • a pointer to the node before is created aka curNode, and a pointer to befores->next->next is created, aka sucNode
  • if curNode is null then sucNode points to heads->next
  • if sucNode is null then tail
  • removing from middle: if curNode’s next pointer is not null, then sucNode points to curNode’s next->next node
24
Q

what is a doubly-linked list?

A

a data structure for implementing a list ADT, where each node has data a pointer to the next node, and a pointer to the previous node

25
Q

what is list traversal?

A

an algorithm that visits all nodes in the list once and performs an operation on each node

26
Q

explain list traversal algorithm

A

a pointer first starts at the list’s head and performs the operation as long as it is not null, then the pointer moves onto the next node and performs operation again and repeats until the node is null

27
Q

explain insert sort for doubly-linked lists

A

Insertion sort for a doubly-linked list operates similarly to the insertion sort algorithm used for arrays. Starting with the second list element, each element in the linked list is visited. Each visited element is moved back as needed and inserted into the correct position in the list’s sorted portion. The list must be a doubly-linked list, since backward traversal is not possible in a singly-linked list.

28
Q

explain insert sort for singly-linked lists

A

Insertion sort can sort a singly-linked list by changing how each visited element is inserted into the sorted portion of the list. The standard insertion sort algorithm traverses the list from the current element toward the list head to find the insertion position. For a singly-linked list, the insertion sort algorithm can find the insertion position by traversing the list from the list head toward the current element.

Since a singly-linked list only supports inserting a node after an existing list node, the ListFindInsertionPosition algorithm searches the list for the insertion position and returns the list node after which the current node should be inserted. If the current node should be inserted at the head, ListFindInsertionPosition return null.

29
Q

what aspect of linked lists makes adapting array-based sorting algorithms to inked lists difficult?

A

elements in the list cannot be accessed by index

30
Q

what is a dummy node (header node)?

A

a node with an unused data member that always resides at the head of the list and cannot be removed

31
Q

explain the advantage of using dummy nodes

A

using dummy nodes simplifies the algorithms for linked lists because head and tail pointers are never null and so the algorithm does not need to account for when head and tail is null

32
Q

explain the advantage of using dummy nodes

A

using dummy nodes simplifies the algorithms for linked lists because head and tail pointers are never null and so the algorithm does not need to account for when head and tail is null

33
Q

explain the forward traversal recursive algorithm

A

Forward traversal through a linked list can be implemented using a recursive function that takes a node as an argument. If non-null, the node is visited first. Then, a recursive call is made on the node’s next pointer, to traverse the remainder of the list.

The ListTraverse function takes a list as an argument, and searches the entire list by calling ListTraverseRecursive on the list’s head.

34
Q

what is bubbly sort?

A

a sorting algorithm that iterates through a list, comparing and swapping adjacent elements if the second element is less than the first element

35
Q

explain the bubble sort algorithm

A

Bubble sort uses nested loops. Given a list with N elements, the outer i-loop iterates N - 1 times. Each iteration moves the largest element into sorted position. The inner j-loop iterates through all adjacent pairs, comparing and swapping adjacent elements as needed, except for the last i pairs that are already in the correct position,.

36
Q

why is bubble sort impractical for real-world use?

A

bubble sorts run time is very large, O(N^2) because of their nested for loops

37
Q

what is bucket sort?

A

a numerical sorting algorithm that distributes numbers into buckets, sorts each bucket with an additional sorting algorithm and then concatenates buckets together to build the sort result

bucket: a container for numerical values in a specific range

38
Q

explain bucket sort algorithm

A

Bucket sort first creates a list of buckets, each representing a range of numerical values. Collectively, the buckets represent the range from 0 to the maximum value in the array. For N buckets and a maximum value of M, each bucket represents M+1 / N values. Each array element is placed in the appropriate bucket. Then, each bucket is sorted with an additional sorting algorithm. Lastly, all buckets are concatenated together in order, and copied to the original array.

39
Q

what is the difference between a list data structure and a list node data structure?

A

a list data structure is a data structure containing the list’s head and tail, and may also include additional information, such as the list’s size, while a list node data structure always maintains the data for each list element, including the element’s data and pointers to the other list element

40
Q

is a list data structure required to implement a linked list?

A

it is not required

41
Q

why is a list data structure helpful when implementing linked lists?

A

a list data structure offers a convenient way to store the list’s head and tail

42
Q

what is a circular linked list?

A

a linked list where the tail node’s next pointer points to the head of the list, instead of null

43
Q

what is a circular linked list?

A

a linked list where the tail node’s next pointer points to the head of the list, instead of null

44
Q

when does a traversal of a circular linked list end?

A

ends after reaching the head node a second time instead of when reaching null