Sie sind auf Seite 1von 12

BIT 121 – DATA STRUCTURES and ALGORITHMS

University of Nueva Caceres


College of Computer Studies
Instructor: Alexis L. Fante

COURSE OUTLINE

I. Introduction to Data Structure


A. Data and Decisions
B. Data Management
C. Classifications of Data Structure

II. Linear Data Structure


A. Array
B. Algorithms of Array Operations and Implementation
i. Initialization
ii. Insertion and Deletion
iii. Accessing a component
iv. Searching
v. Sorting
C. Multi-dimensional Arrays
D. Matrix Operations
E. Link Lists
F. Link List Algorithms
G. Link List Operations
i. Node
ii. Initialization
iii. Search
iv. Insertion & Deletion of Node
H. Stacks
I. Stack Implementation
J. Infix-Postfix Operation and Manipulation
K. Evaluating Infix-Postfix Expressions
L. Stack Applications
M. Queues
N. Implementation of Queues (Q): Array & Link List
O. Q Applications
III. Trees
IV. Graphs
V. Analysis of Algorithm
VI. Mathematical Tools for Analysis

1
BIT 121 – DATA STRUCTURES and ALGORITHMS
University of Nueva Caceres
College of Computer Studies
Instructor: Alexis L. Fante

I. Introduction to Data Structure


A. Data Structure, Data, Information, and Decisions

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

A variable is a memory location declared to serve as buffer for data of a


specific data type. Common characteristic of a variable is that it can only
be used to store a single value at a time. 1 These variables are also known
as atomic variable, usually referred to as scalar variable, that variable
whose value cannot be further subdivided or separated into a built-in data
type.

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

A data structure is a specialized format for organizing and storing


data. General data structure types include the array, the file, the
record, the table, the tree, and so on. Any data structure is
designed to organize data to suit a specific purpose so that it can
be accessed and worked with in appropriate ways. In computer
programming, a data structure may be selected or designed to
store data for the purpose of working on it with various algorithms.2

An organization of information, usually in memory, for better


algorithm efficiency, such as queue, stack, linked list, heap,
dictionary, and tree, or conceptual unity, such as the name and

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 data structure is a grouping of related data items in memory.

DATA & DECISIONS

CLASSIFICATIONS OF DATA STRUCTURES


B. Data Management
C. Classifications of Data Structure

II. Linear Data Structure

A. Array
Definition:

ARRAY is a data structure consisting of components of the same type. It is


a series of memory locations that can store value of the same type.4

It should be declared to contain data of a specific data type. If it is


declared as integer, it can only contain integer data; if used as an array of
characters it can only contain character data type.

Its components (memory locations) can be accessed by directly referring


to the index of a specific location of its content or component. The content
can be referred to as an element of that array.

Furthermore, array is used to store and process a set of values of the


same data type forming a logical group.

X = {1, 2, 3, 4,5} y = {‘a’, ’b’, ’c’}

Array Element Array Subscript


It is a data item that is part of an array. A value or expression enclosed
in brackets after the
array name, specifying which array element
to access.

B. One-Dimensional Array

One-dimensional array is known both as single-dimensional and a single-


subscript array. A list of values of the same data type that is stored using
3
http://www.itl.nist.gov/div897/sqg/dads/HTML/datastructur.html

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.

Although it may seem unusual to force the first element of an array to


have an index of 0, doing so actually increases the speed of accessing
individual elements. The reason is that a starting index value of 0 permits
the compiler to directly equate all subsequent index values, without
further computation, to an internal offset that is used by the compiler in
accessing individual elements.5

It works in a manner that the subscript or index of the array when


referencing an element tells the compiler the number of memory cells or
elements it should skip.

C. Algorithms of Array Operations and Implementation

Creation and Initialization

To declare or create an array in C,

SYNTAX:

Element-type array_name[size]; /* un-initialized */


Element-type array_name[size] = {initialization list}; /*
initialized */

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].

Initializing an array can be done in two ways. It can be initialized


statically or during the program run-time. It is advisable to initialize array
for the reason that during declaration, the memory address allocated
might have contained a garbage value which is being previously used by
the operating system or other applications running previously or
simultaneously with the C program.

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:

int my_int_array[5] ={0, 0, 0, 0, 0}; char my_char_array[5] =


{‘ ‘,’ ‘,’ ‘,’ ‘,’ ‘};
int my_int_array[5]= {1, 2, 3, 4, 5}; char my_char_array[5] =
{‘a’,’b’,’c’,’d’,’e’};

Character arrays or strings can be initialized in three ways:

1. char codes[6] = {‘s’,’a’,’m’,’p’,’l’,’e’};


