Example Pseudocode
Problem: Given a sorted array a with n elements (i.e., a[0] <= a[1] <= … a[n-1]) and a number m, find if m is in the array.
1. Main pseudo code
data
given data
n: the number of integers given
a[0], …, a[n-1]: the given integers
m: given integer (to check if it is in a)
unknown data: N.A.
intermediate data:
found: indicating if m is found from a
plan
// get array a, n from user input (numbers in a must be ordered).
n = getseries(a)
// find if m is in array a from index 0 to n-1
found = search(a, 0 , n-1, m)
if found print m is found in a.
Otherwise print m is not found in a.
(Pseudo code for all functions used in the main pseudocode)
2. Pseudo code for search function
Function name: search
input:
a: an array of numbers
bottom, top: bottom and top index
m: the number to search from a[bottom] to a[top]
output:
b: 1 if m is in a a[bottom] to a[top], 0 otherwise
Data
mid: middle index of the array
plan:
if (bottom > top) b = 0 and stop.
find the mid point mid of the array between bottom and top
if (a[mid] == m) b = 1
else if (m > a[mid])
P2.1 // find if m is in a from mid+1 to top:
b = search(a, mid+1, top, m)
else P2.2 // find if m is in a from bottom to mid-1
b = search(a, bottom, mid-1,m)
3. Pseudo code for getSeries function
omitted here
Programming Language Pragmatics
FOURTH EDITION
Michael L. Scott
Department of Computer Science
University of Rochester
Morgan Kaufmann is an imprint of Elsevier
225 Wyman Street,Waltham, MA 02451, USA
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Cataloging-in-Publication Data
A catalog record for this book is available from the Library of Congress
For information on all MK publications
visit our website at http://store.elsevier.com/
ISBN: 978-0-12-410409-9
Copyright © 2016, 2009, 2006, 1999 Elsevier Inc.
About the Author
Michael L. Scott is a professor and past chair of the Department of Computer Sci-
ence at the University of Rochester. He received his Ph.D. in computer sciences in
1985 from the University of Wisconsin–Madison. From 2014–2015 he was a Vis-
iting Scientist at Google. His research interests lie at the intersection of program-
ming languages, operating systems, and high-level computer architecture, with an
emphasis on parallel and distributed computing. His MCS mutual exclusion lock,
co-designed with John Mellor-Crummey, is used in a variety of commercial and
academic systems. Several other algorithms, co-designed with Maged Michael,
Bill Scherer, and Doug Lea, appear in the java.util.concurrent standard li-
brary. In 2006 he and Dr. Mellor-Crummey shared the ACM SIGACT/SIGOPS
Edsger W. Dijkstra Prize in Distributed Computing.
Dr. Scott is a Fellow of the Association for Computing Machinery, a Fellow of
the Institute of Electrical and Electronics Engineers, and a member of Usenix, the
U.