0% found this document useful (0 votes)
94 views234 pages

Comp631 Chap6

This document discusses data structures and basic terminology related to organizing data. It defines key concepts like data, data items, entities, fields, records, and files. It also describes common data structures like arrays, linked lists, trees, stacks, and queues. Arrays and linked lists are linear data structures while trees allow for hierarchical relationships between data elements. Stacks follow last-in first-out processing while queues are first-in first-out.

Uploaded by

Myo Thi Ha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views234 pages

Comp631 Chap6

This document discusses data structures and basic terminology related to organizing data. It defines key concepts like data, data items, entities, fields, records, and files. It also describes common data structures like arrays, linked lists, trees, stacks, and queues. Arrays and linked lists are linear data structures while trees allow for hierarchical relationships between data elements. Stacks follow last-in first-out processing while queues are first-in first-out.

Uploaded by

Myo Thi Ha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 234

Chapter VI

Data Structures
6.1 Introduction and Overview

6.1.1 Basic Terminology; Elementary Data Organization


• Data
• simply values or sets of values
• Data item
• a single unit of values
• are divided into subitems are called group items;
elementary items
• an entity
• has certain attributes or properties which may be
assigned values, either numeric or nonnumeric
For example,
possible attributes and their corresponding values for an
entity, an employee of a given organization:
Attributes: Name Age Sex Social Security Number
Values: ROHLAND, GAIL 34 F 134-24-5533

• Entity set
• Entities with similar attributes (e.g., all the employees
in an organization)
• fields, records and files reflects the relationship between
attributes, entities and entity sets
• a field is a single elementary unit of information
representing an attribute of an entity
• a record is the collection of field values of a given entity
• a file is the collection of records of the entities in a given
entity set.
Example 1
(a) Suppose an automobile dealership maintains an inventory file where each
record contains the following data:
Serial Number, Type, Year, Price, Accessories
The Serial Number field - primary key
(a unique serial number)

(b) Suppose an organization maintains a membership file where each record


contains the following data:
Name, Address, Telephone Number, Dues Owed
• Name and Address - group items
• Name field - primary key
• Address and Telephone Number fields - not primary keys, since some
members may belong to the same family and have the same address and
telephone number.
Records
• classified according to length.
• fixed-length records or variable-length records
• In fixed-length records
• all the records contain same data items with the same
amount of space assigned to each data item.
• In variable-length records
• file records may contain different lengths.
• For example, student records usually have variable
lengths, since different students take different numbers
of courses. Usually, variable-length records have a
minimum and a maximum length.
6.1.2 Data Structures

• Data may be organized in many different ways;


• logical or mathematical model of a particular organization of data is called a data structure.
• The choice of a particular data model depends on two considerations.
• First, it must be rich enough in structure to mirror the actual relationships of the data in the real world.
• On the other hand, the structure should be simple enough that one can effectively process the data when necessary.
Array
• simplest type - linear (or one-dimensional) array
• a list of a finite number n of similar data elements referenced respectively by a set of n consecutive numbers, usually 1, 2, 3, .... n
• A for the array, then the elements of A
subscript notation
a1, a2, a3, . . . ., an
parenthesis notation
A(l), A(2), A(3), . . . . A(N)
bracket notation
A[l], A[2], A[3], . . .A[N]
The number K in A[K] is called a subscript and
A[K] is called a subscripted variables.
Example 2
A linear array STUDENT consisting of the names of
six students.
STUDENT[1] denotes John Brown,
STUDENT[2] denotes Sandra Gold, and so on.
STUDENT

1 John Brown
2 Sandra Gold
3 Tom Jones
4 June Kelly
5 Mary Reed
6 Alan Smith
Example 3
A chain of 28 stores, each store having 4 departments, may list its weekly sales (to the nearest dollar).
Such data can be stored in the computer using a two-dimensional array in which the first subscript denotes the store and the second subscript the department.
If SALES is the name given to the array, then
SALES[1,1] = 2872, SALES[1, 2] = 805, SALES[1, 3] = 3211,... SALES[28,4] = 982
The size of this array is denoted by 284 (read 28 by 4), since it contains 28 rows (the horizontal lines of numbers) and 4 columns (the vertical lines of numbers).
Dept.
1 2 3 4
Store
1 2872 805 3211 1560
2 2196 1223 2525 1744
3 3257 1017 3686 1951
...

28 2618 931 2333 982


Linked Lists Customer Salesperson
• Suppose a brokerage firm 1 Adams Smith
maintains a file where each
record contains 2 Brown Ray
• a customer's name and 3 Clark Jones
• his or her salesperson 4 Drew Ray
• Clearly the file could be
5 Evans Smith
stored in the computer by
such a table, i.e., by two 6 Farmer Jones
columns of nine names. 7 Geller Ray
• However, this may not be
the most useful way to store 8 Hill Smith
the data 9 Infeld Ray
• Another way of storing the data is to have a separate array for the salespeople and an entry (called a pointer) in the customer file which
gives the location of each customer's salesperson.
• some of the pointers are pictured by an arrow from the location of the pointer to the location of the corresponding salesperson.
• Practically, an integer used as a pointer requires less space than a name
• hence this representation saves space, especially if there are hundreds of customers for each salesperson.
Customer Pointer Salesperson
1 Adams 3 Jones 1
2 Brown 2 Ray 2
3 Clark 1 Smith 3
4 Drew 2
5 Evans 3
6 Farmer 1
7 Geller 2
8 Hill 3
9 Infeld 2
• Suppose the firm wants the list of customers for a given
salesperson.
• One way to simplify such a search is to have the arrows in
figure point
• the other way; each salesperson would now have a set of
pointers giving the positions of his or her customers
• main disadvantage is that each salesperson may have many
pointers and the set of pointers will change as customers
are added and deleted.

Salesperson Pointer

1 Jones 3,6
2 Ray 2,4,7,9
3 Smith 1, 5,8
• Another very popular way to store the type of data, each salesperson has

Customer Link Sales Pointer


one pointer which points to his or her first customer, whose pointer in
turn points to the second customer, and so on, with the salesperson's last
customer indicated by a 0.
• one can easily obtain the entire list of customers for a given salesperson
and person
• one can easily insert and delete customers.

1 Adams 5 Jones 3 1
2 Brown 4 Ray 2 2
3 Clark 6 Smith 1 3
4 Drew 7  
5 Evans 8  
6 Farmer 0  
7 Geller 9  
8 Hill 0  
9 Infeld 0  
Trees
• Data frequently contain a hierarchical relationship between various elements.
• The data structure which reflects this relationship is called a rooted tree graph or, simply, a tree.

Example 4. Record Structure


• an employee personnel record may contain the following data items:
Social Security Number, Name, Address, Age, Salary, Dependents
• group item
– Name - subitems Last, First and MI (middle initial)
– Address - subitems Street and Area
• Area itself a group item having subitems
City, State and ZIP code number.
Employee

Soc. Sec. No. Name Address Age Salary Dependents

Last First MI Street Area

City State ZIP


01 Employee
02 Social Security Number
02 Name
03 Last
03 First
03 Middle Initial
02 Address
03 Street
03 Area
04 City
04 State
04 ZIP
02 Age
02 Salary
02 Dependents
Some of data structures are
(a) Stack
• last-in first-out (LIFO) system
• a linear list in which insertions and deletions can take
place only at one end, called the top.
• is similar in its operation to a stack of dishes on a
spring system
• new dishes are inserted only at the top of the stack and
• dishes can be deleted only from the top of the stack.
(b) Queue
• first-in first-out (FIFO) system
• a linear list in which deletions can take place only at one end of the list, the "front" of the list, and insertions can take place only at the other end of the list, the "rear" of list.
• the same way as a line of people waiting at a bus stop
• the first person in line is the first person to board the bus
• automobiles waiting to pass through an intersection-the first car in line is the first car through.
(c) Graph
• a relationship between pairs of elements which is not necessarily hierarchical in nature
• an airline flies only between the cities connected by lines

Boston
Chicago New York
Phaladelphia

Los Angeles

Miami
Airline flights
6.1.3 Data Structure Operations
(1) Traversing
Accessing each record exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called
"visiting" the record.)
(2) Searching
Finding the location of the record with a given key value, or finding the
locations of all records which satisfy one or more conditions.
(3) Inserting
Adding a new record to the structure.
(4) Deleting
Removing a record from the structure.
• Sometimes two or more of the operations may be used in a given situation; e.g., we may
want to delete the record with a given key, which may mean we first need to search for
the location of the record.
The following two operations, which are used in special situations, will also be considered:
(1) Sorting
Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social
security number or account number)
(2) Merging
Combining the records in two different sorted files into a single sorted file
Other operations, e.g., copying and concatenation.
6.1.4 Algorithms: Complexity, Time-Space Tradeoff

• An algorithm is a well-defined list of steps for solving a


particular problem.
• One major purpose is to develop efficient algorithms for
the processing of our data.
• time and space it uses are two major measures of the
efficiency of an algorithm
• complexity of an algorithm is the function which gives the
running time and/or space in terms of the input size.
• choice of data structure involves a time-space tradeoff:
by increasing the amount of space for storing the data,
one may be able to reduce the time needed for processing
the data, or vice versa.
Searching Algorithms
• Consider a membership file, as in Example 6.
• Suppose we are given the name of a member and we want to find his or her
telephone number.
• One way to do this is to linearly search through the file

Linear Search
• Search each record of the file, one at a time, until finding the given Name and hence
the corresponding telephone number
• time required to execute the algorithm is proportional to the number of comparisons
• assuming that each name in the file is equally likely to be picked, it is intuitively clear
that the average number of comparisons for a file with n records is equal to n/2
• complexity of the linear search algorithm is C(n) = nl2.
• above algorithm would be impossible in practice if we were searching through a list in a telephone book.
• if the names are sorted alphabetically, then we can use an efficient algorithm called binary search.

Binary Search
• Compare the given Name with the name in the middle of the list; this tells which half of the list contains Name.
• Then compare Name with the name in the middle of the correct half to determine which quarter of the list contains Name. Continue the process until finding Name in the list.
• complexity of the binary search algorithm is C(n) = log2 n
6.2 Preliminaries
6.2.1 Mathematical Notation and Functions
Floor and Ceiling Functions
• Let x be any real number. Then x lies between two integers called the floor and the ceiling of x.
 x, called the floor of x, denotes the greatest integer that does not exceed x.
 x , called the ceiling of x, denotes the least integer that is not less than x.
• If x is itself an integer, then x = x ;
otherwise x +1= x.
Example 7

3.14 = 3, 5 = 2, -8.5 = -9, 7 = 7

3.14 = 4, 5 = 3, -8.5 = -8, 7 =7


Remainder Function; Modular Arithmetic
• Let k be any integer and let M be a positive integer. Then
k (mod M)
(read k modulo M) will denote the integer remainder when k is divided by M. More exactly,
k (mod M) is the unique integer r such that
k = Mq + r where 0r<M
• When k is positive, simply divide k by M to obtain the remainder r. Thus
25 (mod 7) = 4, 25 (mod 5) = 0, 35 (mod 11) = 2,
3 (mod 8) = 3
Integer and Absolute Value Functions
• Let x be any real number. The integer value of x, written INT(x), converts x into an integer by (truncating)
the fractional part of the number. Thus
INT(3.14) = 3, INT(5) = 2, INT(-8.5) = -8, INT(7) = 7
Observe that INT(x) = x or INT(x) = x according to whether x is positive or negative.
• The absolute value of the real number x, written ABS(x) or x, is defined as the greater of x or -x. Hence
ABS(0) = 0, and, for x 0, ABS(x) = x or ABS(x) = -x, depending on whether x is positive or negative. Thus
-15 = 15, 7 = 7, -3.33 = 3.33, 4.44 = 4.44,
-0.075 = 0.075
We note that x = -x and, for x  0, x is positive.
Summation Symbol; Sums
• Consider a sequence a1, a2, a3, ..... Then the sums
a1 + a2 + ···· + an and am + am+1 + ···· + an
will be denoted, respectively, by

and

n n

a
• The letter j is a dummy index or dummy variable. Other letters frequently used as dummy variables

a
are i, k, s and t.

j 1
j j
j m
Example 8
n

a b
i 1
i i  a1b1  a 2 b2       a n bn
5


