Data Structures and Algorithms

¡Supera tus tareas y exámenes ahora con Quizwiz!

Define Big O

Big O(...) (Big Omicron) is the upper bound (worst case) of the performance of an algorithm as the input gets arbitrarily large

Define Linked List

Linked List: list of dynamically allocated data linked together by pointers to form a chain in memory

What is the time complexity of addition to the front of a singly linked list?

O(1)

What is the time complexity of random access of an array?

O(1)

What is the time complexity of addition to the end of a singly linked list? (without a tail pointer)

O(n)

What is the advantage of a doubly linked list vs a singly linked list with a tail pointer? Disadvantage?

The advantage is that removal from the end upgrades from linear to constant. O(1). The disadvantage is that it takes more memory to maintain a doubly linked list.

What data structures are typically used to implement a queue?

linked list and resizing array

What data structures are typically used to implement a stack?

linked list and resizing array

What are the two main rules for working with Big O notation?

1. Drop lower order polynomial terms 2. Drop constants

What is the time complexity of addition on the end of an array?

Amortized O(1) - ( O(1) in JS )

What does amortized complexity mean? Then give an example.

Amortized refers to the "usual" time complexity. An example of this would be the addition to the end of a Java array. Usually it will be O(1), but if we are out of room, we will take longer because we will have to allocate more memory.

What are arrays?

Arrays are contiguous regions of memory directly accessible with an index.

A clerk at a shipping company is charged with the task of rearranging a number of large crates in order of the time they are to be shipped out. Thus, the cost of compares is very low (just look at the labels) relative to the cost of exchanges (moving the crates). The warehouse is nearly full: there is extra space sufficient to hold any one of the crates, but not two. What sorting method should the clerk use? Justify your answer.

Because the exchanges are much more expensive than the compares, we want to use selection sort as the exchange complexity is N.

Which method runs faster for an array in reverse order, selection sort or insertion sort? Why?

Both sorting algorithms would run at about the same time because selection sort always runs at 1/2 N^2 and in this case insertion sort would also run at 1/2 N^2 as an array in reverse order is insertion sort's worst case scenario.

Which method runs faster for an array with all keys identical, selection sort or insertion sort? Why?

Insertion sort is faster than selection sort because even though their Big O is the same, insertion sort's average and best case scenarios are much better than selection sort's. Selection sort cannot distinguish between an input that is already sorted, while insertion can.

Say you are implementing a stack or queue. What are the tradeoffs between implementing it with a resizing array vs a linked list?

Linked-list implementation: - Every operation takes constant time in the worst case -Uses extra time and space to deal with the links Resizing-array implementation: -Every operation takes constant amortized time -Less wasted space

What is the time complexity of addition to the end of a doubly linked list?

O(1)

What is the time complexity of addition to the end of a singly linked list? (with a tail pointer)

O(1)

What is the time complexity of addition to the front of a doubly linked list?

O(1)

What is the time complexity of removal from the end of a doubly linked list?

O(1)

What is the time complexity of removal from the end of an array?

O(1)

What is the time complexity of removal from the front of a doubly linked list?

O(1)

What is the time complexity of removal from the front of a singly linked list?

O(1)

What is the time complexity of addition on the front of an array?

O(n)

What is the time complexity of iteration of an array?

O(n)

What is the time complexity of iteration through a doubly linked list?

O(n)

What is the time complexity of iteration through a singly linked list?

O(n)

What is the time complexity of random access on a doubly linked list?

O(n)

What is the time complexity of random access through a singly linked list?

O(n)

What is the time complexity of removal from the front of an array?

O(n)

What is the time complexity of removal to the end of a singly linked list?

O(n)

What is the intuitive interpretation of the growth rate of O(log n)?

The algorithm cuts the size of the problem by some fraction (usually 1/2)

What is the intuitive interpretation of the growth rate of O(n^2)?

The algorithm has full input dependency per input element (such as in most nested loops).

What is the intuitive interpretation of the growth rate of O(n log n)?

The algorithm is logarithmic but also has a linear component

What is the intuitive interpretation of the growth rate of O(n)?

The algorithm time complexity increases directly with input size.

What is the intuitive interpretation of the growth rate of O(1)?

The algorithm time complexity is independent of input size.

Name the main heuristic used to calculate Big O for consecutive coding statements

The maximum Big O of the statements is all that counts

Suppose that we use insertion sort on a randomly ordered array where elements have only one of three values. Is the running time linear, quadratic, or something in between?

The running time would be about quadratic as the values would have to be at least partially ordered to be faster than quadratic.

What is the calculation heuristic of the Big O of a loop?

The total complexity of the algorithm is the complexity of statements inside the for loop times number of iterations.

What is the calculation heuristic of the Big O of an if/else statement?

The total complexity of the algorithm is the complexity of the test statement plus the maximum of the two alternatives.

A colleague suggests you use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort's runtime complexity change if you were to use it to sort a linked list?

This is a bad idea because a singly linked list would only reference each node's "next" value, and insertion needs to be able to traverse backwards to find where it will insert the value in the correct place. The insertion sort's runtime complexity would greatly increase when using it to sort a linked list.

What is the calculation heuristic of the Big O of a nested loop?

Total complexity is complexity of statement multiplied by product of the sizes of all loops. Remember to analyze these inside out.

A colleague suggests you use selection sort for h-sorting in shellsort. Why is this a bad idea?

Using selection sort for h-sorting would be much slower than using insertion sort, as the values are somewhat ordered.


Conjuntos de estudio relacionados

Chapter 9 Project Human Resource Management

View Set

MGMT-309 Exam III Review Questions

View Set

Period 5 (1750-1914) AP world map test full quizlet

View Set

Food Assistance Programs-In Class Handout

View Set

Biology 6.4 Traits, Genes, & Alleles

View Set