cop3530 quiz 7
what is a set?
A collection that contains no duplicate elements set objects: - are not indexed - do not reveal the order of insertion of items - do enable efficient search and retrieval of information - do allow removal of elements without moving other elements around
what is bubble sort?
- "simplest sorting algorithm" - "bubble" the largest element to the end continually, works by repeatedly swapping adjacent elements if they are in the wrong order to sort: - check the current element against its adjacent/next element - swap if the current element is larger (this will bubble the largest element up setting it into its "sorted" position) - repeat until no swaps occured
what are the 3 conditions for hash functions?
- should be able to evenly distribute the data - should be easy to compute - should be deterministic (not random; same key results in same output)
what is heapsort?
-builds a max heap from the array in-place, then pops elements one-by-one -building heap is O(n), then every pop is O(log(n)): O(1) to remove and then O(log(n)) to heapify time: worst/avg/best: O(nlog(n)) space: O(1)
what is a hash table?
-data structure in which data values are mapped to buckets based on keys -uses a hash function to compute an index (a hash code) which maps to a "bucket" containing value -values are places into their bucker using collision resolution policies -hash tables are unordered -a hash function is used to compute an index into an array of buckets
what is quick sort
-repeatedly splits the array around a pivot element and sorts elements around it -a type of divide and conquer algo -divide: selects a pivot element that splits the array, less than pivot part and greater than pivot part - conquer: recursively sort the two sub arrays - can have various implementations based on how you choose the pivot
what are maps?
collection of (key, value) pairs where key is unique
how does selection sort work?
maintains two subarrays: - a sorted subarray at beginning - a remaining unsorted subarray at end to sort: - "select" the minimum in the sorted part by iterating through it - move the minimum to the end of the sorted part - consider the minimum as now part of the sorted array - repeat till unsorted has size of 1
can you access a set's element by index? ex: set[index]
no!
how does a map print?
ordered: prints in order of keys (if unordered it does not print in any order)
what are some other examples of sorts?
sleep sort, counting sort, tim sort, radix sort, bucket sort
selections sort time complexity and space complexity
time: - worst/average/best: O(n^2) - will always have 2 nested for loops that iterate O(n) space: - O(1), in place sort
bubble sort time and space complexity
time: -worst/average: O(n^2), two nested loops, each O(n) -best: O(n), no swaps, break early space: -O(1), in place sort
merge sort time and space complexity
time: -worst/avg/best: o(nlog(n)) -repeatedly halving the array (O(log(n)), merge() compares each of the n elements space: -due to the arrays made for merging, space complexity is O(n)
what are the problems with tree based maps and sets?
- if the datatypes are comparable such as integers or characters, tree based maps and sets make sense. what if the data itself is incomparible? -common operations such as insert or search are O(logn). can we do better than this?
time and space complexity of quick sort
- moving elements around a pivot takes O(n) - how we choose a pivot can determine the number of recursive calls - good pivot: divides the array exactly in half -> O(logn) - bad pivot: divides the array into 1 and n - 1 -> O(n) - the stack frame lead to O(logn) space complexity time: worst: O(n^2) avg/best: O(nlog(n)) space: O(log(n))
what is shell sort?
-an optimization of insertion sort -first sorts the elements far apart from each other, then successively reduces the interval between the elements sorted -(n/2, n/4...1) -implementation depends on gap size choice -best gap size is still debated and time complexity can vary
what operations do sets support?
A and B (only values that they share), A or B (all values), in A but not in A (A-B), A is a subset of B (A < (this but like a U) B)
what is an ordered set implemented as? what is the time complexity?
BST, O(log(n))
what is an ordered map implemented as, what is the time complexity of insert and []?
BST, O(log(n)) for both
which are valid hash function examples? of the valid ones, are they good or not? H(x): {return 0} H(x): {return sum of all ASCII values} H(x): {return powers of 31 with ASCII} H(x): {return Random Number} H(x): {return Current Time}
H(x): {return 0} poor H(x): {return sum of all ASCII values} okay H(x): {return powers of 31 with ASCII} good
what are examples of O(n^2) sorting algorithms, O(nlog(n)) sorting algorithms, and sorting algorithms that are neither time complexity?
O(n^2): selection, bubble, insertion O(nlog(n)): merge, quick, heap other: shell
what is merge sort?
a type of divide and conquer algo divide and conquer - divide: split the problem into subproblems - conquer: solve the subproblems through recursion - combine: used the solved subproblems for the current problem merge sort: - divide: split the input arrays in two halves - conquer: sort each half through recursion - knowing that each half is sorted, merge the arrays into one sorted array
what is the best, avg, and worst time complexities for hashmap insertion, deletion, and search?
best/avg: O(1) worst: O(n), also in some cases rehashing is needed for insertion
which of the following are considered O(n2) sorting algorithms in their average time complexity? -bubble sort -quicksort -selection sort -heap sort
bubble, selection
what is separate chaining?
each bucket stores a linked list; collisions are simply appended to the end of the list
what is open addressing (linear probing)
each bucket stores only one entry; if you try to add an entry and there is a collision, move the "problem" entry (one bucket at a time) to the next available free bucket and put it there
what are the benefits of a good hash function?
evenly distribute data be easy to compute
what is an unordered set implemented as? what is its time complexity?
hash table, O(1) (+ O(k) for hash)
what is an unordered map implemented as? what is its time complexity for insert and []?
hash table, avg case: O(1)
what are the properties of a hash function
input: object x output: an integer representation of x if x is equal to y, H(x) = H(y) if x is not equal to y, it would be great if H(x) is not equal to H(y)
what are the methods of an ordered set?
insert(element) - adds element erase(element) - removed element find(element) - returns iterator to element if it is found, or returns an iterator to std::end otherwise count(element) - returns 1 if element is found or 0 otherwise size() - gives number of elements in set empty() - returns if set is empty or not
what are the methods of ordered maps?
insert(key, value) - if key already exists in map, returns false otherwise inserts new entry with key,value pair map[key] = value - if key already exists in map, overwrites with new value erase(key) - deletes key in map find(key) - searches for key in map, and returns iterator to it if found; otherwise returns iterator to map::end() count(key) - returns 1 if key is found in map or 0 otherwise size() - gives number of elements in map empty() - returns if maps is empty or not
what if we use an array and insert integers? what are the time complexities of insert and find, and what are the problems? how can we do this with strings?
insert: O(1) find: O(1) wastes memory and what about other data types? convert a letter to a number and multiply by s power of 27
what is the purpose of a hash function?
is used to distribute entries across a hash table represented as an array
what is a load factor?
it is a way to describe how funcll the hash table is becoming, it equals # entries / # buckets
what are some common hash functions
key mod table size, mid square hashing, string hashing
how does insertion sort work?
maintains two subarrays: - a sorted subarray at the beginning - remaining unsorted subarray at the end to sort: - grab the first element in the unsorted part - iterate through the sorted part to find where to "insert" the new element - repeat till unsorted has size 1
which of the following is not an in-place sorting algorithm? -selection sort -heap sort -quick sort -merge sort
merge sort
bad/invalid hash functions will?
produce different outputs for the same input take lots of time result in high potential for data collisions
which algorithm sorts by repeatedly moving elements around a pivot?
quicksort
what type of BST is an ordered map implemented as
red black tree
what is open addressing (quadratic probing)
same as linear probing, except you move the "problem" entry 1 bucket, then by 4 buckets, then by 9 buckets, then by 16
what are the methods of unordered maps?
same as ordered maps + 2 new bucket_count() - # buckets load_factor() - # elements / # buckets
what are the methods of an unsorted set?
same as orderedt set, + 2 new bucket_count() - # buckets load_factor() - # elements / # buckets
what are some collision resolution policies?
separate chaining, open addressing with linear or quadratic probing
what is the time complexity and space complexity for insertion sort?
time: - worst/average: O(n^2), two nested for loops each O(n) - best: O(n), if the key is greater than all sorted values, the data is already sorted and the inner loop is bypassed space: - O(1), in place sort
what should we do if load factor becomes too larege?
we should dynamically resize the table, and rehash our values to reduce the load_factor -> making our table more time efficient
what is a problem with linear probing?
we suffer from "clustering" which can be solved by using quadratic probing instead
what is a data collision?
when one thing that we try to insert hashes to the same value than an existing value does