Chapter 2.0 Introduction To Algorithm 4th Edition
Chapter 2.0 Introduction To Algorithm 4th Edition
Chapter 2.0 Introduction To Algorithm 4th Edition
The numbers to be sorted are also known as the keys. Although the problem is conceptually about sorting
a sequence, the input comes in the form of an array with n elements. When we want to sort numbers, it’s
often because they are the keys associated with other data, which we call satellite data. Together, a key
and satellite data form a record. For example, consider a spreadsheet containing student records with
many associated pieces of data such as age, grade-point average, and number of course taken. Any one of
these quantities could be a key, but when the spreadsheet sorts, it moves the associated record (the
satellite data) with the key. When describing a sorting algorithm, we focus on the keys, but it is important
to remember that there usually is associated satellite data.
In this book, we’ll typically describe algorithms as procedures written in a pseudocode that is similar in
many respects to C, C++, Java, Phyton, or JavaScript. (Apologies if we’ll omitted your favorite
programming language. We can’t list them all.) If you have been introduced to any of these languages,
you should have little trouble understanding algorithms “coded” in pseudocode. 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, so do not be
surprised if you come across and English phrase or sentences embedded within a section that looks more
like real code. Another difference between pseudocode and real code is that pseudocode often ignores
aspects of software engineering -such as data abstraction, modularity, and error handling – in order to
convey the essence of the algorithm more concisely.
We start with insertion sort, which is an efficient algorithm for sorting a small number of elements.
Insertion sort works the way you might sort a hand of playing cards.
Figure 2.1 Sorting a hand of cards using insertion sort.
Indentation indicates block structure. For example, the body of the for loop that begins on line 1
consists of line 2-8, and the body of the while loop that begins on line 5 contains lines 6-7 but not
line 8. Our indentation style applies to if-else statements as well. Using indentation instead of
textual indicators of block, such as begin and end statements or curly braces, reduces clutter while
preserving, or even enhancing, clarity.
The symbol “//” indicates that the remainder of the line is a comment
Variables (such as i, j, and key) are local to the given procedure. We won’t use global variables
without explicit indication.
We access array elements by specifying the array name followed by the index in square brackets.
For example, A[i] indicates the i th element of the array A.
The worst-case running time of an algorithm gives an upper bound on the running time for any
input. If you know it, then you have a guarantee that the algorithm never takes any longer. You
need not make some educated guess about the running time and hope that it never gets much
worse. This feature is especially important for real-time computing, in which operations must
complete by deadline.
For some algorithms, the worst case occurs fairly often. For example, in searching a date base for
a particular piece of information, the searching algorithm’s worst case often occurs when the
information is not present in the database. In some applications, searches for absent information
may be frequent.
The “average case” is often roughly as bad as the worst case.
Order of growth
(PAGE 34)