Data Structures LMS
Data Structures LMS
Specialized format to store and organize data in a computer’s memory or disk collection
of variables, possibly of several different data types connected in various ways
Array
Linked list
Stacks/Queues
Trees
Hash tables
Data Types
Data that a variable can hold in a programming language all programming language has
a set of built-in data types
Examples:
Specification of a set of data and set of operations performed in a data storage for data
defined in terms of set of operations to be perform on the data
Algorithms
Finite set of instructions that specify a sequence of operations to be carry out recipe for
solving a problem
ü Massage gently
ü Rinse Off
Note:
All the tasks that can be carry out by a computer can be state as algorithms
4. Finitness - all instructions of an algorithm will terminate after a finite number of steps
Output - are the data items presented to the outside world as the result of the execution
of a program based on the algorithm. An algorithm ought to produce at least one output.
Procedure
Raw data is an input to a computer and an algorithm used to transform this into a
refined data.
PSEUDOCODE
STEPWISE REFINEMENT
The process by which a programmer refines an initial idea to a problem's solution into
more specific terms. The last phase of refinement results in a program ready to be
coded for execution.
Analysis of Algorithms
Determining the amount of resources necessary to execute it such as time and storage,
Analysis of Algorithms
Best-case analysis
Worst-case analysis
Introduction to Array
LIST - ordered set of a variable number of elements to which additions and deletions
may be made one of the simplest and most commonly found type of data
Example
Sunday)
Values in a Deck of (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack,
Arrays - ordered collection of data items of the same type referred to collectively by a
single name
Elements – individual data / items in an array indicated by the array name followed by
its dimensions appears in a square brackets
arrayName [0][0]
Operations in Arrays
Insert
Ø Adds item to the indicated place/cell The process is very fast because it only
includes one step
Search
Ø Another process that the algorithm carries out he red arrow from the figure states
where the search starts
Delete
Hole one or more cells that have filled cells above them
Basics of Arrays in C++
refer to it
Note:
2. The length field of an array can be used to find the size in bytes:
4. the first element is numbered 0 so that the indices in an array of 10 elements run
from 0 to 9
Dimensionality of an Array
§ Onedimensional Array
Array1[j]
§ Twodimensional Array
Array1[i ][j ]
§ Multidimensional Array
Array[i][j][k]…
type[] variableName;
An array of ints
int[] intArray;
Random[] randomArray;
and
int size = 5;
Array
ArrayIndexOutOfBoundsException
Example
int size = 5;
index++)
{ intArray[index] = index; }
index ++)
Example (continued..)
index ++)
{ intArray[index] = index + 1; }
tempArray.length; index++)
{ tempArray[index] =
intArray[index]; }
intArray = tempArray;
ü It is an array of an array
Row–first dimension
Column–second dimension
Arr1[2][3]
rowIndex ++)
{tableArray[rowIndex][colIndex] =
rowIndex * colIndex + 1; }
}}
arrays
{6,7,8,9,0} };
illegalint[][]
Matrix- is a mathematical object which arises in many physical problems and consists
of mrows and n columns
mx n (read as m by n) is written to
columns
ü If you fall off the square imagine the same square as tilting the plane and continue
Transpose–another operation that can be performed on matrices that computes for the
transpose of matrix
ü the elements in the [i,j] position are transferred to the [j,i] position
ü elements in the diagonal will always remain in the same position since i= j
Linear Search
5.
Binary Search
ü Usually coded as
4. If not found read, search parameters, half the size and start over
5. If found, return
Searching
v search key uniquely identifies the data being requested by the user
Information field contains the information associated with the key in the key field
How to sort?
v computers not like humans can visually compare things two items at once It uses two
steps to execute over and over until data are sorted
Bubble sort simplest in sorting process Compare two elements in the array If the
element on the left is larger than the element in the right, swap them If vice versa, no
move will be done Compare now the element that was swapped to the element on its
right position
Selection sort is done by selecting an element in the list and moving it to the
proper position
3. repeat the steps for the remainder of the list starting the next position after the
swapping to the first position
Insertion Sort
1. Divide the list into two: sorted part and the unsorted part
2. Unsorted part: Transfer one by one to their correct location in the sorted area
Recursion technique in programming that calls itself has the capability to save the
condition it was in or the particular process it was serving when calling itself
Ø One or more stopping condition that can be directly evaluated for certain
arguments.
Ø One or more recursive step, sin which a current value of the method can be
computed by repeated calling of the method with arguments that will eventually arrive at
a stopping condition
Ø Direct Recursion
Ø Indirect Recursion
Recursion is viewed as a somewhat mystical technique which is only useful for some
very special class of problems
Assignment statement
Ifelse statement
While statement
Assignment statement
iIfelse statement
recursion
Triangular Numbers
int triangle(int n)
int total = 0;
is 1
return total;
int triangle(int n)
return (n +
sumRemainingColumns );
term n:
int triangle(int n)
return(n + sumAllColumns(n1));
//Instead
int triangle
return(n + triangle(n1));
Sample Output
Ø Calls itself
Ø Some version of the problem that is simple where the routine can solve it without
calling itself
Recursive algorithm
1. There must be at least one case (the base case), for a small value of n that
can be solved directly
2. A problem of a given size n can be split into one or more smaller versions of
the same problem
4. Create a strategy to split the problem into smaller versions of itself while
making progress toward the base case
Anagrams forming of real English word using the same letters, no more, no less, the
process is called anagramming
int i;
charArray[i 1] = charArray[i];
charArray[i 1] = temp;
Towers of Hanoi invented by Edouard Lucas in 1883 its objective is to transfer the
entire tower to the last peg, moving only one disk at a time without moving a larger one
onto a smaller
if(topN==1)
else
}
Merge sort requires another array in the memory
Linear Recursion
Tail Recursion
Binary Recursion
Exponential Recursion
Nested Recursion
Mutual Recursion
Depth of Recursion number of times the procedure is called recursively in the process
of evaluating a given argument
Stack an ordered list where all operations are restricted at one end of the list known as
the top
Stack Operations
Note:
LIFO or LastInFirstOut the last object inserted in the list will be the first one to be
Retrieved
Stack Representation
v One-dimensional array
v Doubly-Linked List
Evaluation of Expressions
A/B*C+D*E
Evaluation of Expressions
v Infix notation
v Postfix notation
Note:
infix expression may be directly translated into postfix form by beginning with the
conversion of the subexpression with the highest precedence
1. ((A/B)/C)*(D+E) = AB/
AB/C/
AB/C/DE+*
2. (A+B)/(CA)+D*E = AB+
AB+CA/
AB+CA/
DE*+
Exercise
1. (A*B+C)/DE*F
2. P/O+DS*R
3. J*Q+SV/BN
4. H(A+K)+E*D
5. L/(FR)+(O*R(W/D))
Postfix Notation The algorithm for Postfix performs the following operations on
expressions written in infix notation:
Algorithm
2. If recognize an operator, pop its operands, apply the operator and push the value
on the stack.
3. Upon conclusion, the value of the postfix expression is on the top of the stack.
Postfix Notation
ABCD*+/E*F–
Exercise
1. AB*C+DEF*/
2. PO/D+SR*
3. JQ*S+VB/N
4. HAK+ED*
5. LFR/OR*WD/+
Eliminating Recursion
Stack is used to hold activation records for each method and so, for every invocation of
a method during recursion allows an activation of record to be pushed on the system
stack
Eliminating Recursion
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.
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.
Eliminating Recursion
2. Get the base pointer value from the Activation Record and replace what's in the
Base Pointer.