2. char codes[ ] = {‘s’,’a’,’m’,’p’,’l’,’e’};
3. char codes[ ] = ”sample” or char codes[ ] = “ “;

For nos. 1 & 2 both are initialized with six array locations for an array
named codes.

The no. 3 initialization creates or assigns the string “sample” to array


codes. This however assigns a seven character array where the last
cell contains the escape sequence \0, known as the NULL character.
The NULL character is automatically appended to all strings by C
compiler.

s a m p l e \0
codes codes codes [ codes [ codes [ codes codes
[0 ] [1 ] 2] 3] 4] [5 ] [6]

ARRAY AS FUNCTION ARGUMENTS

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.

int grades[5] = { 98, 75, 65, 80, 88};

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.

This is similar with looking for a term or meaning of a word in a


dictionary, looking for a name in a list of students, or searching for a
topic in a book index. We usually begin by searching on the beginning
of the dictionary or to the nearest possible reference until we find the
term which meaning we are looking for.

Although linear search is not the most efficient way of searching, it has
its own advantage and disadvantages. To highlight its advantage:

1. The algorithm is simple


2. The list need not be sorted in a particular order.

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

Set a Found flag to .F.


Set an index value to -1
Begin with the first item in the list
While there are still items in the list AND the found flag is .F.
Compare the item with the desired item
If the item is found
Set the Found flag to .T.
Set the index value to the item’s position in the
list
Endif
EndWhile
Return the Index Value

Aside from the advantages enumerated, another advantage of linear


search is if the element is located toward the front part of the list. The
searching will be done in a shorter period of time. The setback
however is if the search token is located at the back of the list.

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

Given the above scenario in a sequential search, if the element of the


list is too many, we can assume that the searching therefore may take
longer. The solution to make the searching faster or significantly
reduced is by using a Binary Search Algorithm.
The requirements for a binary search, however, includes that the list
must be sorted or ordered. In an ordered list, the search token will be
first compared to the element in the middle of the list. This posed a
question for a list with an even number of elements: how to get the
middle? Either of the two middle elements may be used for
comparison.

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.

If the first scenario is true, then no further search is required. In the


second scenario, since the search token is greater than the middle
element, the lower part of the list – from the first element to the
midpoint element may be discarded for searching. In the third
scenario, the upper part of the list, from the midpoint to the last
element in the list is discarded for further search since the search
token is less than the mid-element.

Here is the pseudo-code for the Binary Search

Set an index value to -1


Set a Found Flag to .F.
Set the lower index to 0
Set the upper index to one less than the size of the list (N – 1)
Begin with the first item in the list
While the lower index is less than or equal to the upper index and
upper index and match is not yet found
Set the midpoint index to the integer average of the lower and
upper index values
Compare the search token with the mid-element
If the search token is equal to the mid-element
Search token is found
Else If the search token is greater than the mid-element
7
P 419, Ibid.

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

There are two major categories of sorting techniques: internal and


external.

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.

We will be focusing more on Internal Sort. This type of sorting method


is also known as in-place algorithms that store a constant amount of
array elements outside the array. This means that the size of the array
has been fixed already.

The methods that will be used are comparison-based sorting


algorithms.

The sort algorithms that we will be discussing are the following:


1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Merge-Sort

Selection Sort

Being the simplest sorting technique, the smallest value is initially


selected from the complete list of data and exchanged with the first
element in the list.

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.

For a list of consisting N element, the process is repeated N – 1 times,


in each pass through the list it requires one less comparison than the
previous pass.

The pseudo-code for selection sort is:

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.

For example given the prefix expression P consisting of operators and


operands we can evaluate the expression with a stack: 9

i. If the next item in P is an operator, push it on top of the stack. Set


the argument count to be the number of operands expected by
the operator;
ii. If the next item in P is an operand, push it on top of the stack;
iii. If the top n entries in the stack are operand entries needed for
the top operator, apply the top operator to these operands.
Replace the operator and its n operands by the result of the
operation.

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

While this is fairly simple we have to check if we have enough operands to


satisfy the current top operator
.

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

Infix Prefix Postfix


(A + B)/D /+ABD AB+D/
(A + B)/(D + E) /+AB+DE AB+DE+/
(A - B/C + E)/(A+B) /+-A/BCE+AB ABC/-E+AB+/
2
B - 4*A*C -^B2**4AC B2^4A*C*-

L. Evaluating Infix-Postfix Expressions


M. Stack Applications
N. Queues
O. Implementation of Queues (Q): Array & Link List
P. Q Applications
III. Trees
IV. Graphs
V. Analysis of Algorithm
Mathematical Tools for Analysis

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

Das könnte Ihnen auch gefallen