CS 112
Lecture 15
More on Recursion and Recursive Function Calls
- Some non-trivial examples of recursive functions:
- Merge Sort. We "sorted" by length a whole bunch of pieces of chalk, using merge sort. The algorithm illustrates "divide-and-conquer"
and takes O(n log n) time steps.
- Binary search. It takes O(log n) time steps.
- Evaluation of the recursive Fibonacci function, or of a "divide-and-conquer" function.
- Discussion of orders of magnitude for functions. When we measure the execution time of an algorithm, we express it as
a function of the input size. For an array of size n, findMin takes O(n) time, swap takes constant O(1) time, shift takes O(n) time,
selection sort and insertion sort take (in the worst case) O(n^2) time).
- Ackermann's function, a simple-to-define, simple-to-implement
function that grows faster than anything you can imagine. Running it on some small values such as f(4,5) will take a long long time!!!!
Lab: Queues
-
Review Stack and introduce Queue.
-
Stack
= LIFO (last-in first-out)
-
Queue
= FIFO (first-in first-out)
- Queue abstract data type. What are the class variables and
methods.
- Implementation ideas. Array? Shift elements? Complexity
issues.
- Circular (ring) queues.
Discussion: variations on queue implementation.
- Using all elements of the array (i.e. not always having an unused one).
- Distinguishing between empty and full queue in various implementations.
- Using a boolean variable to capture when the queue is empty (or full).
- "Erasing" elements from the queue (special symbol for an "empty" spot!).
- Efficiency considerations: time versus space usage.
- Queue in Java.
- Applications of queues.
- Waiting. Look at the printer queue.
- Client-server technology and queues.
- Simulation. E.g. an airport simulator. Planes waiting in line
(queue) to land and take-off.
- Queuing theory.
Preparation for next homework
- Hw7: Queue data structure and application.