Uploaded by lin su

线性搜索和冒泡排序

advertisement
Records
Records are composite data types formed by the inclusion of several
related items that may be of different data types. This allows a
programmer to refer to these items using the same identifier, enabling a
structured approach to using related items. A record will contain a fixed
number of items. For example, a record for a book could include title,
author, publisher, number of pages, and whether it is fiction or non-fiction.
A record data type is one example of a composite user-defined data type.
A composite data type references other existing data types when it is
defined. A composite data type must be defined before it can be used. Any
data type not provided by a programming language must be defined before
it can be used.
In pseudocode, a record data type definition takes the following form:
For example, the book record data type could be defined like this:
The data type, TbookRecord, is now available for use and an identifier
may now be declared in the usual way:
Items from the record are now available for use and are identified by:
For example:
1D arrays
2D arrays
linear search
item
The integer to be found
myList
Array to be searched
upperBound
Upper bound of the array
lowerBound
Lower bound of the array
index
Pointer to current array element
found
Flag to show when item has been found
bubble sort
myList
Array to be searched
upperBound
Upper bound of the array
lowerBound
Lower bound of the array
index
Pointer to current array element
swap
Flag to show when swaps have been made
top
Index of last element to compare
temp
Temporary storage location during swap
Abstract data types (ADTs)
An abstract data type (ADT) is a collection of data and a set of
operations on that data.
• Stack – a list containing several items operating on the last in, first out
(LIFO) principle. Items can be added to the stack (push) and removed
from the stack (pop). The first item added to a stack is the last item to be
removed from the stack.
• Queue – a list containing several items operating on the first in, first out
(FIFO) principle. Items can be added to the queue (enqueue) and
removed from the queue (dequeue). The first item added to a queue is
the first item to be removed from the queue.
• Linked list – a list containing several items in which each item in the
list points to the next item in the list. In a linked list a new item is
always added to the start of the list.
Stack operations
Queue operations
Circular queue operation
Linked list operations
Library routines
Many programming language development systems include library
routines that are ready to incorporate into a program. These routines are
fully tested and ready for use. A programming language IDE usually
includes a standard library of functions and procedures as well as an
interpreter and/or a compiler. These standard library routines perform tasks
such as input/output that are required by most programs.
Procedures
When writing an algorithm, there are often similar tasks to perform that
make use of the same groups of statements. Instead of repeating these
statements every time they are required, many programming languages
make use of subroutines or named procedures. A procedure is defined once
and can be called many times within a program.
A procedure can be defined in pseudocode, as follows:
There are two methods of passing a parameter to a procedure: by value
and by reference. When a parameter is passed by value, if a variable is
used, the value of that variable cannot be changed within the procedure.
When a parameter is passed by reference the value of the variable passed
as the parameter can be changed by the procedure.
A procedure with parameters passed by reference can be defined inpseudocode as follows:
Functions
When writing an algorithm, there are often similar calculations or tasks to perform that make use of the
same groups of statements and always produce an answer. Instead of repeating these statements every
time they are required, many programming languages make use of subroutines or named functions.
A function always returns a value; it is defined once andcan be called many times within a program.
Functions can be used on theright-hand side of an expression.
When procedures and functions are defined, the first statement in the
definition is a header, which contains
• the name of the procedure or function
• any parameters passed to the procedure or function
• the type of the return value for a function.
When procedures or functions are called, the parameters or arguments
(the values passed to the procedure or function) must be in the same order
as the parameters in the declaration header and each argument must be of
the same type as the parameter given in the header. Procedure calls are
single stand-alone statements and function calls form part of an expression
on the right-hand side.
Download