DATA STRACTURE REVIEWE
Data Structure Specification of a set of data and set
of operations performed in a data
Week 2
storage for data defined in terms of
Specialized format to store and set of operations to be perform on
organize data in a computer’s the data
memory or disk collection of
variables, possibly of several different
data types connected in various way
Types of Data Structures
Array Algorithms
Linked list Finite set of instructions that specify
a sequence of operations to be carry
Stacks/Queue out recipe for solving a problem
Trees Example of simple algorithm
Hash tables ü Apply to wet hair
ü Massage gently
Data Types ü Leave on for a few moments
Data that a variable can hold in a ü Rinse Off
programming language all
programming language has a set of Note:
built-in data types All the tasks that can be carry out by
Examples: a computer can be state as
algorithms
char - data type representing
character values Criteria for Algorithm
int - data type representing integer Every algorithm must satisfy the
values following criteria:
Abstract Data Types 1. Input - zero or more quantities are
externally suppl
2. Output - at least one quantity is Computer Science also refers to the
produce study of data. It include:
3. Definiteness - each instruction Machines that holds data
must be clear and unambiguous
Languages for describing data
4. Finitness - all instructions of an manipulatio
algorithm will terminate after a finite
Foundations, which describe what
number of steps
kinds of refined
5. Effectiveness - each operation
Data can be produce from raw dat
must be definite, but must also be
feasible Structures for representing data
Notes for Criteria for Algorithm: PSEUDOCODE
Inputs - are the data items presented Textual presentation of a flowchart
to the algorithm. An algorithm has close to a natural language the
either no input or a predetermined control structures impose the logic
number of them may become part of the program
documentation could be translated
Output - are the data items
into a program
presented to the outside world as the
result of the execution of a program STEPWISE REFINEMENT
based on the algorithm. An algorithm The process by which a programmer
ought to produce at least one output. refines an initial idea to a problem's
Procedure solution into more specific terms. The
last phase of refinement results in a
Essential tool in programming that
program ready to be coded for
generalizes the notion of an operator,
execution.
Procedure used to encapsulate parts
of an algorithm by localizing in one Analysis of Algorithms
section of a program all the Determining the amount of resources
statements relevant to a certain necessary to execute it such as time
aspect of a program. and storage,
Raw data is an input to a computer
and an algorithm used to transform
this into a refined data
Best-case analysis
Usually in terms of CPU time and Worst-case analysis
memory requirement
Average case analysis
Analysis of Algorithm
WEEK 2
Introduction to Array Ø Every data inserted is placed in the first
vacant cell
Search
LIST - ordered set of a variable number of
elements to which additions and deletions Ø Another process that the algorithm
may be made one of the simplest and most carries out he red arrow from the figure
commonly found type of data states where the search starts
LINEAR LIST - list which displays the
Ø Moves methodically downwards to
relationship of physical adjacency
search for the item
finite sequence of simple data, items, or
records Delete
Arrays - ordered collection of data items of Ø The algorithm must first locate the item
the same type referred to collectively by a to delete it
single name
Ø The deleted item causes hole in the array
Index – each variable or cell in an array
Elements – individual data / items in an
array indicated by the array name followed
Hole one or more cells that have filled
by its dimensions appears in a square cells above them
brackets
Dimensions – an integer from 1 –n called
dimensioned variables Basics of Arrays in C++
Insert
Ø Arrays are treated as primitive type, they
Ø Adds item to the indicated place/cell The are treated as objects
process is very fast because it only includes
one step
Ø use the new operator to declare an array
Note: Matrix- is a mathematical object which
arises in many physical problems and
consists of mrows and n columns
1. intArray name is a reference to an array
Algorithm to create a magic square by H.
and not the array itself
Coxeter
2. The length field of an array can be used
ü Begin with 1 inthe middle of the top row
to find the size in bytes:
ü Move up and left assigning numbers in
3. Array elements can be accessed
increasing order to empty squares
through the use of square brackets.
ü If you fall off the square imagine the
4. the first element is numbered 0 so that
same square as tilting the plane and
the indices in an array of 10 elements run
continue
from 0 to 9
ü If a square is occupied, move down
5. initialize first an array before
instead and continue
attempting to access it to avoid syntax error
Transpose–another operation that can be
6. can initiate an array of primitive type to
performed on matrices that computes for
something besides 0 using:
the transpose of matrix
Index and Length
ü the elements in the [i,j] position are
Indices–used to access each location of transferred to the [j,i] position
arrays from 0 ranging up to the instantiated
Array
ü elements in the diagonal will always
Changing the Size of an Array remain in the same position since i= j
- Analyze the syntax below and give
its output:
Linear Search
Two Dimensional Array
ü checks every element of a list one at a
ü It is an array of an array time in sequence
ü Also called as tablesor matrices ü also called as sequential search
Row–first dimension
Column–second dimension Algorithm for linear search
1. Create an ordered array
2. Initialize a target value or the key
3. If index == key, return true
4. If index < key, return false 3. Compare the middle item to the target
value or the key
Binary Search
4. If not found read, search parameters,
ü Usually coded as
half the size and start over
mid = (high + low) /2;
5. If found, return
ü Algorithm for binary search
1. Array elements should be sorted
2. Find the middle
WEEK 3
Searching two steps to execute over and over until
data are sorted
v great algorithms have been devised for
searching because it is so important § compare the two items
v information retrieval is one of the most § swap the two items or copy one
common operations performed by
§ item but still; the details are
computer applications
§ handled in different ways
v search key uniquely identifies the data
being requested by the user Bubble sort simplest in sorting process
Compare two elements in the array If the
Key field contains the key of the record
element on the left is larger than the
Information field contains the information element in the right, swap them If vice
associated with the key in the key field versa, no move will be done Compare now
the element that was swapped to the
Sorting
element on its right position.
invariants a condition that is always true at
How to sort? a certain point in a program
v as humans, we can do this by lining up the Selection sort is done by selecting an
items element in the list and moving it to the
v computers not like humans can visually proper position
compare things two items at once It uses
1. find the minimum value in the list
2. swap it with the value in the first 1. Divide the list into two: sorted part and
position the unsorted part
3. repeat the steps for the remainder of
the list starting the next position after the
2. Unsorted part: Transfer one by one to
swapping to the first position.
their correct location in the sorted area
Insertion Sort
To perform insertion sort:
WEEK 4
Recursion technique in programming that While statement
calls itself has the capability to save the
ü Can also be written using:
condition it was in or the particular process
it was serving when calling itself Assignment statement
The design of a recursive method consists iIfelse statement
of the following elements:
recursion
Ø One or more stopping condition that
Triangular Numbers the sum of 1 to n
can be directly evaluated for certain
numbers arranged in equilateral triangle
arguments.
seen by row where each row is longer than
Ø One or more recursive step, sin which a
the
current value of the method can be
computed by repeated calling of the previous row the sum of consecutive
method with arguments that will eventually integers.
arrive at a stopping condition
Finding the Nth term using Recursion
Two classes of recursion
Ø The first (tallest) column, which has the
Ø Direct Recursion value of n.
Ø Indirect Recursion Ø The sum of all the remaining columns.
Recursion is viewed as a somewhat mystical Recursive algorithm
technique which is only useful for some
1. There must be at least one case
very special class of problems
(the base case), for a small value of n that
ü Can be written using: can be solved directly
Assignment statement 2. A problem of a given size n can be
split into one or more smaller versions of
Ifelse statement
the same problem
3. Identify the base case and provide
a solution to it
4. Create a strategy to split the
problem into smaller versions of itself while
making progress toward the base case
5. Merge the solution that will solve the
larger problem
Factorial It is the product of a series of
consecutive positive integers from 1 to a
given number, n Expressed by the symbol !
Algorithm to anagram a word
1. Anagram the rightmost n1 letters
2. Rotate all n letters
3. Repeat these steps n time
Merge - sort requires another array in the
memory.
Types of Recursion
* Linear Recursion
* Tail Recursion
* Binary Recursion
* Exponential Recursio
* Nested Recursion
* Mutual Recursion
Depth of Recursion number of times the
procedure is called recursively in the
process of evaluating a given argument.
operand operator operand
v Postfix notation
operand operand operator
Exercise
WEEK 5 convert the following infix notation to
postfix notation
Stack - an ordered list where all operations
are restricted at one end of the list known 1. (A*B+C)/DE*F
as the top.
2. P/O+DS*
Stack Operation
3. J*Q+SV/BN
Ø PUSH –adds item at the top
4. H(A+K)+E*D
Ø POP–removes element from the to
5. L/(FR)+(O*R(W/D))
Ø PEEK –accesses value at the to
Postfix Notation The algorithm for Postfix
Note: performs the following operations on
expressions written in infix notation:
LIFO or LastInFirstOut the last object
inserted in the list will be the first one to be ü Detects errors in the syntax of the infix
Retrieved. expression.
Function Call It is an example of stack that ü Transforms the infix expression into
arises in computer programming postfix notation
Reversing - a Word it uses stack to Algorithm
temporarily store an element extracting
1. If recognize an operand, push it on
characters onebyone from the input string
the stack.
and pushed onto the stack popped off the
stack and displayed stack reverses the order 2. If recognize an operator, pop its
of the characters because of its last in first operands, apply the operator and push the
out characteristics. value on the stack.
Evaluation of Expressions 3. Upon conclusion, the value of the
postfix expression is on the top of the stack.
An expression is made up of operands and
operators. Exercise
Evaluation of Expression
v Infix notation
Convert the following postfix notation to 6. Copies the calling parameters into the
infix notation Parameter region.
1. AB*C+DEF*/ 7. Initializes local variables in the local
variable region.
2. PO/D+SR*
Eliminating Recursion
3. JQ*S+VB/N
When a method return
4. HAK+ED
1. Get the program counter from the
5. LFR/OR*WD/
activation record and replace what's in the
Eliminating Recursio Program Counter
Most compilers implement recursion using 2. Get the base pointer value from the
stacks. Activation Record and replace what's in the
Base Pointer.
Stack is used to hold activation records for
each method and so, for every invocation of 3. Pop the Activation Record entirely
a method during recursion allows an from the stack.
activation of record to be pushed on the
system stack.
Eliminating Recursion
Call and Return Process
When a method/function is called
1. An activation record is created; its size
depends on the number and Size of the
local variables and parameters.
2. The Base Pointer value is saved in the
special location reserved for it.
3. The Program Counter value is saved in
the Return Address location.
4. The Base Pointer is now reset to the
new base (top of the call stack prior to the
creation of the Activation Record).
5. The Program Counter is set to the
location of the first bytecode of the method
being called.