DS Unit 1
DS Unit 1
Unit-I
Unit-IA Introduction: Basic Terminology: Elementary Data Organization, Data Structure
Operations, Algorithms Complexity and Time-Space Trade off.
Unit-IB Arrays: Array Definition and Analysis, Representation of Linear Arrays in Memory,
Traversing, Insertion And Deletion in Array, Single Dimensional Arrays, Two Dimensional
Arrays, Bubble Sorting, Selection Sorting, Linear Search, Binary Search, Multidimensional
Arrays, Function Associated with Arrays, Character String in C, Character String Operations,
Arrays as parameters, Implementing One Dimensional Array.
Unit-IA
Algorithm
Flowchart
Program
Variables
Runtime
Storage
Rate of growth
Scalability
Commonly used rates of growth
Type of analysis: best case, average case, worst case
Asymptotic notations: represent upper and lower bounds
Big-O Notation (Upper Bounding Function)
O(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n > n0}.
g(n) is an asymptotic tight upper bound for f(n).
Our objective is to give the smallest rate of growth g(n) which is greater than or equal to the
given algorithm’s rate of growth.
Example-1: Find upper bound for f(n) = 3n + 8
Ω(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0}.
g(n) is an asymptotic tight lower bound for f(n).
Our objective is to give the largest rate of growth g(n) which is less than or equal to the given
algorithm’s rate of growth.
Example-1: Find lower bound for f(n) = 5n2.
Θ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for
all n ≥ n0}.
g(n) is an asymptotic tight bound for f(n).
Θ(g(n)) is the set of functions with the same order of growth as g(n).
Why is it called asymptotic analysis?
For a given function f(n) we are trying to find another function g(n) which approximates f(n) at
higher values of n.
Loops: The running time of a loop is, at most, the running time of the statements inside the loop
(including tests) multiplied by the number of iterations.
Nested loops: Analyze from the inside out. Total running time is the product of the sizes of all
the loops.
If-then-else statements: Worst-case running time: the test, plus either the then part or the else
part (whichever is the larger).
Logarithmic complexity: An algorithm is O(log n) if it takes a constant time to cut the problem
size by a fraction (usually by ½).
Unit-IB
Array: linear data structure, similar elements, ordered, stored in contiguous memory locations
int *ptr ;
ptr=A;
ptr=&A[0];
Linear_search (A, n, T)
i=1
While i <= n
If A[i] = T
Return i
Return -1
Binary_search (A, n, T)
L=1
R=n
While L <= R
m = floor ((L + R)/2)
If A[m] = T
Return m
Else if A[m] < T
L = m+1
Else if A[m] > T
R = m-1
Return -1
Read the array left to right and compare two consecutive elements and swap them if necessary
Repeat till the array is sorted
Bubble_sort (A, n)
i=1
while i<=n-1
j=n-1
while j>=i
if A[j]>A[j+1]
swap A[j] and A[j+1]
j=j-1
i=i+1
Selection_sort (A, n)
i=1
while i<=n-1
min=i
j=i+1
while j<=n
if A[j]<A[min]
min=j
j=j+1
swap A[i] and A[min]
i=i+1
int A[10][20];
A[5][8]=100;
Matrix multiplication
Multi-dimensional array
int A[10][20][30];
A[5][8][12]=250;
//function call
int A[10];
int x = func1 (A);
//function definition
int* func2 ()
{int A[10];
return A;
}
//function call
int *ptr;
ptr = func2 ();
Character string
char s1[50];
cin>>s1;
cout<<s1;
gets(s1);
puts(s1);
End of Unit