A class consists of data and functions that operate
on that data.
A class in C++ has two parts – public and private
(let’s ignore the protected members for now).
The data and functions defined in the class are
called the members of the class.
17.
ADTs and C++ Classes
Users of the class can only access and
manipulate the class state through the public
members of the class.
Private members can only be used by other
members of the class (let’s also ignore the friends for now).
Data encapsulation is achieved by declaring
all data members of a class to be private.
The interface of the class is provided through
the use of public member functions.
18.
Data Structures
The primary objective of programming is to efficiently
process the input to generate the desired output.
We can achieve this objective in an efficient and neat
style if the input data is organized in a way to help us
meet our goal.
Data Structures is nothing but ways and means of
organizing data so that it can be processed easily and
efficiently.
Data structures dictate the manner in which the data
can be processed. In other words, the choice of an
algorithm depends upon the underlying data
organization. ( What is an Algorithm ? )
19.
What is an Algorithm ?
An algorithm is a well defined list of steps for solving a particular problem
An algorithm manipulates the data in data structures in various ways, such as inserting a new element, searching for a particular item etc.
An algorithm must satisfy the following criteria
1) Input 2) output 3) Definiteness ( each instruction should be clear and unambiguous) 4) Fitness (terminates after finite number of steps) 5) Effectiveness (each instruction must be feasible enough)
20.
Data Structure Operations
Traversing
Searching
Inserting
Deleting
Sorting
Merging
Recursion
To perform operations on various data structures we use algorithms.
21.
Types of Data Structures
Premitive/Scalar : data types that can be manipulated as a single quantity or can be represented alone
Structured/ Non-Premitive (Data type which is collection of other premitive or non-premitive data structures.
Can be further divided into
a) linear b) non-linear
- Linear can be further split into
a) physically linear b) logically linear
22.
problem: Determine if a key is present in a collection of 10 integers
Organization 1: Data are stored in 10 disjoint ( as opposed to
composite ) variables: A0, A2, A3,……,A9
Algorithm
found=false;
if (key = = A0 ) found = true;
else if (key = = A1 ) found = true;
else if (key = = A2 ) found = true;
else if (key = = A3 ) found = true;
else if (key = = A4 ) found = true;
else if (key = = A5 ) found = true;
else if (key = = A6 ) found = true;
else if (key = = A7 ) found = true;
else if (key = = A8 ) found = true;
else if (key = = A9) found = true;
23.
problem: Determine if a key is present in a collection of 10 integers
Organization 2: Data are stored in an array of 10 elements
Algorithm
found=false;
for (int i =0; i < 10; i ++)
if ( A[i] == key)
{
found = true;
break;
}
Average number of comparisons in both cases is the same so
both algorithms are equally efficient (or in efficient)
Organization 2 is better because it yields an algorithms which is
more maintainable. For example, if the collection contains 100 elements. In general, number of elements could be N.
24.
problem: Determine if a key is present in a collection of 10 integers
Organization 3: Data are stored in an array A in ascending order
. The space complexity of a program is the measure
of the amount of memory that it needs to run to
completion.
Time Complexity:
. The time complexity of a program is the measure of
the amount of computer time it needs to run to
completion.
28.
Performance Estimation
How to determine which algorithm is
better?
We need some mechanism to predict
the performance without actually
executing the program.
Mechanism should be independent of
the compiler and underlying hardware.
29.
Step Count
Program Step: A program step is a
meaningful program segment.
We can consider each statement as a
single step.
a = 2;
a = 2 * b + c + 3 * c / d – e;
30.
Step Count
To count total number of steps, we must
determine:
1. Step count for each statement –
steps/execution or s/e
2. Frequency of each statement
3. Total steps for each statement
4. Finally sum these counts to get the total step
count
31.
Example 1 – Summing of a list of numbers (Step count Table) 2n+3 Total 0 0 0 } 1 1 1 return temp; N n 1 temp+=list[i] ; n + 1 n + 1 1 for (i=0;i<n; i++) 0 0 0 int i ; 1 1 1 float temp=0; 0 0 0 { 0 0 0 Float sum (float list[],int n) Total Steps Frequency S/e Statement
32.
Problems
Determining the exact step count of a program can be a
Very difficult task
- inexactness of the definition of a step, exact step count is not very
useful for - comparative purposes.e.g. which one is better 45n+3 or 100n+10
- Frequency of execution
. How many steps are executed?
if (condition)
{
step1;step2step3;step4;step5;
}
else
step6;
We need some asymptotic notation as a measure of growth
33.
Big Oh (O)
Big Oh is defined as:
F(n)=O(g(n)) iff there exists positive constants c and n 0
such that f(n)<= cg(n) for all values of n > = n 0
No matter what the value of c 1 , c 2 ,c 3 there will be an n
beyond which the program with complexity c 3 n will be faster than the one with complexity c 1 n2+c 2 n
Example: 1) 3n +3 = O(n) as 3n +3 <=4n for all n >=3