I . chp 2. Getting Started Flashcards
Objective
This chapter will familiarize you with the framework we shall use throughout the
book to think about the design and analysis of algorithms. We begin by examining the insertion sort algorithm to solve the sorting problem.Having specified the insertion sort algorithm, we then argue that it
correctly sorts, and we analyze its running time. The analysis introduces a notation
that focuses on how that time increases with the number of items to be sorted. Following our discussion of insertion sort, we introduce the divide-and-conquer
approach to the design of algorithms and use it to develop an algorithm called
merge sort. We end with an analysis of merge sort’s running time.
Keys in sorting problem:
The numbers that we wish to sort are also known as the keys.
Pseudo Code Vs Real Code.
What separates pseudocode from “real” code is that in
pseudocode, we employ whatever expressive method is most clear and concise to
specify a given algorithm. Sometimes, the clearest method is English.
Another difference between pseudocode and real code
is that pseudocode is not typically concerned with issues of software engineering.
Issues of data abstraction, modularity, and error handling are often ignored in order
to convey the essence of the algorithm more concisely.
Insertion Sort:
INSERTION-SORT(A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1 ... j - 1]. 4 i = j - 1 5 while i>0 and A[i] > key 6 A[i + 1] = A[i] 7 i = i - 1 8 A[i + 1] = key
Loop Invariant? Why do we need it?
invariant:
a function, quantity, or property which remains unchanged when a specified transformation is applied.
We use loop invariants to help us understand why an algorithm is correct.
Loop Invariant of Insertion Sort:
At the start of each iteration of the for loop of lines 1–8, the subarray
A[1 , , , j -1 ] consists of the elements originally in
A[ 1 , , , j - 1 ], but in sorted order
What things must be shown about loop invariant:
(3)
How to show Algo is correct?
Initialization: It is true prior to the first iteration of the loop.
Maintenance: If it is true before an iteration of the loop, it remains true before the
next iteration.
Termination: When the loop terminates, the invariant gives us a useful property
that helps show that the algorithm is correct.
When the first two properties hold, the loop invariant is true prior to every iteration
of the loop.
Pseudocode Conventions:
- block structure:
- Comments?
- Indentation indicates block structure.
3. // Indicates that the remainder of the line is a comment.
2.Loop Pseudocode Conventions:
2.The looping constructs while, for, and repeat-until and the if-else conditional
construct have interpretations similar to those in C, ..etc.
In this book, the loop counter retains its value after exiting the loop.
keyword downto when a for loop
decrements its loop counter,
“to” when increment.
When the loop counter changes by an amount
greater than 1, the amount of change follows the optional keyword “by”
Psedocode
j = e, i = j
different allowed assignment form.
4.A multiple assignment of the form i = j = e assigns to both variables i and j
the value of expression e; it should be treated as equivalent to the assignment
j = e followed by the assignment i = j
Scope of variables in Pseudocode:
- Variables (such as i, j , and key) are local to the given procedure. We shall not
use global variables without explicit indication.
Array Convention Pseudocode:
- We access array elements by specifying the array name followed by the index in square brackets. For example, A[i] indicates the ith element of the
array A. The notation “. .” is used to indicate a range of values within an array. Thus, A [1 . . j] indicates the subarray of A consisting of the j elements
A[1], A[2], . . , A[j] .
Compund data types rule, pointers to them.
Notations:
- We typically organize compound data into objects, which are composed of
attributes.
We access a particular attribute using the syntax : the object name, followed by a dot,
followed by the attribute name.
Example : Array A is treated as object and its attribute length, A.length.
We treat a variable representing an array or object as a pointer to it.
For all attributes f of an object x, setting y = x
causes y.f = x.f
If x.f = 3, then y.f = 3 as well. I
x and y point to the same object after the assignment y = x.
Our attribute notation can “cascade.” For example, suppose that the attribute f
is itself a pointer to some type of object that has an attribute g.
Then the notation
x.f.g is implicitly parenthesized as (x.f).g.
If y = x.f , then x.f.g is the same as y.g.
When pointer refers to no object at all, it has value NIL
Passing paramenters, Calling Convention:
- We pass parameters to a procedure by value: the called procedure receives its
own copy of the parameters, and if it assigns a value to a parameter, the change
is not seen by the calling procedure. When objects are passed, the pointer to
the data representing the object is copied, but the object’s attributes are not. For
example, if x is a parameter of a called procedure, the assignment x = y within
the called procedure is not visible to the calling procedure. The assignment
x.f = 3, however, is visible. Similarly, arrays are passed by pointer, so that a pointer to the array is passed, rather than the entire array, and changes to
individual array elements are visible to the calling procedure.
Return Statements:
9.A return statement immediately transfers control back to the point of call in
the calling procedure. Most return statements also take a value to pass back to
the caller. Our pseudocode differs from many programming languages in that
we allow multiple values to be returned in a single return statement.
Boolean Operators:
The boolean operators “and” and “or” are short circuiting. That is, when we
evaluate the expression “x and y” we first evaluate x. If x evaluates to FALSE,
then the entire expression cannot evaluate to TRUE, and so we do not evaluate y.
If, on the other hand, x evaluates to TRUE, we must evaluate y to determine the
value of the entire expression. Similarly, in the expression “x or y” we evaluate the expression y only if x evaluates to FALSE. Short-circuiting operators
allow us to write boolean expressions such as “x != NIL and x.f = y” without
worrying about what happens when we try to evaluate x.f when x is NIL.