Beruflich Dokumente
Kultur Dokumente
COURSE OUTLINE
1
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
Definition:
DATA
Plural of Latin datum
An item of information
INFORMATION
The meaning of data as it is intended to be interpreted by people.
Collection of data
To use the data stored in the variable can be done by referencing or using
the memory location of the declared variable.
Another method of storing and retrieving data is through the use of data
structure. Data Structure is a type of an aggregate data type, a data type
with two main characteristics:
1. values can be decomposed into individual elements, each of which is
either atomic or another data structure;
2. it provides an access scheme for ,locating individual data elements
within the data structure.
DATA STRUCTURE
1
P. 376, Bronson G., A First Book of ANSI C, 2007.
2
http://datastructures.itgo.com/appendix.htm
2
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
address of a person. It may include redundant information, such as
length of the list or number of nodes in a subtree.3
A. Array
Definition:
B. One-Dimensional Array
4
P. 1, Albacea, Intro. to Data Structures & Algorithms, 2002
3
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
a single group name. It is simply one implementation of a list in which all
the elements are stored consecutively in a set of contiguous memory
locations.
In C, the starting index value for arrays is always zero. The starting index
value is fixed by the compiler and it cannot be altered.
SYNTAX:
int my_int_array[5];
int my_int_array[5] = {11,13,24,12,67};
where int is the data type of the elements to be stored in the array
named my_int_array, and 5 is the number of components that can be
stored within the array. The first element of the array is my_int_array[0]
and the last element is my_int_array[4].
5
P. 377, Bronson G., A First Book of ANSI C, 2007.
4
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
In this manner, initialization will secure that the memory to be used
will either be initialized to contain a specific value assigned by the
programmer.
Static Initialization:
For nos. 1 & 2 both are initialized with six array locations for an array
named codes.
s a m p l e \0
codes codes codes [ codes [ codes [ codes codes
[0 ] [1 ] 2] 3] 4] [5 ] [6]
Like any other data, array elements or the array itself can be used as
function arguments, either in a user defined function (UDF) or system
defined function.
Findlow(grades[1],grades[5]);
Findmax(grades);
Array Subscripts
SYNTAX: array_name[subscript]
Example: my_array[5];
my_array[ i + 3]; /* where i is an int data type */
Searching
5
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
The types of search algorithms that we will use are Linear Search and
Binary Search.
A. Linear Search
Linear Search is also known as Sequential Search.
In a Linear Search, every item in the list is being compared with the
search token, the value that is to be search, until the search token is
found or unfound.
Although linear search is not the most efficient way of searching, it has
its own advantage and disadvantages. To highlight its advantage:
In linear search, the search begins at the first item and continues
sequentially, element by element through the list. The pseudocode 6
for a function performing a linear search is
6
p 416-417, Bronson, A First Book of ANSI C, 2007
6
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
On an average and assuming that the search token is equally likely to
be anywhere within the list, the number of required of comparisons are
N/2, where N is the number of items in the list. For a 10-element list,
the average number of comparisons needed for a linear search is 5,
and for 10,000-element list the average number of comparisons is
5000.7
B. Binary Search
There are three possible scenarios that may occur once a comparison
of elements is done:
1. the search token may be equal to the middle element;
2. it may be greater than the middle element; or,
3. it may be less than the middle element.
7
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
Set the lower index value to the mid-element index plus
1
Else If the search token is less than the mid-element
Set the upper index to the mid-element index less 1
Endif
EndWhile
Return the index value
Sorting
Sorting is a process of rearranging a set of values (that is in no
particular order in the array) to produce an ordered version of the
values in the same array.8
Internal Sorting is used when the data list is not too large and
manageable to be stored within the computer memory.
External Sorting is used for larger sets of data that are usually stored
in secondary memories. These data sets are too large that cannot be
sorted as a whole in the computer memory.
Selection Sort
After the first selection and exchange, the next smallest element in the
revised list is selected and exchanged with the second element in the
8
P. 8, Albacea, E. Intro. to Data Structures & Algorithms, 2007.
8
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
list, so the second pass will consider only the second element through
the last element in the list.
Set interchange count to zero (not required; done just to keep track of
the interchanges)
For each element in the list from the next-to-last
Find the smallest element from the current element being
referenced to the last element by:
Setting the minimum value equal to the current element
Saving (storing) the index of the current element
For each element in the list from the current element +
1 to the last element in the list
If element [inner loop index] < minimum value
Set the minimum value = element [inner
loop index]
Save the index of the new found minimum
value
Endif
EndFor
Swap the current value with the new minimum value
Increment the interchange count
EndFor
Return the interchange count
D. Multi-dimensional Arrays
E. Matrix Operations
F. Link Lists
G. Link List Algorithms
H. Link List Operations
ii. Node
iii. Initialization
iv. Search
v. Insertion & Deletion of Node
I. Stacks
J. Stack Implementation
K. Infix-Postfix-Prefix Operation and Manipulation
During our studies we learn mathematics via infix notation. Our math
instructors teach us to write operation symbol written between the
operands. Such as '-' operator between operands A and B. We solve the
expression A - B by subtracting B from A.
9
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
In some fields of computer science and programming languages, it makes
more sense to order expressions with different notations called prefix and
postfix. Each of these three notations, prefix, postfix and infix have
different useful attributes. They differ in how one computes the value of
an expression.
Infix Notation
Infix notation is most suitable for binary operations. In infix notation, the
operation symbol is written between the two operands. Because the infix
notation for the basic arithmetic and logical operations have been
commonly used in ordinary mathematics, this notation has been widely
adopted in programming languages. Infix notation use in programming
languages may lead to unique problems. For example, when more than
one infix operator appears in an expression, the notation is ambiguous
unless parentheses are used. Consider 2 * 3 + 4. One understands that
the value should be 10, but it could also be 14. We learned from
mathematics that multiplication is performed before addition. To clear this
ambiguity, parentheses may be used to explicitly indicate the grouping of
operators and operands, which may lead to deep nests of parentheses.
Therefore, languages use implicit control rules that are called hierarchy of
operations (precedence) and associativity.
Prefix Notation
Function calls are usually written with name preceding its arguments, foo
(x, y). When this is extended to operations we call it the prefix notation. In
prefix notation, the operator, precedes (comes before) the operands
hence it is called prefix notation. It actually makes sense to know the
operation prior to receiving the arguments so that one can expect the
proper number of operands. There is no ambiguity, and no parentheses
are need to specify how to evaluate the expression. Prefix notation is
sometimes called Polish notation because of the inventor Polish
mathematician Lukasiewicz. The prefix notation is easy to decode
mechanically. So, it is simple to translate code into sequences.
9
http://triton.towson.edu/~akayabas/COSC455_Spring2000/Prefix.html
10
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
Postfix Notation
With postfix notation, you place the operator after the operands. When an
operator is scanned its operands are already evaluated. Therefore the
stack evaluation is as follows:
i. If the next item of P is an operand, place it on the stack.
ii. If the next item of P is an operator, then its n arguments
must be the top n items on the stack. Replace these n
items by the result of applying this operation using the n
items as arguments.
So, postfix evaluations are easier to implement using a stack. Often the
syntax of expressions is converted to postfix during code translation
Examples
11
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante
http://datastructures.itgo.com/appendix.htm
12