j 2
j 2  2 2  3 2  4 2  5 2  4  9  16  25  54
n

 j  1 2   n
i 1

n(n  1)
1 2   n 
2
Factorial Function
• The product of the positive integers from 1 to n, inclusive, is denoted by n! (read "n factorial").
That is,
n! = 1·2·3 ···(n - 2)(n - 1)n
It is also convenient to define 0! = 1.

Example 9
(a) 2! = 1·2 = 2; 3! = 1·2·3 = 6; 4! = 1·2·3·4 =24
(b) For n > 1, we have n! = n · (n -1)! Hence
5! = 5 · 4! = 5 · 24 = 120;
6! =6·5! = 6·120 = 720
Permutations
• A permutation of a set of n elements is an arrangement of
the elements in a given order.
• For example, the permutations of the set consisting of the
elements a, b, c are as follows:
abc, acb, bac, bca, cab, cba
• There are n! permutations of a set of n elements .
4! = 24 permutations of a set with 4 elements,
5! = 120 permutations of a set with 5 elements, and so on.
Exponents and Logarithms
• Recall the following definitions for integer exponents
(where m is a positive integer):
1
am = a · a ··· a (m times), a0 = 1, a-m =
am
Exponents are extended to include all rational numbers by
defining, for any rational number m/n,

a m/n
 an m
 ( a)
n m

For example, 1 1
24 = 16, 2-4 = 4
 , 1252/3 = 52 = 25
2 16
• exponents are extended to include all real numbers by defining, for
any real number x.

where r xis a rational number


a  lim a r
rx
• exponential function f(x) = ax is defined for all real numbers
• Let b be a positive number. The logarithm of any positive number x to
the base b, written
logb x
represents the exponent to which b must be raised to obtain x. That is,
y = logb x and by = x
are equivalent statements.
log2 8 = 3 since 23 = 8; log10 100 = 2 since 102 = 100
log2 64 = 6 since 26 = 64;
log10 0.001 = -3 since 10-3 = 0.001
Furthermore, for any base b,
logb 1 = 0 since b0 = 1
logb 1 = 0 since b0 = 1
logb b = 1 since b1 = b
The logarithm of a negative number and the logarithm of 0 are not define.
One may also view the exponential and logarithmic functions
f(x) = bx and g(x) = logb x
as inverse functions of each other.
• logarithms are expressed using approximate values. For example, using
tables or calculators, one obtains
log10 300 = 2.4771 and loge 40 = 3.6889
as approximate answers. (Here e = 2.718281....)
• logarithms to the base 10 (called common logarithms)
• logarithms to the base e (called natural logarithms)
• logarithms to the base 2 (called binary logarithms)
ln x instead of loge x
lg x or Log x instead of log2 x
This text on data structures is mainly concerned with binary logarithms.
Accordingly,
The term log x shall mean log2 x unless otherwise specified.
Frequently, we will require only the floor or the ceiling of a binary logarithms.
For example,
log2 100 = 6 since 26 = 64 27 = 128
log2 1000 = 9 since 28 = 512 and 29 = 1024 and so on.
6.2.2 Algorithmic Notation
Example 10
• array DATA of numerical values is in memory
• to find the location LOC and the value MAX of the largest
element
• Given no other information about DATA

• Initially begin with LOC = 1 and MAX = DATA[l].


• Then compare MAX with each successive element DATA[K] of
DATA.
• If DATA[K] exceeds MAX, then update LOC and MAX so that
LOC = K and MAX = DATA[K].
• The final values appearing in LOC and MAX give the location
and value of the largest element of DATA.
Algorithm 1: (Largest Element in Array) A nonempty array DATA with N
numerical values is given. This algorithm finds the location
LOC and the value MAX of the largest element of DATA. The
variable K is used as a counter.
Step 1. [Initialize.] Set K:= 1, LOC:= 1 and MAX:= DATA[l].
Step 2. [Increment counter.] Set K:= K + 1.
Step 3. [Test counter.] If K > N, then:
Write: LOC, MAX, and Exit.
Step 4. [Compare and update.] If MAX < DATA[K], then:
Set LOC: = K and MAX:= DATA[K].
Step 5. [Repeat loop.] Go to Step 2.
Start

K1
LOC  1
MAX  DATA[1]

K  K+1

Yes
Is K>N ? Write: LOC, MAX

No
STOP
No
Is MAX<DATA[K] ?

Yes
LOC  K
MAX  DATA[K]
Identifying Number
Steps, Control, Exit
• The steps of the algorithm are executed one after the other, beginning
with Step 1
• Control may be transferred to Step n of the algorithm by the statement
"Go to Step n."
• If several statements appear in the same step, e.g.,
Set K := 1, LOC := 1 and MAX := DATA[l].
then they are executed from left to right.
The algorithm is completed when the statement
Exit.
Comments
• Each step may contain a comment in brackets which indicates the main
purpose of the step.
• usually appear at the beginning or the end of the step.

Variable Names
• use capital letters, as in MAX and DATA
• single-letter names of variables used as counters or subscript will be
capitalized in the algorithms (K and N, for example)
• even through lowercase may be used for these same variables (k and n) in
the accompanying mathematical description and analysis.
Assignment Statement
• use the dots-equal notation :=
For example,
MAX := DATA[1]
assigns the value in DATA[1] to MAX.

Input and Output


• Data may be input and assigned to variables by means of a Read statement
Read: Variables names.
Similarly, messages, placed in quotation marks, and data in variables may be output
by means of a Write or Print statement
Write: Messages and/or variable names.
Procedures
• used for an independent algorithmic module which solves a particular problem.
• "procedure" or "module" rather than "algorithm" for a given problem is simply a matter of taste.
• "algorithm" will be reserved for the solution of general problems.
• "procedure" will also be used to describe a certain type of subalgorithm.
6.2.3 Control Structures

(1) Sequence logic, or sequential flow


(2) Selection logic, or conditional flow
(3) Iteration logic, or repetitive flow
(1) Sequence Logic (Sequential Flow)
• The sequence may be presented explicitly, by means of numbered steps, or implicitly, by the order in which the modules are written.
Algorithm Flowchart equivalent

Module A

Module B

Module C

Module A

Module B

Module C
(2) Selection Logic (Conditional Flow)

• employs a number of conditions which lead to a selection of


one out of several alternative modules
• conditional structures or If structures
• end of such a structure by the statement
[End of If structure.]

3 Types of Sturctures

(1) Single alternative

If condition, then:
[Module A]
[End of If structure .]
• If the condition holds, then Module A, which may consist of
one or more statements, is executed; otherwise Module A is
skipped and control transfers to the next step of the
algorithm.

No
Condition ? No
Condition ?

Yes Yes
Module A Module A Module B

(a) Single alternative (b) Double alternative


(2) Double alternative

If condition, then:
[Module A]
Else:
[Module B]
[End of If structure.]

if the condition holds, then Module A is executed;


otherwise Module B is executed.
(3) Multiple alternatives

If condition(1), then:
[Module A1]
Else if condition(2), then:
[Module A2]
.
.
Else if condition(M), then:
[Module AM]
Else:
[Module B]
[End of If structure.]
Example 11
The solutions of the quadratic equation
ax2 + bx + c = 0
where a  0, are given by the quadratic formula

• The quantity D = b2 - 4ac is called the discriminant


• If D is negative, then there are no real solutions.
• If D = 0, then there is only one (double) real solution,
x = -b/2a.
• If D is positive, the formula gives the two distinct real
solutions.
Algorithm 2: (Quadratic Equation) This algorithm inputs the coefficients A, B, C
of a quadratic equation and outputs the real solutions, if any.
Step 1. Read: A, B,C.
Step 2. Set D := B2 - 4AC.
Step 3. If D > 0, then:
(a) Set X1 := (-B + )/2A and X2 := (-B - )/2A.
(b) Write: X1, X2.
Else if D = 0, then:
(a) Set X:= -B/2A.
(b) Write: 'UNIQUE SOLUTION', X.
Else:
Write: 'NO REAL SOLUTIONS'.
[End of If structure.]
Step 4. Exit.
(3) Iteration Logic (Repetitive Flow)
• repeat-for loop uses an index variable, such as K, to control the
loop. The loop will usually have the form:
Repeat for K = R to S by T:
[Module]
[End of loop.]
R - initial value
S - end value or test value
T – increment
• Observe that the body of the loop is executed first with K = R,
then with K = R + T, then with K = R + 2T, and so on. The cycling
ends when K > S.
K←R
No
Condition ?

Yes
Is K > S ? Yes

Module
No (body of loop)
Module
(body of loop)

K ← K+T

(a) Repeat-For structure (b) Repeat-While structure


The flowchart assumes that the increment T is positive;
if T is negative, so that K decreases in value, then the
cycling ends when K < S.
• The repeat-while loop uses a condition to control the loop.
The loop will usually have the form
Repeat while condition:
[Module]
[End of loop.]
• Observe that the cycling continues until the condition is
false.
Example 12
Algorithm 1 is rewritten using a repeat-while loop rather than a Go to statement:
Algorithm 3: (Largest Element in Array) Given a nonempty array DATA with N numerical values, this algorithm finds the location LOC and the value MAX of the largest element of DATA.
1. [Initialize.] Set K:= 1, LOC:= 1 and MAX:= DATA[l].
2. Repeat Steps 3 and 4 while K  N:
3. If MAX < DATA[K], then:
Set LOC: = K and MAX:= DATA[K].
[End of If structure.]
4. Set K:= K + 1.
[End of Step 2 loop.]
5. Write: LOC, MAX.
6. Exit.
6.3 Arrays, Records and Pointers
• classified as either linear or nonlinear
• linear if its elements form a sequence, or, in other words, a linear
list
• two basic ways of representing such linear structures in memory
• to have the linear relationship between the elements
represented by means of sequential memory locations,
arrays
• to have the linear relationship between the elements
represented by means of pointers or links, linked lists
(a) Traversal. Processing each element in the list
(b) Search. Finding the location of the element with a given
value or the record with a given key.
(c) Insertion. Adding a new element to the list.
(d) Deletion. Removing an element from the list.
(e) Sorting. Arranging the elements in some type of order.
(f) Merging. Combining two lists into a single list.
6.3.1 Linear Arrays

• A linear array is a list of a finite number n of homogeneous


data elements (i.e., data elements of the same type) such
that:
(a) referenced respectively by an index set consisting of n
consecutive numbers.
(b) stored respectively in successive memory locations.

• The number n of elements is called the length or size of the


array.
• length or number of data elements of the array
Length = UB-LB+1
where
UB is the largest index, called the upper bound
LB is the smallest index, called the lower bound
The elements of an array A may be denoted by the subscript notation
A1, A2, A3, . . . An
or by the parentheses notation
A(1), A(2), A(3), . . . , A(N)
or by the bracket notation
A[1], A[2], A[3], . . . , A[N]
Example 13
(a) Let DATA be a 6 element linear array of integers such that
DATA[1]=247 DATA[2]=56 DATA[3]=429
DATA[4]=135 DATA[5]=87 DATA[6]=156
Sometimes we will denote such an array by simply writing
DATA: 247, 56, 429, 135, 87, 156
The array DATA is frequently pictured as
DATA
1 247
DATA 2 56
3 429
247 56 429 135 87 156
4 135
5 87
6 156
(b) An automobile company uses an array AUTO to record the
number of automobiles sold each year from 1932 through
1984. Rather than beginning the index set with 1, it is
more useful to begin the index set with 1932 so that
AUTO[K] = number of automobiles sold in the year K
Then LB = 1932 is the lower bound and UB = 1984 is the
upper bound of AUTO.
Length = UB - LB + 1 = 1984-1932+ 1 = 53
That is, AUTO contains 53 elements and its index set
consists of all integers from 1932 through 1984.
Example 14
(a) Suppose DATA is a 6-element linear array containing real values.
Various programming languages declare such an array as follows:
FORTRAN: REAL DATA(6)
Pascal: VAR DATA: ARRAY[l.. 6] OF REAL

