Algorithms and Data Structures
Algorithms and Data Structures
Correctness
Efficiency
Implementation Goals
Robustness
Adaptability
Reusability
Data Representation
For example in sorting, if elements are already sorted for a specific algorithm.
The best case running time rarely occurs in practice comparatively with the first and second
case.
Data
Data Abstraction – the separation between specification of a data object and its
implementation
Data Type – a collection of objects and set of operations that act on those objects
Primitive
Composite
In computer science, a data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently.
Data may be organized in many different ways, the logical or mathematical model of a particular
organization of data in memory or on disk is called Data Structure.
The data appearing in our data structure is processed by means of certain operations. In fact,
the particular data structure that one chooses for a given situation depends largely on the
frequency with which specific operations are performed. The following four operations play a
major role:
Traversing
Accessing each record exactly once so that certain items in the record may be
processed.(This accessing or processing is sometimes called 'visiting" the records.)
Searching
Finding the location of the record with a given key value, or finding the locations of all
records, which satisfy one or more conditions.
Inserting
Deleting
What is an Algorithm?
An algorithm is a finite set of instructions that takes some raw data as input and transforms it in
to refined data.
Many solutions
Recursive Algorithms
A recursive algorithm calls itself which usually passes the return value as a parameter to the
algorithm again. This parameter is the input while the return value is the output.
Complexity of Algorithm
Efficiency or complexity of an algorithm is stated as a function relating the length to the number
of steps (time complexity) or storage location (space complexity). f (n)
Performance Analysis
Are the functions created in such way that they perform logical subfunctions?
Performance Analysis
Time complexity: the amount of computer time needed by a program to run to comple
tion
fixed part: independent of the characteristics (e.g. number, size) of the inputs and outp
uts
variable part: dependent on the particular problem instance being solved, hence on the
inputs and outputs characteristics
Time Complexity
program is typed, compiled and run on a specific machine. Execution time is physically c
locked,
TP(n) is measured...
But, the value measured is inaccurate (multiuser systems, system load, number of runni
ng programs, ...)
Asymptotic Notation
Suppose we are considering two algorithms, A and B, for solving a given problem. Furthermore,
let us say that we have done a careful analysis of the running times of each of the algorithms
and determined them to be Ta(n) and Tb(n),respectively, where n is a measure of the problem
size. Then it should be a fairly simple matter to compare the two functions and to determine
which algorithm is the best!
Types of Analysis
The behavior of the algorithm with respect to the worst possible case of the input instance.
The worst-case running time of an algorithm is an upper bound on the running time for any
input. Knowing it gives us a guarantee that the item does not occur in data.
The expected behavior when the input is randomly drawn from a given distribution.
The average-case running time of an algorithm is an estimate of the running time for an
"average" input.
Computation of average-case running time entails "knowing all possible input sequences, the
probability distribution of occurrence of these sequences, and the running times for the
individual sequences”.
Often it is assumed that all inputs of a given size are equally likely.
For example in sorting, if elements are already sorted for a specific algorithm.
The best case running time rarely occurs in practice comparatively with the first and second
case.
Time-Space Tradeoff
So if your problem is taking a long time but not much memory, a space-time tradeoff would let
you use more memory and solve the problem more quickly.
Or, if it could be solved very quickly but requires more memory than, you can try to spend more
time solving the problem in the limited memory.
Arrays
Accessing Array
Declaring Array
-Compiler reserves necessary storage space for the array on the basis of declaration
-Retrieving
-Adding
-Deleting
-Inserting
Retrieving
Adding
Inserting
o -It adds new element in a specified position, without replacing any existing element
o -The position is specified in terms of index value or position relative to an existing
element.
o -Ex. We want to insert element z before x[index] The elements
x[index],x[index+1],x[index+2]….. x[n-1] are moved to the right position
Array Traversing
Advantages of an Array
An array uses a single variable to represent a large set of homogenous data collection.
An array provides direct access to a storage address for an element. Therefore, retrieval of an
element is extremely fast.
The elements on an array can be manipulated easily using an index. Thus, processing of array is
flexible.
Disadvantages of an Array
For very large data sets, the program may run out of the storage space