Statement Effort MergeSort(A, lower_bound, upper_bound) begin T(n) if (lower_bound < upper_bound) Θ(1) mid = (lower_bound + upper_bound)/ 2 Θ(1) MergeSort(A, lower_bound, mid) T(n/2) MergeSort(A, mid+1, upper_bound) T(n/2) Merge(A, lower_bound, mid,upper_bound) Θ(n) end So T(n) = Θ(1) when n = 1, and 2T(n/2) + Θ(n) when n > 1 So what (more succinctly) is T(n)?
ADT: A collection of data and functions that operate on the data. since it is abstract, we do not need to specify languagespecific data types or functions. specify the collection by the general way in which the data is stored, and describe the actions of the functions. Linear List : An ordered collection of data; that is, the data is arranged into some order (not necessarily sorted), so that data is referenced by its position within the list
2 Subclasses Stacks Queues Operation Initialize Empty Insert Delete Explanation Initialize the internal structure of the list, make sure it s empty Returns true iff the list is empty Inserts a new element after the k th element; if k is zero, then insert at the beginning of the list Delete the k th item in the list.
Subclass of Linear Lists Access restricted to one end of the list called the Top Operation Description Initialize Push Pop Empty StackTop Size Initialize internal structure; create empty stack Add new element to top of stack Remove top element from stack True iff stack has no elements Returns copy of top element of stack (without popping it) Returns number of elements in the stack
Reversing data Parsing Postponement Backtracking
Parsing is the process of breaking down a string into constituent components and determining the structure of the component sequence Checking if expressions are balanced Process the string from left to right, one character at a time When a left parenthesis is found, push it onto the stack When a right parenthesis is found, if the stack is empty then there is a mismatch (R without L), otherwise pop the stack once (to remove the matching left parenthesis) When done, if the stack is empty, then the parentheses are balanced, otherwise there is a mismatch (L without R).
Sometimes data within a list is not processed when it is encountered; rather, processing must be deferred until a later step. postfix evaluation is a classic example of postponement. In postfix notation, operators appear after their operand, so that 1 + 2 in our normal (infix) notation becomes 1 2 +.
Infix Postfix a * b a b * a + b * c a b c * + a * b + c a b * c + (a + b) * c + d + e * f - g a b + c * d + e f * + g -
1. Initialize stack 2. For each item x in the expression, from left to right, do 2.1-2.2. 2.1. If x is a number (or variable), Then 2.1.1. Push x onto stack 2.2. Else (x is an operator (+, -, *, /)) 2.2.1. Pop op 2 from stack 2.2.2. Pop op 1 from stack (notice the reverse order) 2.2.3. Perform the operation: op 1 x op 2 2.2.4. Push the result onto the stack 3. Pop answer from the stack
1. Set postfix string to empty string 2. Create an empty operator stack 3. Repeat 3.1 Get the next token in the infix string 3.2 If next token is an operand (begins with digit), append it to postfix string 3.3 If next token is an operator 3.3.1 Process the next operator
Set done to false Repeat If operator stack is empty or next op is (, push next op onto stack and set done to true Else if precedence(next op) > precedence(top operator) Push next op onto the stack (ensures higher precedence operators evaluated first) and set done to true Else Pop the operator stack If operator popped is (, set done to true Else append operator popped to postfix string Until done
Infix Expression 3 + 5 * 6 7 * (8 + 5) Postfix Expression 3 5 6 * + 7 8 5 + *