(b) Consider the integer array AUTO with lower bound LB=1932 and
upper bound UB=1984.
FORTRAN 77 INTEGER AUTO(1932:1984)
Pascal VAR AUTO: ARRAY[1932..1984] of INTEGER
6.3.2 Representation of Linear Arrays in Memory
• Let LA be a linear array in the memory of the compute.
• LOC(LA[K]) = address of the element LA[K] of the array LA
• elements of LA are stored in successive memory cells.
• computer does not need to keep track of the address of every element of LA, but needs to keep track only of the address of the first element of LA, denoted by
Base( LA)
base address of LA
• Using this address Base(LA), the computer calculates the address of any element of LA by the following formula:
LOC(LA[K]) = Base(LA) + w(K - lower bound)
where w is the number of words per memory cell for the array LA.
Example 15
Consider the array AUTO in Example 14(b), which records the number
of automobiles sold each year from 1932 through 1984. Suppose AUTO
appears in memory as pictured in the following figure.
That is, Base(AUTO) = 200, and w= 4 words per memory cell for AUTO.
Then
LOC(AUTO[1932]) = 200, LOC(AUTO[1933])=204,
LOC(AUTO[1934])=208, . . .
The address of the array element for the year K=1965 can be obtained
LOC(AUTO[1965]) = Base(AUTO) + w(1965-lower bound)
=200+4(1965-1932)=332
200
201
202 AUTO[1932]
203
204
205
206 AUTO[1933]
207
208
209
210 AUTO[1934]
211
. .
. .
. .
6.3.3 Traversing Linear Arrays

• Let A be a collection of data elements stored in the memory


of the computer.
• Suppose we want to print the contents of each element of
A or suppose we want to count the number of elements of
A with a given property.
• This can be accomplished by traversing A, that is, by
accessing and processing (frequently called visiting ) each
element of A exactly once.
Algorithm 4. (Traversing a Linear Array) Here LA is a linear
array with lower bound LB and upper bound UB. This
algorithm traverses LA applying an operation PROCESS to
each element of LA.
1. [Initialize counter.] Set K:=LB.
2. Repeat Steps 3 and 4 while KUB:
3. [Visit element.] Apply PROCESS to LA[K].
4. [Increase counter.] Set K:=K+1.
[End of Step 2 loop.]
5. Exit.

We also state an alternative form of the algorithm which


uses a repeat-for loop instead of the repeat-while loop.
Algorithm 4':(Traversing a Linear Array) This algorithm
traverses a linear array LA with lower bound LB and upper
bound UB.
1. Repeat for K = LB to UB:
Apply PROCESS to LA[K].
[End of loop.]
2. Exit.
Example 16
Consider the array AUTO in Example 14(b), which records the
number of automobiles sold each year from 1932 through 1984. Each of the
following modules, which carry out the given operation. involves traversing
AUTO.
(a) Find the number NUM of years during which more than 300
automobiles were sold.
1. [Initialization step.] Set NUM:= 0.
2. Repeat for K =1932 to 1984:
If AUTO[K] > 300, then: Set NUM: = NUM + 1.
[End of loop.]
3. Return.
(b) Print each year and the number of automobiles sold in that
year.
1. Repeat for K=1932 to 1984:
Write: K, AUTO[K].
[End of loop.]
2. Return.
6.3.4 Inserting and Deleting
• A be a collection of data elements in the memory of the
computer.
• "Inserting" refers to the operation of adding another element
to the collection A,
• "deleting" refers to the operation of removing one of the
elements from A.
• Inserting an element at the "end" of a linear array can be easily
done provided the memory space allocated for the array is large
enough to accommodate the additional element.
• On the other hand, suppose we need to insert an element in the
middle of the array. Then, on the average, half of the elements
must be moved downward to new locations to accommodate
the new element and keep the order of the other elements.
• Similarly, deleting an element at the "end" of an array
presents no difficulties, but deleting an element
somewhere in the middle of the array would require that
each subsequent element be moved one location upward to
order to "fill up" the array.
Example 17
Suppose TEST had been declared to be a 5-element array
but data have been recorded only for TEST[1], TEST[2] and
TEST[3]. if X is the value of the next test, then one simply assigns
TEST[4]:=X
to add X to the list. Similarly, if Y is the value of the subsequent
test, then we simply assign
TEST[5]:= Y
to add Y to the list. Now, however, we cannot add any new test
scores to the list.
Example 18
• NAME is an 8-element linear array,
• five names are in the array, as in figure(a).
• the names are listed alphabetically, and
• want to keep the array names alphabetical at all times.
• Suppose Ford is added to the array. Then Johnson, Smith and Wagner
must each be moved downward one location, as in figure (b).
• Next suppose Taylor is added to the array; then Wagner must be
moved, as in figure (c).
• Last, suppose Davis is removed from the array. Then the five names
Ford, Johnson, Smith, Taylor and Wagner must each be moved upward
one location, as in figure(d).
• Clearly such movement of data would be very expensive if thousands of
names were in the array.
NAME NAME NAME NAME
1 Brown 1 Brown 1 Brown 1 Brown
2 Davis 2 Davis 2 Davis 2 Ford
3 Johnson 3 Ford 3 Ford 3 Johnson

4 Smith 4 Johnson 4 Johnson 4 Smith

5 Wagner 5 Smith 5 Smith 5 Taylor

6 6 Wagner 6 Taylor 6 Wagner


7 7 7 Wagner 7
8 8 8 8

(a) (b) (c) (d)


