Unit 1
Unit 1
Algorithms
1
Elementary Data Organization
• Data are simply values or sets of values.
• A data item refers to a single unit of values .
• Data items that are divided into subitems are called
group items, those are not are called elementary
items.
• Collection of data are frequently organized into a
hierarchy of fields, records and files.
• This organization of data may not complex enough to
maintain and efficiently process certain collections of
data.
• For this reason, data are organized into more complex
type of structures called Data Structures.
To learn about these data structures, we have to
follow these three steps:
1.Logical or mathematical description of the
structure.
2.Implementation of the structure on a
computer.
3.Quantitative analysis of the structure, which
includes determining the amount of memory
needed to store the structure and the time
required to process the structure.
Data Structures
Data Structures
The logical or mathematical model of a particular organization of data is called a data
structure.
Types of Data Structure
D E F
4
A data structure is said to be linear if its elements form a sequence,
or, in other words, a linear list.
There are two basic ways of representing such linear structures in
memory.
1)One way is to have the linear relationship between the elements
represented by means of sequential memory locations. These linear
structures are called arrays.
2)The other ways is to have the linear relationship between the
elements represented by means of pointers or links. These linear
structures are called linked lists.
info next info next info next
list null
1. It must be rich enough in structure to mirror the actual relationships of data in the
real world.
2. The structure should be simple enough that one can effectively process data when
necessary.
10
20 10
30
40 20 30
50
60
70 40 50 60 70
80
Figure 2: Array with 8 items Figure 3: Tree with 8 nodes
6
Data Structure Operations
2. Searching: Finding the location of the record with a given key value.
6. Merging: Combing the records in two different sorted files into a single
sorted file.
7
Two more operations
• Sorting
• Merging
Algorithms
It is a well-defined set of instructions used to solve a particular problem.
Example:
Write an algorithm for finding the location of the largest element of an array Data.
9
Complexity of Algorithms
10
O-notation
- A function f(n)=O(g(n)) if there are positive constants c and n 0 such that
- When f(n)=O(g(n)), it is guaranteed that f(n) grows at a rate no faster than g(n).
Example:
11
Some rules related to asymptotic notation
Rule-1
If fa(n) = O(ga(n)) and fb(n) = O((gb(n)) then
(a) fa(n)+fb(n) = max(O(ga(n)),O(gb(n))
(b) fa(n) * fb(n) = O(ga(n) * gb(n))
Rule-2
If f(n) is a polynomial of degree k, then f(n) = Θ(nk).
Rule-3
Logkn = O(n) for any constant.
12
Typical Growth Rates
Function Name
c Constant
logn Logarithmic
log2n Log-squared
n Linear
nlogn
n2 Quadratic
n3 Cubic
2n Exponential
13