Algorithms Part I
Suppose that an array a[ ] is a max-heap that contains the distinct integer keys 1, 2, ..., N with N >= 7. The key N must be in a[1] and the key N - 1 must be in either a[2] or a[3]. Where must the key N - 2 be?
2,3,4,5,6, or 7
What is a priority queue?
A kind of queue in which the elements are dequeued in priority order.
What does it mean for a sorting algorithm to be stable?
A stable sort is a sort that preserves the relative order of items with equal keys. For example, when ordering the songs in a playlist, perhaps you would also like for the albums to be sorted simultaneously among those songs. In a list of students with their corresponding section in a class, perhaps you may want to FIRST sort by name, THEN sort by section.
What's another advantage of using comparators?
You can use comparators to sort data with two different keys... (Different comparison techniques)
Write a heap implementation of the sink method.
private void sink (int k){ while (2 * k <= N) { int j = 2 *k; if (j < N && less (j, j+1)) j++; if (!less (k, j)) break; exch (k, j); k = j; }}
Write a heap implementation of the swim method.
private void swim (int k){ while (k > 1 && less(k/2, k)){ exch(k, k/2); k = k/2; }}
Create a comparator class that allows for ordering phone numbers as in the British system.
public class BritishPhoneBookOrder implements Comparator<String>{ @Override public in compare(String s1, String s2){ /** Implementation of however Brits organize phone numbers in their phone books */ } }
Describe Merge sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
Merge sort is a stable sorting algorithm that isn't in-place. That is, merge sort requires the creation of an auxiliary array. Merge sort has a linearithmic guarantee. Although, quicksort is faster in practice and needs less space.
Which algorithm is faster at sorting data for large n? Insertion sort or mergesort?
MergeSort. On billions of input, merge sort will take approximately 18 minutes to sort the items. Insertionsort will take approximately 317 years.
How does a stack function?
Most recently added items are removed first.
Sorting algorithms are often more about what attributes than sorting elements?
-Stability -Parallel -Deterministic -Keys all distinct -Multiple key types -Linked list or arrays? -Large or small items? -Is your array randomly ordered? -Need guaranteed performance?
Using a compare-based lower bound for sorting, prove that a sorting algorithm must use at least N * log-2(N) compares in the worst-case.
-Assume array consist of N distinct values a1 through aN. -Worst case dictated by height h of decision tree. -Binary tree of height h has at most 2^h leaves -N! different orderings -> at least N! leaves 2^h >= # leaves >= N! -> h >= log-2(N!) ~ N*log-2(N) [Stirling's formula]
How would you implement mergesort?
-Divide array into two halves -Recursively sort each half. -Merge two halves.
What are the two important properties of a heap-ordered binary tree?
1. Keys in nodes. 2. Parent's key no smaller than children's key.
What two invariants that a binary heap satisfies?
1. The priorities of the children of a node are at least as large as the priority of the parent. By implication, the node at the top (root) of the tree has minimum priority. 2. The different paths from root to leaf differ in height by at most one. At the bottom of the tree there may be some missing leaves; these are to the right to all of the leaves that are present.
Describe 3-way Quick sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
3-way quicksort is an in-place sorting algorithm that isn't stable. Worst case sorting requires quadratic time. Average case requires linearithmic and best case requires linear time. This algorithm improves quicksort in presence of duplicate keys.
What is computational complexity?
A Framework to study efficiency of algorithms for solving a particular problem X.
What is a binary heap?
A binary heap is a special kind of balanced binary tree.
Describe what a binary heap is. Emphasize array representation.
A binary heap is an array representation of a heap-ordered complete binary tree. The properties of the array representation are: Indices start at 1, take nodes in level order, and there are no explicit links needed!
What is a Collection? What are 4 types of collections?
A collection is a data structure that supports methods for inserting and deleting items. Each collection type is distinguished based on which item it removes first. Stacks, Queues, Randomized Queues, and Priority Queues are types of Collections
What is a complete binary tree?
A perfectly balanced tree, except for bottom level.
How does a randomized queue function?
A randomized queue, also known as a bag, removes a random item.
How would you implement the insert method for a binary heap?
Add node at end of array, then swim it up to satisfy the properties of a heap-ordered binary tree. At most 1 + lgN compares
What is an optimal algorithm?
An algorithm with best possible cost guarantee for X. In other words, we must prove that the upper bound and the lower bound are the same.
What is Tilda Notation?
An approximation (or average) of the amount of time an algorithm takes to complete given an input size N. An algorithm is ~ N (linear) if it's run time is c * N + b. In other words, an algorithm is ~ f(N) if lim as N gets large of f(N)/g(N) = 1. for g(N) is the algorithm's theoretical runtime.
What will happen if the assert statement is false?
An exception will be thrown if the assert statement is false. You can enable or disable assertions at runtime.
What is Big Oh Notation?
Big Oh is the average amount of time an algorithm takes to complete, given an input size N
What is Big Omega Notation?
Big Omega is the lower bound on the amount of time an algorithm takes to complete given an input size N
What is Big Theta Notation?
Big Theta is the upper bound on the amount of time an algorithm takes to complete, given an input size N
What is the order-of-growth of running time for a binary heap implementation of a priority queue?
Binary Heap: insert requires logarithmic time. Deleting max requires logarithmic time. Retrieving and removing max requires constant time.
What is the upper bound in terms of a model of computation?
Cost guarantee provided by some algorithm for X. The problem is at most this difficult.
What properties do priority queues have?
Each element has a priority, an element of a totally ordered set, usually a number. Max Priority queues give large numbers higher priority. Min Priority queues give smaller numbers higher priority.
In a binary heap implementation, when a child's key becomes larger than its parent's key, how can you eliminate this violation?
Exchange key in child with key in parent. Repeat until heal-order is restored.
What is the goal of mergesort?
Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].
What is merge sort?
In computer science, merge sort (also commonly spelled mergesort) is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.
Which sorts are stable?
Insertion Sort and MergeSort
What is insertion sort?
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Describe Insertion sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
Insertion sort is an in-place and stable sorting algorithm that performs in quadratic time in worst case and average case. N^2 / 2 and N^2 / 4 respectively. In the best case, linear time is taken. Use for small n or partially ordered items.
How does a queue function?
Least recently added items are removed first. Imagine a line of people waiting to buy a ticket.
What is the expected number of array accesses and compares, respectively, to insert a random key into an ordered array implementation of a priority queue?
Linear and Logarithmic. We can use binary search to find the insertion point in logarithmic time. On average, the key to be inserted must be placed in the middle of the array-- to keep the array in order, we must shift over all larger keys.
What is a model of computation?
Operations that an algorithm is allowed to perform. In framing the difficulty of a problem we need an upper bound, lower bound, and an optimal algorithm.
What is the order-of-growth of running time for an ordered array implementation of a priority queue?
Ordered array: insert requires linear time. Deleting max requires constant time. Retrieving and removing max requires constant time.
What is the lower bound in terms of a model of computation?
Proven limit on cost guarantee of all algorithms for X. In other words, no algorithm could perform better.
Describe Quick sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
Quicksort is an in-place sorting algorithm that's not stable. Worst case runtime is quadratic but Quicksort gives a probabilistic guarantee of running in linearithmic time. This is the fastest algorithm in practice.
Which sorts are not stable?
Quicksort, SelectionSort and ShellSort
What is quicksort?
Quicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(n log n) algorithms.
How does a priority queue function?
Remove the largest or smallest item from the collection.
Describe Selection sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
Selection sort is an in-place sorting algorithm that isn't stable. Worst, average, and best case are quadratic time. N^2 / 2 for all. There are N exchanges.
What is ShellSort?
Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort).
What is an Assertion?
Statement to test assumptions about your program. -Helps detect logic bugs. -Documents code.
Sort an array of strings such that the sorting algorithm is insensitive to uppercase/lowercase conditions.
String[ ] array = new String[size]; Arrays.sort(array, String.CASE_INSENSITIVE_ORDER) String.CASE_INSENSITIVE_ORDER returns a comparator object that is used by the java's sort algorithm to sort through the string array without considering the uppercase/lowercase nature of each character.
Why does Array.sort( ) in Java use mergesort instead of quicksort when sorting reference types?
The Java API for Arrays.sort( ) requires that it is stable and that it offers guaranteed N log N performance. Neither of these are properties of standard quicksort. Quicksort uses less memory and is faster in practice on typical inputs ( and is typically used by Arrays.sort ( ) when sorting primitive types, where stability is not relevant).
What is our model of computation for sorting algorithms? What is our cost model?
The decision tree. The height of the tree is the worst case number of compares. We'll consider the number of compares as our cost model.
How can you use array indices to move through a heap-ordered binary tree?
The parent node at k is k / 2. Children of node at k are at 2*k and 2*k + 1. We can move through a tree if we know how to access the parent of some node k and its children.
What is selection sort?
The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array. The number of times the sort passes through the array is one less than the number of items in the array.
What is the order-of-growth of running time for an unordered array implementation of a priority queue?
Unordered array: inert requires constant time. Deleting max requires linear time. Retrieving and removing max requires linear time.
How do you declare an assertion is java?
Use the keyword "assert".
What are the applications of priority queues?
Useful for event-based simulators, with priority = simulated time, real-time games, searching, routing, compression via Huffman coding.
What's a good way of implementing a priority queue?
Using balanced binary trees such as an AVL tree, a red-black tree, or a splay tree.
Why use the comparator interface in sorting algorithms?
Using the Comparator interface helps decouple the definition of the data type from the definition of what it means to compare two objects of that type.
Is the system sort provided by Java's Array.sort ( ) class method good enough?
Usually. Java provides a mergesort implementation for reference types and quicksort for primitive types with the Arrays.sort ( ) method.
Is Mergesort an optimal algorithm? How can you tell?
Yes. Because the lower bound and upper bound are equivalent: N * lg(N) this algorithm is optimal. Mergesort is optimal with respect to the number of compares. But it's not optimal with respect to space usage.
How many compares does Mergesort use?
at most N log-2 N compares and 6Nlog-2 N array accesses to sort any array of size N.
Give an array representation of a binary heap.
i - 0 1 2 3 4 5 6 7 index a[i] -T S R P N O A T is the parent of S and R. S is the parent of P and N. R is the parent of O and A
What is the height of a binary tree with N nodes?
log base 2 of N. Proof: Height only increases when N is a power of 2.
Describe Shell sort in terms of 6 attributes: in place? stable? worst case? average case? best case? and remarks
Shell sort is an in-place sorting algorithm that isn't stable. Worst case and average case are not clearly known. Linear time is best case runtime. Tight code, sub-quadratic.