• The following algorithm inserts a data element ITEM into the
Kth position in a linear array LA with N elements.
• The first four steps create space in LA by moving downward one
location each element from the Kth position on.
• We emphasize that these elements are moved in reverse order
-i.e., first LA[N], then, LA[N - 1], . . . , and last LA[K]; otherwise
data might be erased.
• In more detail, we first set J := N and then, using J as a counter,
decrease J each time the loop is executed until J reaches K.
• The next step, Step 5, inserts ITEM into the array in the space
just created.
• Before the exit from the algorithm, the number N of elements in
LA is increased by 1 to account for the new element.
Algorithm 5: (Inserting into a Linear Array) INSERT(LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such
that K≤N. This algorithm inserts an element ITEM into the Kth position in
LA.
1. [Initialize counter.] Set J:=N.
2. Repeat Steps 3 and 4 while JK:
3. [Move Jth element downward.] Set LA[J+1]:= LA[J].
4. [Decrease counter.] Set J:=J-1.
[End of Step 2 loop.]
5. [insert element.] Set LA[K] := ITEM.
6. [Reset N.] Set N:=N+1.
7. Exit.
Algorithm 6: (Deleting from a Linear Array) DELETE(LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive
integer such that K≤ N. This algorithm deletes the Kth element
from LA.
1. Set ITEM:= LA[K].
2 . Repeat for J = K to N - 1:
[Move J + 1st element upward.] Set LA[J]:= LA[J + 1].
[End of loop.]
3. [Reset the number N of elements in LA.] Set N:= N - 1.
4. Exit.
6.3.5 Sorting; Bubble Sort
• Let A be a list of n numbers.
• Sorting A refers to the operation of rearranging the elements of A
so they are in increasing order, i.e., so that
A[l] < A[2] < A[31 < ... < A[N]

For example, suppose A originally is the list


8, 4, 19, 2, 7, 13, 5, 16
After sorting, A is the list
2, 4, 5, 7, 8, 13, 16, 19
Sorting may seem to be a trivial task. Actually, sorting efficiently
may be quite complicated. In fact, there are many, many different
sorting algorithms. Here we present and discuss a very simple
sorting algorithm known as the bubble sort.
Suppose the list of numbers A[1], A[2], . . . , A[N] is in memory. The bubble
sort algorithm works as follows:
 Step 1. Compare A[1] and A[2] and arrange them in the desired
order, so that A[1]<A[2]. Then compare A[2] and A[3] and arrange
them so that A[2]<A[3]. Then compare A[3] and A[4] and arrange
them so that A[3]<A[4]. Continue until we compare A[N-1] with
A[N] and arrange them so that A[N-1]<A[N].
 Observe that step 1 involves N-1 comparisons. (During Step 1, the
largest element is "bubbled up" to the nth position or "sinks" to the
nth position.) When Step 1 is completed, A[N] will contain the
largest element.
 Step 2. Repeat Step 1 with one less comparison; that is, now we
stop after we compare and possibly rearrange A[N-2] and A[N-1].
(Step 2 involves N-2 comparisons and when Step 2 is completed,
the second largest element will occupy A[N-1].)
 Step 3. Repeat Step 1 with two fewer comparisons; that is
we stop after we compare and possibly rearrange A[N-3]
and A[N-2].
.
.
 Step N-1. Compare A[1] with A[2] and arrange them so
the A[1]<A[2].
 After N-1 steps, the list will be sorted in increasing order.
 The process of sequentially tranversing through all or part
of a list is frequently called a "pass", so each of the above
steps is called a pass. Accordingly, the bubble sort
algorithm requires N-1 passes, where N is the number of
input items.
Example 19.
Suppose the following numbers are stored in an array A:
32, 51, 27, 85, 66, 23, 13, 57
We apply the bubble sort to the array A. We discuss each pass separately.
Pass 1. We have the following comparisons:
(a) Compare A1 and A2. Since 32<51, the list is not altered.
(b) Compare A2 and A3. Since 51>27, interchange 51 and 27 as
follows:
32, 27, 51, 85, 66, 23, 13, 57
(c) Compare A3 and A4. Since 51<85, the list is not altered.
(d) Compare A4 and A5. Since 85>66, interchanges 85 and 66 as
follows:
32, 27, 51, 66, 85, 23, 13, 57
(e) Compare A5 and A6. Since 85>23, interchange 85 and
23 as follows:
32, 27, 51, 66, 23, 85, 13, 57
(f) Compare A6 and A7. Since 85>13, interchange 85 and
13 to yield:
32, 27, 51, 66, 23, 13, 85, 57
(g) Compare A7 and A8. Since 85>57, alterchange 85 and
57 to yield:
32, 27, 51, 66, 23, 13, 57, 85
At the end of this first pass, the largest number, 85, has
moved to the last position. However, the rest of the numbers
are not sorted, even though some of changed their positions.
Pass 2. 27, 32, 51, 66, 23, 13, 57, 85

27, 32, 51, 23, 66, 13, 57, 85

27, 32, 51, 23, 13, 66, 57, 85

27, 32, 51, 23, 13, 57, 66, 85

At the end of Pass 2, the second largest number, 66, has


moved its way down to the next-to-last position.
Pass 3. 27, 32, 23, 51, 13, 57, 66, 85

27, 32, 23, 13, 51, 57, 66, 85

Pass 4. 27, 23, 32, 13, 51, 57, 66, 85

27, 23, 13, 32, 51, 57, 66, 85

Pass 5. 23, 27, 13, 32, 51, 57, 66, 85

23, 13, 27, 32, 51, 57, 66, 85


Pass 6 actually has two comparisons, A1 with A2 and A2 with A3. The
second comparison is not involve an interchange.
Pass 7. Finally, A1 is compared with A2. Since 13 < 23, no interchange
takes place.
Since the list has 8 elements; it is sorted after the seventh pass.
Algorithm 7: (Bubble Sort) BUBBLE(DATA, N)
Here DATA is an array with N elements. This algorithm sorts the elements in DATA.
1. Repeat Steps 2 and 3 for K = 1 to N - 1:
2. Set PTR := 1. [Initializes pass pointer PTR.]
3. Repeat while PTR  N - K: [Executes pass. ]
(a) If DATA[PTR] > DATA[PTR + 1], then:
Interchange DATA[PTR] and DATA[PTR + 1].
[End of If structure.]
(b) Set PTR := PTR + 1.
[End of inner loop.]
[End of Step 1 outer loop.]
4. Exit.
Complexity of the Bubble Sort Algorithm
• Traditionally, the time for a sorting algorithm is measured in terms of
the number of comparisons.
• The number f(n) of comparisons in the bubble sort is easily computed.
• Specifically, there are n-1 comparisons during the first pass, which
places the largest element in the last position; there are n-2
comparisons in the second step, which places the second largest
element in the next-to-last positions and so on. Thus
f(n)=(n-1)+(n-2)+ . . . . +2+1== O(n2)
In other words, the time required to execute the the bubble sort
algorithm is proportional to n2, where n is the number of input items.
6.3.6 Searching; Linear Search

• Let DATA be a collection of data elements in memory, and suppose a


specific ITEM of information is given.
• Searching refers to the operation of finding the location LOC of ITEM
in DATA, or printing some message that ITEM does not appear there.
• The search is said to be successful if ITEM does appear in DATA and
unsuccessful otherwise.
• Frequently, one may want to add the element ITEM to DATA after an
unsuccessful search for ITEM in DATA. One then uses a search and
insertion algorithm, rather than simply a search algorithm.
Linear Search

• Suppose DATA is a linear array with n elements.


• first we test whether DATA[1] =ITEM, and then we test
whether DATA[2] = ITEM, and so on.
• traverses DATA sequentially to locate ITEM, is called linear
search or sequential search.
• To simplify the matter, we first assign ITEM to DATA[N + 1],
the position following the last element of DATA. Then the
outcome
LOC = N + 1
Algorithm 7: (Linear Search) LINEAR(DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is a given
item of information. This algorithm finds the location LOC of ITEM in DATA, or
sets LOC := 0 if the search is unsuccessful.
1. [Insert ITEM at the end of DATA.] Set DATA[N + 1]:= ITEM.
2. [Initialize counter.] Set LOC:= 1.
3. [Search for ITEM.]
Repeat while DATA[LOC]  ITEM:
Set LOC: = LOC + 1.
[End of loop.]
4. [Successful?] If LOC = N + 1, then: Set LOC:=0.
5. Exit.
Example 20
Consider the array NAME, where n = 6.
(a) Suppose we want to know whether Paula appears in the array and, if
so, where. Our algorithm temporarily places Paula at the end of the
array, by setting NAME[7] = Paula. Then the item searches the array
from top to bottom. Since Paula first appears in NAME[N +1], Paula
is not in original array.
(b) Suppose we want to know whether Susan appears in the array and if
so, where. Our algorithm temporarily places Susan at the end of the
array, by setting NAME[7] = Susan. Then the algorithm searches the
array from top to bottom. Since Susan first appears in NANIE[4]
(where 4 n), we know that Susan is in the original array.
NAME NAME NAME

1 Mary 1 Mary 1 Mary


2 Jane 2 Jane 2 Jane
3 Diane 3 Diane 3 Diane
4 Susan 4 Susan 4 Susan
5 Karen 5 Karen 5 Karen
6 Edith 6 Edith 6 Edith
7 7 Paula 7

8 8 8
Complexity of the Linear Search Algorithm
• Two important cases to consider are the average case and
the worst case.
• Clearly the worst case occurs when one must search
through the entire array DATA, i.e., when ITEM does not
appear in DATA. In this case, the algorithm requires
f(n) = n + 1 comparisons. Thus, in the worst case, the
running time is proportional to n.
• The running time of the average case uses the probabilistic
notion of expectation. Suppose pk is the probability that
ITEM appears in DATA[K], and suppose q is the probability
that ITEM does not appear in DATA. (Then p1 + p2 + .... +
pn + q = 1.) Since the algorithm uses k comparisons when
ITEM appears in DATA[K], the average number of
comparisons is given by
f(n) = 1 · p1 + 2 · p2 + ... + n· pn + (n +1) · q
• In particular, suppose q is very small and ITEM appears
with equal probability in each element of DATA. Then q  0
and each pi, = 1/n. Accordingly,
f(n) = (n+1)/2
6.3.7 Binary Search

• During each stage our algorithm, our search for ITEM is


reduced to a segment of elements of DATA:
DATA[BEG], DATA[BEG + 1], DATA[BEG + 2],..., DATA[END]
• Note that the variables BEG and END denote, respectively,
the beginning and end locations of the segment under
consideration. The algorithm compares ITEM with the middle
element DATA[MID] of the segment, where MID is obtained
by
MID = INT((BEG + END) /2)
• If DATA[MID] = ITEM, then the search is successful and we set
LOC:= MID.
• Otherwise a new segment of DATA is obtained as follows:
(a) If ITEM < DATA[MID], then ITEM can appear only in the left
half of the segment:
DATA[BEG], DATA[BEG +1], . . . , DATA[MID - 1]
So we reset END:= MID - 1 and begin searching again.
(b) If ITEM > DATA[MID], then ITEM can appear only in the right
half of the segment:
DATA[MID + 1], DATA[MID + 2], . . . , DATA[END]
So we reset BEG:= MID + 1 and begin searching again.
Algorithm 8: (Binary Search) BINARY(DATA, LB, UB, ITEM,
LOC)
Here DATA is a sorted array with lower bound LB and upper
bound UB, and ITEM is a given item of information. The
variables BEG, END and MID denote, respectively, the
beginning, end and middle locations of a segment of elements
of DATA. This algorithm finds the location LOC of ITEM in
DATA or sets LOC = NULL.
1 .[Initialize segment variables.]
Set BEG:= LB, END:= UB and MID= INT ((BEG+ END) /2).
2. Repeat Steps 3 and 4 while BEG  END and DATA[MID]  ITEM:
3. If ITEM < DATA[MID], then:
Set END:= MID - 1.
Else:
Set BEG:= MID + 1.
[End of If structure.]
4. Set MID:= INT((BEG + END) /2).
[End of Step 2 loop.]
5. If DATA[MID] = ITEM, then:
Set LOC: = MID.
Else:
Set LOC: =NULL.
[End of If structure.]
6. Exit.
Example 21
Let DATA be the following sorted 13-element array:
DATA: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(a) Suppose ITEM = 40.
(1) Initially, BEG = 1 and END = 13, Hence
MID = INT[(1 + 13)/2] = 7 and so DATA[MID] = 55
(2) Since 40 < 55, END has its value changed by END = MID - 1 = 6. Hence
MID = INT[(1 + 6)/2] = 3 and so DATA[MID] = 30
(3) Since 40 > 30, BEG has its value changed by BEG = MID + 1 = 4. Hence
MID = INT[(4+6)/1] = 5 and so DATA[MID] = 40
We have found ITEM in location LOC = MID = 5
(1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
[Successful]
(b) Suppose ITEM = 85.
(1) Again initially, BEG = 1, END = 13, MID = 7 and DATA[MID] =
55.
(2) Since 85 > 55, BEG has its value changed by BEG = MID + 1 =
8. Hence
MID = INT[(8 + 13)/2] = 10 and so DATA[MID] = 77
(3) Since 85 > 77, BEG has its value changed by BEG = MID +
1 = 11. Hence
MID = INT[(11 + 13) /2] = 12 and so DATA[MID] = 88
(4) Since 85 < 88, END has its value changed by END = MID -
1 = 11. Hence
MID = INT[(11 + 11)/2] = 11 and so DATA[MID] = 80
(Observe that now BEG = END = MID = 11.)
Since 85 > 80, BEG has its value changed by BEG = MID + 1 =
12. But now BEG > END. Hence ITEM does not belong to DATA.
(1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

(4) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
[Unsuccessful]
Complexity of the Binary Search Algorithm

• each comparison reduces the sample size in half. Hence


we require at most f(n) comparisons to locate ITEM where
2f(n) > n or equivalently f(n) = [log2 n] + 1
• That is, the running time for the worst case is
approximately equal to log2 n. One can also show that the
running time for the average case is approximately equal
to the running time for the worst case.
Example 22
Suppose DATA contains 1000000 elements. Observe that

210 = 1024>1000 and hence 220 > 10002 = 1000000

Accordingly, using the binary search algorithm, one


requires only about 20 comparisons to find the location of
an item in a data array with 1000000 elements.
6.3.8 Multidimensional Arrays

Two-Dimensional Arrays
• A two-dimensional m  n array.
• A is a collection of m  n data elements such that each element is
specified by a pair of integers (such as J, K), called subscripts,
with the property that
1Jm and 1Kn
The element of A with first subscript j and second subscript k
will be denoted by
AJ,K or A[J, K]
• called matrices in mathematics and
• tables in business applications
• sometimes called matrix arrays
Columns

1 2 3 4

1A[1,1]A[1, 2] A[1, 3] A[1, 4]

Rows 2 A[2,1] A[2, 2] A[2, 3] A[2, 4]

3A[3,1]A[3, 2] A[3, 3] A[3, 4]

Two-dimensional 3  4 array A.
Example 23
Suppose each student in a class of 25 students is given 4
tests. Assuming the students are numbered from 1 to 25, the
test scores can be assigned to a 25  4 matrix array SCORE.
Thus SCORE[K, L] contains the Kth student's score on the Lth
test. In particular, the second row of the array.
SCORE[2,1], SCORE[2,2], SCORE[2,3], SCORE[2, 4]
contains the four test scores of the second student.
Student Test 1 Test 2 Test 3 Test 4

1 84 73 88 81

2 95 100 88 96

3 72 66 77 72

. . . . .

. . . . .

25 78 82 70 85

Array SCORE
Representation of Two-Dimensional Arrays in Memory
• Let A be a two-dimensional m  n array.
• Although A is pictured as a rectangular array of elements with
m rows and n columns, the array will be represented in memory
by a block of m  n sequential memory locations.
• Specifically, the programming language will store the array A
either
(1) column by column, is what is called column-major order, or
(2) row by row, in row-major order.
• The following figure shows these two ways when A is a two-
dimensional 3  4 array.
A Subscripts A Subscripts
(1, 1) (1, 1)
(2, 1) Column 1 (1, 2) Row 1
(3, 1) (1, 3)
(1,2) (1, 4)
(2, 2) Column 2 (2,1)
(3, 2) (2, 2) Row 2
(1, 3) (2, 3)
(2, 3) Column 3 (2, 4)
(3, 3) (3, 1)
(1, 4) (3, 2) Row 3
(2, 4) Column 4 (3, 3)
(3, 4) (3, 4)
(a) Column-major order. (b) Row-major order.
• Recall that, for a linear array LA, the computer does not
keep track of the address LOC(LA[K]) of every element
LA[K] of LA, but does keep track of Base(LA), the address
of the first element,of LA. The computer uses the formula
LOC(LA[K]) = Base(LA) + w(K - 1)
• to find the address of LA[K] in time independent of K.
(Here w is the number of words per memory cell for the
array LA, and 1 is the lower bound of the index set of LA.)
• A similar situation also holds for any two-dimensional m  n
array A. That is, the computer keeps track of Base(A)-the
address of the first element A[1, 1] of A- and computes the
address LOC(A[J, K]) of A[J, K] using the formula
(Column-major order)
LOC(A[J, K]) = Base(A) + w[M(K - 1) + (J - 1)]
or
(Row-major order)
LOC(A[J, K]) = Base(A) + w[N(J - 1) + (K - 1)]
Example 24
Consider the 25 x 4 matrix array SCORE in example 23.
Suppose Base(SCORE) = 200 and there are w= 4 words per
memory cell. Furthermore, suppose the programming language
stores two-dimensional arrays using row-major order.
Then the address of SCORE[12, 3], the third test of the twelfth
student, follows.
LOC(SCORE[12, 3]) = 200 +4[4(12 - 1)+ (3 - l)]
= 200 + 4[46] = 384
6.3.9 Pointers; Pointer Array
• DATA be any array.
• A variable P is called a pointer if P "points" to an element in DATA,
i.e., if P contains the address of an element in DATA.
• Analogously, an array PTR is called a pointer array if each element
of PTR is a pointer.
• Pointers and pointer arrays are used to facilitate the processing of
the information in DATA.
• Consider an organization which divides its membership list into
four groups, where each group contains an alphabetized list of
those members living in a certain area. Observe that there are 21
people and the groups contain 4, 9, 2 and 6 people, respectively.
Group 1 Group 2 Group 3 Group 4
Evans Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Troy
Wagner
• Suppose the membership list is to be stored in memory keeping
track of the different groups.
• One way to do this is to use a two-dimensional 4  n array
where each row contains a group, or to use a two-dimensional n
× 4 array where each column contains a group.
• Although this data structure does allow us to access each
individual group, much space will wasted when the groups vary
greatly in size.
• Specifically, the data in the above figure will require at least a
36-element 4  9 or 9  4 array to store the 21 names, which is
almost twice the space that is necessary.
• The following figure shows the representation of the 4 x 9 array;
the asterisks denote data elements and the zeros denote unused
storage locations. (Arrays whose rows- or column- begin with
different numbers of data elements and end with unused storage
location are said to be jagged.)

* * * * 0 0 0 0 0
* * * * * * * * *
* * 0 0 0 0 0 0 0
* * * * * * 0 0 0
Jagged array
• Another way the membership list can be stored in memory is pictured
in figure (a).
• the list is placed in a linear array, one group after another.
• this method is space-efficient. Also, the ist can easily be processed-
one can easily print all the names on the list, for example.
• On the other hand, there is no way to access any particular group;
e.g., there is no way to find and print only the name in the third group.
• A modified version of the above method is pictured in figure (b).
• the names are listed in a linear array; group by group, except now
some sentinel or marker, such as the three dollar signs used here, will
indicate the end of a group.
MEMBER MEMBER
1 Evans 1 Evans
2 Harris Group 1 2 Harris Group 1
3 Lewis 3 Lewis
4 Shaw 4 Shaw
5 Conrad 5 $$$
. Group 2 6 Conrad Group 2
. . .
13 Wagner
14 Davis Group 3 14 Wagner
15 Segal 15 $$$
16 Baker 16 Davis Group 3
. Group 4 17 Segal
. 18 $$$
21 Reed 19 Baker
. . Group 4
. .
24 Reed
25 $$$
• This method uses only a few extra memory cells--one for
each group--but now one can access any particular group.
• For example, a programmer can now find and print those
names in the third group by locating those names which
appear after the second sentinel and before the third
sentinel.
• The main drawback of this representation is that the list
still must be traversed from the beginning in order to
recognize the third group. In other words, the different
groups are not indexed with this representation.
Pointer Arrays
• pointer array which contains the locations of the different
groups or, more specifically, the locations of the first
elements in the different groups.
• Observe that GROUP[L] and GROUP[L + 1]-1 contain,
respectively, the first and last elements in group L.
MEMBER
1 Evans
2 Harris Group 1
GROUP 3 Lewis
1 1 4 Shaw
2 5 5 Conrad
3 14 . Group 2
4 16
5 22 13 Wagner
14 Davis Group 3
15 Segal
16 Baker
. Group 4
.
21 Reed
22 $$$
Example 25
Suppose one wants to print only the names in the Lth group,
where the value of L is part of the input. Since GROUP[L] and
GROUP[L + 1] - 1 contain, respectively, the locations of the first and
last name in the Lth group, the following module accomplishes our
task:
1. Set FIRST := GROUP[L] and LAST := GROUP[L+1] - l.
2. Repeat for K = FIRST to LAST:
Write: MEMBER[K].
[End of loop.]
3. Return.
MEMBER
1 Evans
2 Harris Group 1
GROUP 3 Lewis
1 1 4 Shaw
2 7 5
3 19 6
• A slight variation of 4 23 7 Conrad
the data structure is 8 Felt
. . Group 2
pictured in the
. .
following figure, . .
where unused NUMB 15 Wagne
r
memory cells are
1 4 16
indicated by the 2 9 17
shading. 3 2 18
4 6 19 Davis Group 3
20 Segal
21
22
FREE 23 Baker
1 2 24 Cooper
2 3 . . Group 4
3 2 . .
4 4 28 Reed
29
30
• Observe that now there are some empty cells between the groups.
• Accorrdingly, a new element may be inserted in a group without
necessarily moving the elements in any other group.
• Using this data structure, one requires an array NUMB which gives
the number of elements in each group.
• Observe that GROUP[K + 1] - GROUP[K] is the total amount of space
available for Group K; hence
FREE[K] = GROUP[K + 1] - GROUP[K] - NUMB[K]
is the number of empty cells following GROUP K.
• Sometimes it is convenient to explicitly define the extra array FREE.
Example 26
Suppose, again, one wants to print only the names in the Lth group,
where L is part of the input. Observe that
GROUP[L] and GROUP[L] + NUMB[L] - 1
contain, respectively, the locations of the first and last names in the
Lth group. Thus the following module accomplishes our task:
1. Set FIRST := GROUP[L] and LAST := GROUP[L] + NUMB[L] - 1.
2. Repeat for K = FIRST to LAST:
Write: MEMBER[K].
[End of loop.]
3. Return.
6.3.10 Records; Record Structures
• Collections of data are frequently organized into a
hierarchy of field, records and files.
• Specifically, a record is a collection of related data items,
each of which is called a field or attribute, and a file is a
collection of similar records.
• Each data item may be a group item composed of
subitems, those items which are indecomposable are
called elementary items or atoms or scalars.
• The names given to the various data items are called
identifiers.
• Under the relationship of group item to subitem, the data
items in a record form a hierarchical structure which can
be described by means of "level" numbers

BROWN, JOHN M. M 0416 84 BROWN, TOBERT S. 26 BROWN, SUSAN B. 22


MD Y Name Name
Age Age

Name Sex Birthday Father Mother


Example 27
Suppose a hospital keeps a record on each newborn baby
which contains the following data items:
Name, Sex, Birthday, Father, Mother
Suppose further that Birthday is a group item with
subitems Month, Day and Year, and
Father and Mother are group items, each with subitems
Name and Age.
1 Newborn • The number to the left of each
2 Name
identifier is called a level number.
2 Sex
2 Birthday • each group item is followed by its
3 Month subitems
3 Day • level of the subitems is 1 more than
3 Year
the level of the group item
2 Father
3 Name • an item is a group item if and only if
3 Age it is immediately followed by an
2 Mother
item with a greater level number.
3 Name
3 Age
• Some of the identifiers in a record structure may also refer
to arrays of elements.
• first line of the above structure is replaced by
1 Newborn(20)
• indicate a file of 20 records

Newborn1, Newborn2, Newborn3 , ...

Newborn[l], Newborn[2], Newborn[3],...


Example 28
A class of student records may be • identifier Student(20)
organized as follows: indicates that there are 20
1 Student(20) students.
2 Name • The identifier Test (3)
indicates that there are
3 Last
three tests per student.
3 First
• there are 8 elementary
3 MI (Middle Initial) items per Student,
2 Test(3) • since Test is counted 3
2 Final times. Altogether, there are
2 Grade 160 elementary items in the
entire Student structure.
Indexing Items in a Record
• Suppose we want to access some data item in a record.
• In some cases, we cannot simply write the name of the item
since the same name may appear in different places in the
record.
• For example, Age appears in two places in the record in Example
27.
• Accordingly, in order to specify a particular item, we may have
to qualify the name by using appropriate group item names in
the structure.
• This qualification is indicated by using decimal points (periods)
to separate grouped from subitems.
Example 29
(a) Consider the record structure Newborn in Example 27. Sex and year
need no qualification, since each refers to a unique item in the
structure. On the other hand, suppose we want to refer to the age of
the father. This can be done by writing
Newborn.Father.Age or simply Father.Age
(b) Suppose the first line in the record structure in Example 27 is replaced
by
1 Newborn(20)
Some languages allow the sex of the sixth newborn to be referenced by
writing
Newborn.Sex[6] or simply Sex[6]
Analogously, the age of the father of the sixth newborn may be referenced by
writing
Newborn.Father.Age[6] or simply Father.Age[6]
(c) Consider the record structure Student in Example 28, the second test of the
sixth student may be referenced by writing
Student.Test[6, 2] or simply Test[6, 2]
The order of the subscripts corresponds to the order of the qualifying
identifiers. For example,
Test[3, 1]
does not refer to the third test of the first student, but to the first test of the
third student.
6.3.11 Representation of Records in Memory; Parallel Arrays

• Since records may contain nonhomogeneous data, the


elements of a record cannot be stored in an array.
• Some programming languages, such as PL/1, Pascal and
COBOL, do have record structures built into the language.
Example 30
Consider the record structure Newborn in Example 27. One can store such a record in PL/1 by the
following declaration, which defines a data aggregate called a structure:
DECLARE 1 NEWBORN,
2 NAME CHAR(20),
2 SEX CHAR(l),
2 BIRTHDAY,
3 MONTH FIXED
3 DAY FIXED,
3 YEAR FIXED,
2 FATHER,
3 NAME CHAR(20),
3 AGE FIXED,
2 MOTHER
3 NAME CHAR(20),
3 AGE FIXED;
• variables SEX and YEAR are unique; hence references to them need not
be qualified.
• AGE is not unique. Accordingly, one should use
FATHER.AGE or MOTHER.AGE
depending on whether one wants to reference the father's age or the
mother's age.
• Assuming the record contains nonhomogeneous data, the record may
have to be stored in individual variables.
• Note that all data elements belonging to the same identifier do have
the same type. Such a file may be stored in memory as a collection of
parallel arrays; that is, where elements in the different arrays with the
same subscript belong to the same record.
NAME AGE SEX PHONE
Example 31
Suppose a membership
1 John Brown 28 Male 234-5186
list contains the name, age,
sex and telephone number 2 Paul Cohen 33 Male 456-7272

of each member. One can 3 Mary Davis 24 Female 777-1212


store the file in four parallel
arrays, NAME, AGE, SEX and 4 Linda Evans 27 Female 876-4478
PHONE; that is, for a given
subscript K, the elements 5 Mark Green 31 Male 255-7654

NAME[K], AGE[K], SEX[K] . . . . .


and PHONE[K] belong to . . . . .

the same record.


6.3.12 Matrices
• "Vectors" and "matrices" are mathematical terms which refer to linear and two-
dimensional arrays.
(a) An n-element vector V is a list of n numbers usually given in the form
V = (V1, V2, . . . . , Vn)
(b) An m  n matrix A is an array of m.n numbers arranged in m rows and n columns as
follows:

A11 A12 ... A1n

A= A21 A22 ... A2n


.

Am1 Am2 ... Amn


• In the context of vectors and matrices, the term scalar is
used for individual numbers.
• A matrix with one row (column) may be viewed as a vector
and, similarly, a vector may be viewed as a matrix with
only one row (column).
• A matrix with the same number n of rows and columns is
called a square matrix or an n-square matrix. The diagonal
or main diagonal of an n-square matrix A consists of the
elements A11, A22, . . . , Ann.
Algebra of Matrices
• A and B are mn matrices.
• The sum of A and B, written A+B, is the mn matrix
obtained by adding corresponding elements from A and B;
• product of a scalar k and the matrix A, written kA, is the
mn matrix obtained by multiplying each element of A by
k.
• U and V are n-element vectors. Then the scalar product of
U and V, written UV, is the scalar obtained by multiplying
the elements of U by the corresponding elements of V, and
then adding: n

UV= U1V1 + U2V2 + - - - + UnVn =  U kVk


k 1
• A is an mp and suppose B is a pn matrix.
• product of A and B, written AB, is the mn matris C
whose ijth element Cij is given by
p

Cij = Ai1 B1j + Ai1 B2j + - - - + Aip Bpj = A


k 1
ik Bkj

That is, Cij is equal to the scalar product of row i of A


and column j of B.
Example 33
(a) Suppose
1 -2 3 3 0 -6
A= and B =
0 4 5 2 -3 1
Then
1+3 -2+0 3+(-6) 4 -2 -3
A+B = =
0+2 4+(-3) 5+1 2 1 6

3·1 3·(-2) 3·3 3 -6 9


3A = =
3·0 3·4 3·5 0 12 15
(b) Suppose U = (1, -3, 4, 5), V = (2, -3, -6, 0) and
W = (3,-5, 2, -1). Then
UV = 12 + (-3)(-3) + 4(-6) + 50 = 2 + 9 - 24 + 0 = = 13
UW = 13 + (-3) (-5) + 42 + 5(-1) = 3 + 15 + 8 - 5 = 21
(c) Suppose

1 3 2 0 -4
A = and B =
2 4 3 2 6

The product matrix AB is defined and is a 2×3 matrix. The elements in the first
row of AB are obtained, respectively, by multiplying the first row of A by each
of the columns of B:
1
 3   2 0  4  1 2  33 1 0  3 2 1 (4)  3 6  11 6 14 
    
2  
4  3 2 6       
   
Similarly, the elements in the second row of AB are obtained, respectively, by multiplying the second row of A by each of the columns of B:

That is,

1
 3   2 0  4   11 6 14  11 6 14 
  
  
  
  
2
 4   3 2 6   2  2  4 3 2  0  4  2 2  (4)  4  6  16 8 16

11 6 14 
AB   
16 8 16 

Algorithm 9: (Matrix Multiplication) MATMUL(A, B, C, M, P, N)
Let A be an M × P matrix array, and let B be a P × N matrix array. This
algorithm stores the product of A and B in an M × N matrix array C.
1. Repeat Steps 2 to 4 for I = 1 to M:
2. Repeat Steps 3 and 4 for J = 1 to N:
3. Set C[I, J] := 0. [Initializes C[I, J]]
4. Repeat for K = 1 to P:
C[I, J] := C[I, J] + A[I, K] * B[K, J].
[End of inner loop.]
[End of Step 2 middle loop.]
[End of Step 1 outer loop.]
5. Exit.
6.4 Stacks, Queues and Recursion
6.4.1 Introduction
• A stack is a linear structure in which items may be
added or removed only at one end.
• three everyday examples of such a structure: a stack of
dishes, a stack of pennies and a stack of folded towels.
• the last item to be added to a stack is the first item to
be removed.
• also called last-in first-out (LIFO) lists.
• Other names are "piles" and "push-down lists".
• A queue is a linear list in which items may be added only at one end and items may be removed only at the other end.
• a queue of people waiting at a bus stop. Each new person who comes takes his or her place at the end of the line, and when the bus comes,
the people at the front of the line board first.
• the first person in the line is the first person to leave.
• are also called first-in first-out (FIFO) lists.
6.4.2 Stacks

• two basic operations associated with stacks:

(a) "Push" is the term used to insert an element into a stack.


(b) "Pop" is the term used to delete an element from a stack
Example 34.
1 AAA
TOP 2 BBB
3 CCC
4 DDD
FFF TOP 5 EEE
EEE 6 FFF
DDD 7
CCC 8
BBB 9
AAA
N-1
N
AAA BBB CCC DDD EEE FFF
1 2 3 4 5 6 7 8 9 ... N-1 N
TOP
• the right-most element is the top element
• EEE cannot be deleted before FFF is deleted, DDD cannot be deleted before EEE and FFF are deleted, and so on.
• Consequently, the elements may be popped from the stack only in the reverse order of that in which they were pushed
onto the stack.
6.4.3 Array Representation of Stacks

• a one-way list or a linear array


• each of our stacks will be maintained by a linear array STACK; a pointer variable TOP, which contains the location of
the top element of the stack; and a variable MAXSTK which gives the maximum number of elements.
• TOP = 0 or TOP = NULL will indicate that the stack is empty.
• Since TOP = 3, the stack has three elements, XXX, YYY and ZZZ;
and since MAXSTK = 8, there is room for 5 more items in the stack.

XXX YYY ZZZ


1 2 3 4 5 6 7 8
MAXSTR 8
TOP 3
Procedure 1: PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack.
1. [Stack already filled?]
If TOP = MAXSTK, then: Print: OVERFLOW, and Return.
2. Set TOP:= TOP + 1. [Increases TOP by 1.]
3. Set STACK[TOP]: =ITEM. [Inserts ITEM in new TOP position.]
4. Return.
Procedure 2: POP(STACK, TOP, ITEM)
This procedure deletes the top element of STACK and assigns it to the variable ITEM.
1. [Stack has an item to be removed?]
If TOP= 0, then: Print: UNDERFLOW, and Return.
2. Set ITEM:= STACK[TOP]. [Assigns TOP element to ITEM.]
3. Set TOP:= TOP - 1. [Decreases TOP by 1.]
4. Return.

Frequently, TOP and MAXSTK are global variables; hence the procedures may be called using only
PUSH(STACK, ITEM) and POP(STACK, ITEM)
Example 35.
(a) Consider the stack in the above figure. We simulate the operation PUSH(STACK, WWW):
1. Since TOP = 3, control is transferred to Step 2.
2. TOP=3+1=4.
3. STACK[TOP] = STACK[4] = WWW.
4. Return.
Note that WWW is now the top element in the stack.

(b) This time we simulate the operation POP(STACK, ITEM):


1. Since TOP = 3, control is transferred to Step 2.
2. ITEM = ZZZ.
3. TOP = 3-1 = 2.
4. Return.
Observe that STACK[TOP] = STACK[2] = YYY is now the top element in the stack.
6.4.4 Arithmetic Expressions; Polish Notation

• Let Q be an arithmetic expression involving constants and operation.


• finds the value of Q by using reverse Polish (postfix) notation.
• binary operations in Q may have different levels of precedence.
• the following three levels of precedence for the usual five binary operations.
Highest: Exponentiation (  )
Next highest: Multiplication (*) and division (/)
Lowest: Addition (+) and subtraction (-)
Example 36
Suppose we want to evaluate the following parenthesis-free arithmetic expression:
2  3 + 5 * 2  2 - 12 / 6
First we evaluate the exponentiations to obtain
8 + 5 * 4 - 12 / 6
Then we evaluate the multiplication and division to obtain 8 + 20 - 2.
Last, we evaluate the addition and subtraction to obtain the final result, 26.
Observe that the expression is traversed three times, each time corresponding to a level of precedence of the operations.
Polish Notation
• For most common arithmetic operations, the operator symbol is placed between its two operands. For example,
A+B C-D E*F G/H
called infix notation.
With this notation, we must distinguish between
(A+B)*C and A+(B*C)
• Polish notation, named after the Polish mathematician Jan Lukasiewicz,
• refers to the notation in which the operator symbol is placed before its two operands. For example,
+AB -CD *EF /GH
• We translate, step by step, the following infix expressions into Polish notation using brackets [] to indicate a partial translation:
(A + B) * C = [+AB] * C = *+ABC
A + (B * C) = A + [*BC] = +A*BC
(A + B)/(C - D) = [+AB]/[-CD] = /+AB-CD
• The fundamental property of Polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operands in the expression.
• Accordingly, one never needs parentheses when writing expressions in Polish notation.
• Reverse Polish notation refers to the analogous notation in which the operator symbol is placed after its two operands:
AB+ CD- EF* GH/
• never needs parentheses to determine the order of the operations in any arithmetic expression written in reverse Polish notation.
• frequently called postfix (or suffix) notation
• The computer usually evaluates an arithmetic expression written in infix notation in two steps. First, it converts the expression to postfix notation, and then it evaluates the postfix
expression.
• first we show how stacks are used to evaluate postfix expressions, and then we show how stacks are used to transform infix expressions into postfix expressions.
Algorithm 10: This algorithm finds the VALUE of an arithmetic expression P written in postfix notation.
1 . Add a right parenthesis ")" at the end of P. [This acts as a sentinel.]
2. Scan P from left to right and repeat Steps 3 and 4 for each element of P until the sentinel ")" is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator  is encountered, then:
(a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element.
(b) Evaluate B  A.
(c) Place the result of (b) back on STACK.
[End of If structure.]
[End of Step 2 loop.]
5. Set VALUE equal to the top element on STACK.
6. Exit.
Example 37
Consider the following arithmetic expression P written in postfix notation:
P: 5, 6, 2, +, *, 12, 4, /, -
(Commas are used to separate the elements of P so that 5, 6, 2 is not interpreted as the number 562.)

Symbol Scanned STACK


(1) 5 5 The equivalent infix
(2) 6 5, 6 expression Q follows:
(3) 2 5, 6, 2
(4) + 5, 8 Q: 5 * (6 + 2) -12 / 4
(5) * 40
Note that parentheses are
(6) 12 40,12
necessary for the infix
(7) 4 40, 12, 4
expression Q but not for
(8) / 40,3
the postfix expression P.
(9) - 37
(10) )
Transforming Infix Expressions into Postfix Expressions
• Let Q be an arithmetic expression written in infix notation.
• Besides operands and operators, Q may also contain left and right parentheses.
• We assume that the operators in Q consist only of exponentiations (), multiplications (*), divisions (/), additions (+) and subtractions (-)
• We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by
parentheses.
Algorithm 11: POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.
1. Push "(" onto STACK, and add ")" to the end of Q.
2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator  is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than .
(b) Add  to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
[End of If structure.]
[End of Step 2 loop.]
7. Exit.
• After Step 20 is executed, the STACK is empty and
P: A B C * D E F  / G * - H * +
which is the required postfix equivalent of Q.

Symbol Scanned STACK Expression P


(1) A ( A
(2) + (+ A
(3) ( (+( A
(4) B (+( AB
(5) * (+(* AB
(6) C (+(* ABC
(7) - (+(- ABC*
(8) ( (+(-( ABC*
(9) D (+(-( ABC*D
(10) / (+(-(/ ABC*D
Symbol Scanned STACK Expression P
(11) E (+(-(/ ABC*DE
(12)  (+(-(/ ABC*DE
(13) F (+(-(/ ABC*DEF/
(14) ) (+(- ABC*DEF/
(15) * (+(-* ABCDEF/G
(16) G (+(-* ABCDEF/G
(17) ) (+ ABCDEF/G*-
(18) * (+* ABCDEF/G*-
(19) H (+* ABCDEF/G*-H
(20) ) ABCDEFG*-H*+
6.4.5 Quicksort, An Application of Stacks
• Let A be a list of n data items- "Sorting A" refers to the operation of rearranging the elements of A.
• Quicksort is an algorithm of the divide-and-conquer type.
• That is, the problem of sorting a set is reduced to the problem of sorting two smaller sets, "reduction step"
• Suppose A is the following list of 12 numbers:

44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66

• finds the final position of one of the numbers; we use the first number, 44.
• Beginning with the last number, 66, scan the list from right to left, comparing each number with 44 and
stopping at the first number less than 44. The number is 22.
• Interchange 44 and 22 to obtain the list
22, 33, 11, 55, 77, 90, 40, 60, 99, 44, 88, 66

(Observe that the numbers 88 and 66 to the right of 44 are each greater than 44.)
• Beginning with 22, next scan the list in the opposite direction, from left to right, comparing each number with 44 and stopping
at the first number greater than 44. The number is 55.
• Interchange 44 and 55 to obtain the list

22, 33, 11, 44, 77, 90, 40, 60, 99, 55, 88, 66
(Observe that the numbers 22, 33 and 11 to the left of 44 are each less than 44.)
• Beginning this time with 55, now scan the list in the original direction, from right to left, until meeting the first number less
than 44. It is 40.
• Interchange 44 and 40 to obtain the list

22, 33, 11, 40, 77, 90, 44, 60, 99, 55, 88, 66
• (Again, the numbers to the right of 44 are each greater than 44.) Beginning with 40, scan the list from left to right. The
first number greater than 44 is 77.
• Interchange 44 and 77 to obtain the list

22, 33, 11, 40, 44, 90, 77, 60, 99, 55, 88, 66

• (Again, the numbers to the left of 44 are each less than 44.) Beginning with 77, scan the list from right to left seeking a
number less than 44. We do not meet such a number before meeting 44.
• This means all numbers have been scanned and compared with 44.
• Furthermore, all numbers less than 44 now form the sublist of numbers to the left of 44, and all numbers greater than 44
now form the sublist of numbers to the right of 44, as shown below:
22, 33, 11, 40, 44, 90, 77, 60, 99, 55, 88, 66
First sublist Second sublist
• Thus 44 is correctly placed in its final position
• The above reduction step is repeated with each sublist containing 2 or
more elements.
• by using two stacks, called LOWER and UPPER, to temporarily "hold" such
sublists.
• That is, the addresses of the first and last elements of each sublist, called
its boundary values, are pushed onto the stacks LOWER and UPPER
Example 39
Consider the above list A with n = 12 elements. The algorithm begins by pushing the boundary values 1 and 12 of A
onto the stacks to yield
LOWER: 1 UPPER: 12
In order to apply the reduction step, the algorithm first removes the top values 1 and 12 from the stacks, leaving
LOWER: (empty) UPPER: (empty)

The reduction step, as executed above, finally places the first element, 44, in A[5]. Accordingly, the algorithm pushes
the boundary values 1 and 4 of the first sublist and the boundary values 6 and 12 of the second sublist onto the stacks to yield
LOWER: 1, 6 UPPER: 4, 12
In order to apply the reduction step again, the algorithm removes the top values, 6 and 12, from the
stacks, leaving
LOWER: 1 UPPER: 4
and then applies the reduction step to the corresponding sublist A[6], A[7], . . . , A[12].
The reduction step changes this list as in the following figure.. Observe that the second sublist has
only one element. Accordingly, the algorithm pushes only the boundary values 6 and 10 of the first sublist
onto the stacks to yield
LOWER: 1, 6 UPPER: 4, 10
and so on.
The algorithm ends when the stacks do not contain any sublist to be processed by the reduction step.

A[6], A[7], A[8], A[9], A[10], A[11], A[12],

90, 77, 60, 99, 55, 88, 66

66, 77, 60, 99, 55, 88, 90

66, 77, 60, 90, 55, 88, 99

66, 77, 60, 88, 55, 90, 99

First sublist Second sublist


• The first part gives a procedure, called QUICK, which executes the above reduction step of the algorithm, and
• the second part uses QUICK to sort the entire list.
Procedure 3:QUICK(A, N, BEG, END, LOC)
Here A is an array with N elements. Parameters BEG and END contain the boundary values of the sublist of A to which this procedure applies. LOC keeps track of the position of the
first element A[BEG] of the sublist during the procedure. The local variables LEFT and RIGHT will contain the boundary values of the list of elements that have not been scanned.
1. [Initialize.] Set LEFT:=BEG, RIGHT:=END and LOC:=BEG.
2. [Scan from right to left.]
(a) Repeat while A[LOC] ≤ A[RIGHT] and LOC  RIGHT:
RIGHT:= RIGHT - 1.
[End of loop.]
(b) If LOC = RIGHT, then: Return.
(c) If A[LOC] > A[RIGHT], then:
(i) [Interchange A[LOC] and A[RIGHT].]
TEMP: = A[LOC], A[LOC]:=A[RIGHT],
A[RIGHT]: = TEMP.
(ii) Set LOC: =RIGHT.
(iii) Go to Step 3.
[End of If structure.]
3. [Scan from left to right.]
(a) Repeat while A[LEFT]≤A[LOC] and LEFTLOC:
LEFT:= LEFT + 1.
[End of loop.]
(b) If LOC = LEFT, then: Return.
(c) If A[LEFT] > A[LOC], then
(i) [Interchange A[LEFT] and A[LOC].]
TEMP:= A[LOC], A[LOC]:= A[LEFF],
A[LEFT]:= TEMP.
(ii) Set LOC: =LEFT.
(iii) Go to Step 2.
[End of If structure.]
Algorithm 12: (Quicksort) This algorithm sorts an array A with N elements.
1. [Initialize.] TOP:= NULL.
2. [Push boundary values of A onto stacks when A has 2 or more elements.]
If N > 1, then: TOP:= TOP + 1, LOWER[1]:= 1, UPPER[l]:= N.
3. Repeat Steps 4 to 7 while TOP NULL.
4. [Pop sublist from stacks.]
Set BEG:= LOWER[TOP], END:= UPPER[TOP], TOP:= TOP - 1.
5. Call QUICK(A, N, BEG, END, LOC). [Procedure 6.4.5.]
6. [Push left sublist onto stacks when it has 2 or more elements.]
If BEG < LOC - 1, then:
TOP:= TOP+ 1, LOWER[TOP]: = BEG,
UPPER[TOP] = LOC - 1.
[End of If structure.]
7. [Push right sublist onto stacks when it has 2 or more elements.]
If LOC + 1 < END, then:
TOP: = TOP + 1, LOWER[TOP]: = LOC + 1,
UPPER[TOP]: = END.
[End of If structure.]
[End of Step 3 loop.]
8. Exit.
Complexity of the Quicksort Algorithm
• The running time of a sorting algorithm is usually measured by the number f(n) of comparisons required to sort n
elements.
• The quicksort algorithmhas a worst-case running time of order n2/2, but an average-case running time of order n log n.
The reason for this is indicated below.
• The worst case occurs when the list is already sorted.
• Then the first element will require n comparisons to recognize that it remains in the first position.
• Furthermore, the first sublist will be empty, but the second sublist will have n-1 elements. Accordingly, the second
element will require n-1 comparisons to recognize that it remains in the second position. And so on. Consequently, there
will be a total of
f(n) = n + (n- 1) + - - - + 2 + 1=
comparisons. Observe that this is equal to the complexity of the bubble sort algorithm.

n(n 1)  n2  O(n)  O(n2 )


2 2
The complexity f(n) = O(n log n) of the average case comes from the fact that, on the average, each reduction step of
the algorithm produces two sublists. Accordingly:
(1) Reducing the initial list places 1 element and produces two sublists.
(2) Reducing the two sublists places 2 elements and produces four sublists.
(3) Reducing the four sublists places 4 elements and produces eight sublists.
(4) Reducing the eight sublists places 8 elements and produces sixteen sublists.
And so on.
Observe that the reduction step in the kth level finds the location of 2k-1 elements; hence there will be approximately
log2 n levels of reductions steps. Furthermore, each level uses at most n comparisons, so f(n) = O(n log n). In fact,
mathematical analysis and empirical evidence have both shown that
f(n)  1.4[n log n]
is the expected number of comparisons for the quicksort algorithm.
6.4.6 Recursion
• Recursion is an important concept in computer science.
• Many algorithms can be best described in terms of recursion.
• Suppose P is a procedure containing either a Call statement to itself or a Call statement to a second
procedure that may eventually result in a Call statement back to the original procedure P. Then P is
called a recursive procedure.
• So that the program will not continue to run indefinitely, a recursive procedure must have the
following two properties:
(1) There must be certain criteria, called base criteria, for which the procedure does not call itself.
(2) Each time the procedure does call itself (directly or indirectly), it must be closer to the base
criteria.
• A recursive procedure with these two properties is said to be well-defined.
• Similarly, a function is said to be recursively defined if the function definition refers to itself.
• Again, in order for the definition not to be circular, it must have the following two properties:
(1) There must be certain arguments, called base values, for which the function does not
refer to itself.
(2) Each time the function does refer to itself, the argument of the function must be closer to
a base value.
• A recursive function with these two properties is also said to be well-defined.
Example 40
Let us calculate 4! using the recursive definition. This calculation requires the following nine steps:
(1) 4! = 4  3!
(2) 3! = 3  2!
(3) 2! = 2  1!
(4) 1! = 1  0!
(5) 0! = 1
(6) 1! = 1  1 =1
(7) 2! = 2 1 = 2
(8) 3! = 3  2 = 6
(9) 4! = 4  6 = 24
That is:
Step 1. This defines 4! in terms of 3!, so we must postpone evaluating 4! until we evaluate 3!
This postponement is indicated by indenting the next step.
Step 2. Here 3! is defined in terms of 2!, so we must postpone evaluating 3! until we evaluate
2!
Step 3. This defines 2! in terms of 1!
Step 4. This defines 1! in terms of 0!
Step 5. This step can explicitly evaluate 0!, since 0 is the base value of the recursive definition.
Steps 6 to 9. We backtrack, using 0! to find 1!, using 1! to find 2!, using 2! to find 3!, and finally using 3! to
find 4! This backtracking is indicated by the "reverse" indention.
Procedure 4A: FACTORIAL(FACT, N)
This procedure calculates N! and returns the value in the variable FACT.
1. If N = 0, then: Set FACT:= 1, and Return.
2. Set FACT:= 1. [Initializes FACT for loop.]
3. Repeat for K = 1 to N.
Set FACT: = K * FACT.
[End of loop.]
4. Return.
Procedure 4B: FACTORIAL(FACT, N)
This procedure calculates N! and returns the value in the variable FACT.
1. If N = 0, then: Set FACT:= 1, and Return
2. Call FACTORIAL(FACT, N - 1).
3. Set FACT: = N * FACT.
4. Return.

• Observe that the first procedure evaluates N! using an iterative loop process.
• The second procedure, on the other hand, is a recursive procedure, since it contains a call to itself.
• Suppose P is a recursive procedure.
• During the running of an algorithm or a program which contains P, we associate a level number with
each given execution of procedure P as follows.
• The original execution of procedure P is assigned level 1; and each time procedure P is executed
because of a recursive call, its level is 1 more than the level of the execution that has made the recursive
call.
• In Example 6.4.8, Step 1 belongs to level 1. Hence Step 2 belongs to level 2, Step 3 to level 3, Step 4 to
level 4 and Step 5 to level 5. On the other hand, Step 6 belongs to level 4, since it is the result of a return
from level 5.
• In other words, Step 6 and Step 4 belong to the same level of execution. Similarly, Step 7 belongs to
level 3, Step 8 to level 2, and the final step, Step 9, to the original level 1.
Fibonacci Sequence

The celebrated Fibonacci sequence (usually denoted by F0, F1, F2, . . .) is as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .
That is, F0 = 0 and F1 = 1 and each succeeding term is the sum of the two
preceding terms. For example, the next two terms of the sequence are
34 + 55 = 89 and 55 + 89 = 144
Definition (Fibonacci Sequence)

(a) If n = 0 or n = 1, then Fn = n.
(b) If n > 1, then Fn = Fn-2 + Fn-1
This is another example of a recursive definition, since the definition refers to itself
when it uses Fn-2 and Fn-1. Here (a) the base values are 0 and 1, and (b) the value of
Fn. is defined in terms of smaller values of n which are closer to the base values.
Accordingly, this function is well-defined.
Procedure 5: FIBONACCI(FIB, N)
This procedure calculates FN and returns the value in the first parameter FIB.
1. If N = 0 or N = 1, then: Set FIB:= N, and Return.
2. Call FIBONACCI(FIBA, N - 2).
3. Call FIBONACCI(FIBB, N - 1).
4. Set FIB:= FIBA + FIBB.
5. Return.

This is another example of a recursive procedure, since the procedure contains a call to itself. In fact, this
procedure contains two calls to itself. We note that one can also write an iterative procedure to calculate Fn. which
does not use recursion.
6.4.7 Towers of Hanoi
• Suppose three pegs, labeled A, B and C, are given, and suppose on peg A
there are placed a finite number n of disks with decreasing size.
• The object of the game is to move the disks from peg A to peg C using peg B
as an auxiliary.
• The rules of the game are as follows:
(a) Only one disk may be moved at a time. Specifically, only the top disk on
any peg may be moved to any other peg.
(b) At no time can a larger disk be placed on a smaller disk.
Initial setup of Towers of Hanoi with n=6

• Sometimes we will write X  Y to denote the instruction "Move top disk from peg X to peg Y," where X and Y may be any of the three pegs.
• The solution to the Towers of Hanoi problem for n=3 appears in the following figure. Observe that it consists of the following seven moves:
n = 3: Move top disk from peg A to peg C.
Move top disk from peg A to peg B.
Move top disk from peg C to peg B.
Move top disk from peg A to peg C.
Move top disk, from peg B to peg A.
Move top disk from peg B to peg C.
Move top disk from peg A to peg C.

A B C A B C A B C A B C

(a) Initial (1) A → C (2) A → B (3) C → B


A B C A B C A B C A B C

(4) A → C (5) B → A (6) B → C (7) A → C


In other words,
n = 3: A  C, A B, C B, A C, B A, B  C, AC
For completeness, we also give the solution to the Towers of Hanoi problem for n = 1 and n = 2:
n = 1: AC
n = 2: A  B, A  C, B  C
Note that n = 1 uses only one move and that n = 2 uses three moves.

Rather than finding a separate solution for each n , we use the technique of recursion to develop a general solution. First we observe that the solution to the Towers of Hanoi problem forn > 1 disks may be reduced to the following subproblems:
(1) Move the top n - 1 disks from peg B
(2) Move the top disk from peg A to peg C. A  C.
(3) Move the top n - 1 disks from peg B to peg C.
This reduction is illustrated in the following figure for n = 6. That is, first we move the top five disks from peg A
to peg B, then we move the large disk from peg A to peg C, and then we move the top five disks from peg B to peg C.

A B C A B C

(a) Initial n=6 (b) Move top five disks from peg A to
peg B
A B C A B C

(c) Move top disk from peg A to peg C (d) Move top five disks from peg B to peg C
Let us now introduce the general notation
TOWER(N, BEG, AUX, END)

to denote a procedure which moves the top n disks from the initial peg BEG to the final peg END using the peg AUX as an auxiliary. When n = 1, we have the following obvious solution:
TOWER(1, BEG, AUX, END) consists of the single instruction BEG  END
Furthermore, as discussed above, when n>1, the solution may be reduced to the solution of the following three subproblems:
(1) TOWER(N-1, BEG, END, AUX)

(2) TOWER(L, BEG, AUX, END) or BEG  END

(3) TOWER(N - 1, AUX, BEG, END)


The following figure contains a schematic diagram of the above recursive solution for
TOWER(4, A, B, C)
Observe that the recursive solution for n = 4 disks consists of the following 15 moves:
AB AC BC AB CA CB AB
AC BC BA CA BC AB AC BC

In general, this recursive solution requires f(n ) = 2n - 1 moves for n disks.


Procedure 6: TOWER(N, BEG, AUX, END)
This procedure gives a recursive solution to the Towers of Hanoi problem for N disks.
1. If N = 1, then:
(a) Write: BEG  END.
(b) Return.
[End of If structure.]
2. [Move N-1 disks from peg BEG to peg AUX.]
Call TOWER(N-1, BEG, END, AUX).
3. Write: BEG  END.
4. [Move N-1 disks from peg AUX to peg END.]
Call TOWER(N-1, AUX, BEG, END).
5. Return.
6.4.8 Queues
• A queue is a linear list of elements in which deletions can take place only at one end, called the front, and insertions can take place only at the other end, called the rear.
• "front" and "rear" are used in describing a linear list only when it is implemented as a queue.
• Queues are also called first-in first-out (FIFO) lists, since the first element in a queue will be the first element out of the queue.
• In other words, the order in which elements enter a queue is the order in which they leave. This contrasts with stacks, which are last-in first-out (LIFO) lists.
Example 41
• The following figure
(a) is a schematic diagram of a queue with 4 elements, where AAA is the front element and DDD is the rear element.Suppose an element is deleted from the queue. Then it must be AAA.
(b) where BBB is now the front element. Next, suppose EEE is added to the queue and then FFF is added to the queue. Then they must be added at the rear of the queue.
(c) Note that FFF is now the rear element. Now suppose another element is deleted from the queue; then it must be BBB.
(d) Observe that in such a data structure, EEE will be deleted before FFF because it has been placed in the queue before FFF. However, EEE will have to wait until CCC and DDD are deleted.
AAA BBB CCC DDD (a)

BBB CCC DDD (b)

BBB CCC DDD EEE FFF (c)

CCC DDD EEE FFF (d)


Representation of Queues
• The following figure will be stored in memory using an array QUEUE with N elements.
• Observe that whenever an element is deleted from the queue, the value of FRONT is increased by 1; this can be implemented by the assignment

FRONT: = FRONT + 1

• Similarly, whenever an element is added to the queue, the value of REAR is increased by 1; this can be implemented by the assignment

REAR:= REAR + 1
• Suppose we want to insert an element ITEM into a queue at the time the queue does occupy the last part of the array, i.e., when REAR = N.
• One way to do this is to simply move the entire queue to the beginning of the array, changing FRONT and REAR accordingly, and then inserting ITEM as above.
• This procedure may be very expensive. The procedure we adopt is to assume that the array QUEUE is circular, that is, that QUEUE[1] comes after QUEUE[N] in the array. With this assumption, we insert ITEM into the queue by assigning ITEM to QUEUE[l]. Specifically, instead of increasing REAR to N+1, we reset REAR=1 and then assign

QUEUE[REAR]: = ITEM
• Similarly, if FRONT=N and an element of QUEUE is deleted, we reset FRONT=1 instead of increasing FRONT to N+1.

QUEUE

FRONT: 1 AAA BBB CCC DDD .....


REAR: 4
1 2 3 4 5 6 7 ..... N
(a)

FRONT: 2 BBB CCC DDD .....


REAR: 4
1 2 3 4 5 6 7 ..... N
(b)
FRONT:2 CCC DDD EEE FFF .....
REAR: 6
1 2 3 4 5 6 7 ..... N
(c)

FRONT: 3 CCC DDD EEE FFF .....


REAR: 6
1 2 3 4 5 6 7 ..... N
(d)
• Suppose that our queue contains only one element, i.e.,
suppose that
FRONT = REAR  NULL
• and suppose that the element is deleted. Then we assign
FRONT:=NULL and REAR:=NULL
to indicate that the queue is empty.
Example 42
• The following figure shows how a queue may be maintained by a circular array QUEUE with N = 5 memory locations.
• Observe that the queue always occupies consecutive locations except when it occupies locations at the beginning and at the end of the array.
• If the queue is viewed as a circular array, this means that it still occupies consecutive locations.

(a) Initial empty FRONT: 0


REAR: 0
1 2 3 4 5
(a)

(b) A, B and then C inserted: FRONT: 1


A B C
REAR: 3
1 2 3 4 5
(b)
(c) A deleted: FRONT: 2
B C
REAR: 3
1 2 3 4 5

(c)
(d) D and then E inserted: FRONT: 2
B C D E
REAR: 5
1 2 3 4 5
(d)
(e) B and C deleted: FRONT: 4 D E
REAR: 5
1 2 3 4 5
(e)
(f) F is inserted: FRONT: 4
F D E
REAR: 1
1 2 3 4 5

(f)
(g) D deleted: FRONT: 5
F E
REAR: 1
1 2 3 4 5
(g)
(h) G and then H inserted: FRONT: 5 F G H E
REAR: 3
1 2 3 4 5
(h)
(i) E deleted: FRONT: 1 F G H
REAR: 3
1 2 3 4 5
(i)
(j) F deleted: FRONT: 2
G H
REAR: 3
1 2 3 4 5

(j)
(k) K inserted: FRONT: 2
G H K
REAR: 4
1 2 3 4 5
(k)
(l) G and H deleted: FRONT: 4
K
REAR: 4
1 2 3 4 5
(l)
(m) K deleted, QUEUE empty: FRONT: 0
REAR: 0
1 2 3 4 5
(m)
Procedure 7: QINSERT(QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1 . [Queue already filled?]
If FRONT = 1 and REAR = N, or if FRONT = REAR+1, then:
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If FRONT:= NULL, then: [Queue initially empty.]
Set FRONT := 1 and REAR := 1.
Else if REAR= N, then:
Set REAR := 1.
Else:
Set REAR := REAR + 1.
[End of If structure.]
3. Set QUEUE[REAR] := ITEM. [This inserts new element.]
4. Return.
Procedure 8: QDELETE(QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the variable ITEM.
1. [Queue already empty?]
If FRONT : = NULL, then: Write: UNDERFLOW, and Return.
2. Set ITEM := QUEUE[FRONT]
3. [Find new value of FRONT.]
If FRONT= REAR, then: [ Queue has only one element to st art.]
Set FRONT := NULL. and REAR := NULL.
Else if FRONT = N, then:
Set FRONT:= 1.
Else:
Set FRONT:= FRONT+ 1.
[End of If structure.]
4. Return.
6.4.9 DEQUES
• A deque (pronounced either "deck" or "dequeue") is a linear list in which elements can be added or removed at either end but not in the middle.
• double-ended queue.
• circular array DEQUE with pointers LEFT and RIGHT, which point to the two ends of the deque.
• assume that the elements extend from the left end to the right end in the array.
• "circular" comes from the fact that we assume that DEQUE[1] comes after DEQUE[N] in the array. The following figure pictures two deques, each with 4 elements maintained in an array with N=8 memory locations. The condition LEFT = NULL will be used to indicate that a deque is empty.
• There are two variations of a deque
 input-restricted deque
 output-restricted deque
• which are intermediate between a deque and a queue.
• input-restricted deque is a deque which allows insertions at only one end of the list but allows deletions at both ends of the list
• an output-restricted deque is a deque which allows deletions at only one end of the list but allows insertions at both ends of the list.
QUEUE

LEFT: 4 AAA BBB CCC DDD


RIGHT: 7 1 2 3 4 5 6 7 8

(a)

QUEUE
LEFT: 7
YYY ZZZ WWW XXX
RIGHT: 2
1 2 3 4 5 6 7 8

(b)
6.4.10 Priority Queues
• A priority queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:
(1) An element of higher priority is processed before any element of lower priority.
(2) Two elements with the same priority are processed according to the order in which they were added to the queue.
• A prototype of a priority queue is a timesharing system: programs of high priority are processed first, and programs with the same priority form a standard queue.
• There are various ways of maintaining a priority queue in memory.
• We discuss two of them here: one uses a one-way list, and the other uses multiple queues.
• The ease or difficulty in adding elements to or deleting them from a priority queue clearly depends on the representation that one chooses.
One-Way List Representation of a Priority Queue
• One way to maintain a priority queue in memory is by means of a one-way list, as follows:
(a) Each node in the list will contain three items of information: an information field INFO, a priority number PRN and a link number LINK.
(b) A node X precedes a node Y in the list (1) when X has higher priority than Y or (2) when both have the same priority but X was added to the list before Y. This means that the order in the one-way list corresponds to the order of the priority queue.
• Priority numbers will operate in the usual way: the lower the priority number, the higher the priority.
Example 43
• figure (a) shows a schematic diagram of a priority queue with 7 elements. The diagram does not tell us whether BBB was added to the list before or after DDD. On the other hand, the
diagram does tell us that BBB was inserted before CCC, because BBB and CCC have the same priority number and BBB appears before CCC in the list.
• figure (b) shows the way the priority queue may appear in memory using linear arrays INFO, PRN and LINK.

AAA 1 BBB 2 CCC 2 DDD 4


   

EEE 4  FFF 4  GGG 5 

(a)
INFO PRN LINK
1 BBB 2 6
START 5
2 7
3 DDD 4 4
AVAIL 2
4 EEE 4 9
5 AAA 1 1
6 CCC 2 3
7 10
8 GGG 5 0
9 FFF 4 8
10 11
11 12
12 0

(b)
Algorithm 13: This algorithm deletes and processes the first element in a priority queue which appears in memory as a one-way list.
1. Set ITEM:= INFO[START]. [This saves the data in the first node.]
2. Delete first node from the list.
3. Process ITEM.
4. Exit.

Adding an element to our priority queue is much more complicated than deleting an element from the queue, because we need to find the correct place to insert the element.
Algorithm 14: This algorithm adds an ITEM, with priority number N to a priority queue which is maintained in memory as a one-way list.
(a) Traverse the one-way list until finding a node X whose priority number exceeds N. Insert ITEM in front of node X.
(b) If no such node is found, insert ITEM as the last element of the list.
The above insertion algorithm may be pictured as a weighted object "sinking" through layers of elements until it meets an element with a heavier weight.
Example 44
• Consider the priority queue in figure (a).
• Suppose an item XXX with priority number 2 is to be inserted into the queue.
• We traverse the list, comparing priority numbers.
• Observe that DDD is the first element in the list whose priority number exceeds that of XXX.
• Hence XXX is inserted in the list in front of DDD.
• Observe that XXX comes after BBB and CCC, which have the same priority as XXX.
• Suppose now that an element is to be deleted from the queue.
• It will be AAA, the first element in the list. Assuming no other insertions, the next element to be deleted will be BBB, then CCC, then XXX, and so on.
START 
XXX 2

AAA 1  BBB 2  CCC 2  DDD 4 

EEE 4  FFF 4  GGG 5 


Array Representation of a Priority Queue
• Another way to maintain a priority queue in memory is to use a separate queue for each level of priority (or for each priority number).
• Each such queue will appear in its own circular array and must have its own pair of pointers, FRONT and REAR.
• In fact, if each queue is allocated the same amount of space, a two-dimensional array QUEUE can be used instead of the linear arrays.
• Observe that FRONT[K] and REAR[K] contain, respectively, the front and rear elements of row K of QUEUE, the row that maintains the queue of elements with priority number K.
FRONT RARE 1 2 3 4 5 6
1 1 2 2 1 AAA
2 2 1 3 2 BBB CCC XXX
3 3 0 0 3
4 4 5 1
4 FFF DDD EEE
5 GGG
5 5 4 4
Algorithm 15: This algorithm deletes and processes the first element in a priority queue maintained by a two-dimensional array QUEUE.
1. [Find the first nonempty queue.]
Find the smallest K such that FRONT[K]  NULL.
2. Delete and process the front element in row K of QUEUE.
3. Exit.
Algorithm 16: This algorithm adds an ITEM with priority number M to a priority queue maintained by a two-dimensional array QUEUE.
1. Insert ITEM as the rear element in row M of QUEUE.
2. Exit.

You might also like