UNIT-1
DATA STRUCTURES:
Definition: A data structure is a set of domains. A designated domain has a set of functions and a
set of axioms.
The set of axioms describes the semantics of the operations. Implementing can be done by the
functions using a conventional programming language.
ALGORITHM:
Definition: An algorithm is a finite set of instructions which, if followed, accomplish a particular
task.
In addition every algorithm must satisfy the following criteria:
Input: there are zero or more quantities which are externally supplied;
Output: at least one quantity is produced;
Definiteness: each instruction must be clear and unambiguous;
Finiteness: algorithm will terminate after a finite number of steps;
Effectiveness: every instruction must be sufficiently basic that it can in principle be carried out by
a person using only pencil and paper.
PERFORMANCE ANALYSIS:
Performance analysis is also called as Priori.
It involves manual calculations.
The values obtained are machine independent.
Analysis involves-
1. Time complexity: Approximate time required to execute the instructions.
2. Space complexity: Storage required to execute the instructions.
PERFORMANCE MEASUREMENT:
Performance measurement is also called as posteriori.
This involves actual measurement of time and space.
The values obtained are machine dependent.
Measurement involves-
1. Run Time: Total time taken from the beginning of execution till the end.
2. Memory: Total memory occupied in the system.
ASYMPTOTIC NOTATION:
Time complexity can be denoted by the following notations.
1. Big (oh) notation -O [Worst case time complexity]
2. Omega notation -Ω [Best case time complexity]
3. Theta notation – Ɵ [Average case time complexity]
Data Structures [G Stalin Babu,Dept of CSE] Page 1
1. Big (oh) notation –O
Big oh can be expressed as f(n)=O(g(n)) iff there exists c, n0 such that f(n)<=c g(n)
For all n>=n0.
2. Omega notation -Ω
Omega can be expressed as f(n)=Ω(g(n)) iff there exists c, n0 such that f(n)>=c g(n)
For all n>=n0.
3. Theta notation – Ɵ
Theta can be expressed as f(n)=Ɵ(g(n)) iff there exists c1,c2, n0 such that
c1g(n)<=f(n)<=c2 g(n) For all n>=n0.
Example:
Selection sort:
I. Time complexity= 3n²-3n
II. Big (oh) notation –O = O(n²) where c=3 and n0=1
III. Omega notation -Ω = Ω(n²) where c=1 and n0=2
IV. Theta notation – Ɵ = Ɵ(n²) where c1=3, c2=1 and n0=2
RECURSION
It is defined as the process in which a procedure invokes itself or invokes other procedures that eventually
invoke the first procedure.
When a procedure contains a call to itself it is called direct recursion and when a procedure calls another
procedure, which in turn calls the first procedure it is called as indirect recursion.
FACTORIAL FUNCTION
Given a positive integer n, n factorial is defined as the product of all integers between n and 1.
Ex: 5 factorial is 5 * 4 * 3 * 2 * 1 = 120
! is often used to denote the factorial function.
n ! = 1 if n == 0
n ! = n * ( n – 1) * ( n – 2 ) * ( n – 3 ) * ------- * 1 if n > 0
The pseudocode can be written as
product = 1;
for( i = n ; i > 0 ; i - - )
product = product * i;
Data Structures [G Stalin Babu,Dept of CSE] Page 2
return(product);
This is called as an iterative procedure. It calls for the explicit repletion of a process until a certain
condition is satisfied. It can be written as a function that returns the product n ! when n is the input.
To write this using the recursive definition the algorithm is
Factorial (Fact, N)
Step 1: if (N = 0)
Fact = 1
Return
Step 2: Factorial (Fact, N -1)
Step 3: Fact = Fact * N
Fibonacci Series
The fibonacci sequence is 0,1,1,2,3,5,8,13,21,34,…….Each element in this sequence is the sum of the
two preceding elements.
It is written as
0+1=1
1+1=2
1+2=3
Recursive definition
Fib(n) = n if n == 0 or n == 1
Fib(n)= fib( n - 2 ) + fib( n – 1 ) if n > = 2
Greatest Common Divisor by Recursion
As a simple rule of recursion, any function can be computed using a recursive routine if :
1. The function can be expressed in its own form.
2. There exists a termination step, the point at which f(x) is known for a particular ‘x’.
Therefore to write the recursive program for finding the greatest common divisor of any two numbers, we
have to express the greatest common divisor (GCD) function in a recursive form using the above 2 rules :
1. if m <= n and n%m = 0 then gcd(n,m) = m (termination step).
2. if n < m then gcd(n,m) = gcd(m,n) (recursive definition of greatest common divisor).
3. if n >= m then gcd(n,m) = gcd(m,n%m) (recursive definition of greatest common divisor)
Data Structures [G Stalin Babu,Dept of CSE] Page 3
#include<stdio.h>
#include<conio.h>
int GCD (int a,int b)
{
if (a<0) a= -a;
if (b<0) b= -b;
if (a==0 || b==1 || a==b) return b;
if (a==1 || b==0) return a;
if (a>b) return GCD (b, a%b);
else return GCD (a, b%a);
}
void main()
{
int x,y;
clrscr();
printf("nEnter 1st number:");
scanf("%d",&x);
printf("nEnter 2nd number:");
scanf("%d",&y);
printf("\nGCD is:%d", GCD(x,y));
getch();
}
Tower of Hanoi Problem
Three pegs A, B and C exist. Disks are placed on A such that the larger disk is always below a smaller disk. The task is to m
five disks to peg C using peg B as a temporary storage.
The rules are:
1. Only the top disk of any peg can be moved to the other peg.
2. The larger disk cannot sit on the smaller disk.
The solution:
General case : n disks.
If n = 1 the solution is to move the disk from A and place it on peg C.
If n = 2 the solution is
1. Move disk 1(smaller disk) from peg A and place it on peg B.
Data Structures [G Stalin Babu,Dept of CSE] Page 4
2. Move disk 2(larger disk ) from peg A and place it on peg C.
3. Move disk 1 from peg B and place it on the larger disk on peg C.
If n = 3 the solution is
1. Move disk 1 from peg A and place it on peg C.
2. Move disk 2 from peg A and place it on peg B.
3. Move disk 1 from peg C and place it on peg B.
4. Move disk 3 from peg A and place it on peg C.
5. Move disk 1 from peg B and place it on peg A.
6. Move disk 2 from peg B and place it on peg C.
7. Move disk 1 from peg A and place it on peg C.
Program for towers of hanio:
#include<stdio.h>
#include<conio.h>
void toh(int, char, char, char);
void main()
{
int n;
clrscr();
printf("\n Enter the number of disks\n");
scanf("%d", &n);
toh(n,'A','B','C');
getch();
}
// Function toh
void toh(int n, char source, char temp, cha dest)
{
if(n==1)
{
printf("\n Move disk %d from peg %c to peg %c", n, source,dest);
return;
}
toh(n-1, source , dest, temp);
printf("\n Move disk %d from peg %c to peg %c", n, source,dest);
toh(n-1, temp, source, dest);
Searching:
It is a process to find whether a particular value with specified properties is present or not among a
collection of items.
Data Structures [G Stalin Babu,Dept of CSE] Page 5
If the value is present in the collection, then searching is said to be successful, and it returns the
location of the value in the array.
Otherwise, if the value is not present in the array, the searching process displays the appropriate
message and in this case searching is said to be unsuccessful.
1) Linear or Sequential Searching 2) Binary Searching
Linear or Sequential Searching
for each item one by one in the list from the first, until the match is found.
Efficiency of Linear search :
Executes in O ( n ) times where n is the number of elements in the list.
Algorithm:
Linear_Search (A[ ], N, val , pos )
Step 1 : Set pos = -1 and k = 0
Step 2 : Repeat while k < N
Begin
Step 3 : if A[ k ] = val
Set pos = k
print pos
Goto step 5
End while
Step 4 : print “Value is not present”
Step 5 : Exit
Binary Search
It is an efficient method of finding out a required item from a given list, provided the list is in order.
The process is:
1.First the middle item of the sorted list is found.
2.Compare the item with this element.
3.If they are equal search is complete.
Data Structures [G Stalin Babu,Dept of CSE] Page 6
4.If the middle element is greater than the item being searched, this process is repeated in
the upper half of the list.
5.If the middle element is lesser than the item being searched, this process is repeated in
the lower half of the list.
Step 1: [If the item is not found]
If First > Last
Return ( -1 ).
Else go to Step 2
Step 2: [Calculate the middle]
Mid = (First + Last) / 2
Step 3: If item < List [mid]
Binary_search (List [ ], item, First, Mid -1)
Step 4: Else if Item > List [Mid]
Binary_search (List [ ], item, Mid + 1, Last)
Step 5: Else
Return (Mid + 1).
Step 6: Stop.
Ex: Array a contains the elements 1,3,4,5,7,9,31,33
The item to be searched is 7. This is a sorted list.
First has the value 0. Last has the value 7.
The middle position in this list is (First + Last) / 2 = (0+7) / 2 = 3.
The middle element is a[3] = 5.
If Item < a [Mid]
Binary_Search (a [ ], Item, First, Mid -1)
Else if Item > a [Mid]
Binary_Search (a [ ], Item, Mid + 1, Last)
Is 7 < 5 then search the list between the first element i.e. 0 and the element with the value Mid – 1 i.e. 2
Data Structures [G Stalin Babu,Dept of CSE] Page 7
Is 7 > 5 then search the list between the element with the value Mid + 1 i.e. a[4] and the last element i.e.
a[7].
The search process is repeated in the lower half of the list. The list is 7,9,31,33
The middle position in this list is now ( 4 + 7 ) / 2 = 5 and the middle element in this new list is 9.
Is 7 < 9 or 7 > 9? Is 7 < 9 then search the list between the element at position 4 and the element with the
value Mid – 1 i.e. 4 . The list is 7,9.
The middle position in this list is (4 + 4 ) / 2 = 4 and the middle element is now 7.
The search returns the element 7.
Program
#include<stdio.h>
#include<conio.h>
#define N 20
int a[N];
int binary();
void sort();
void main()
{
Int ch, n,no,i,j,c;
clrscr();
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter the Array Elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(1)
{
clrscr();
sort(a,n);
Data Structures [G Stalin Babu,Dept of CSE] Page 8
printf("Sorted Array\n");
printf("---------------\n");
sort(a,n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("1.Binary Search\n");
printf("2.Exit\n");
printf("Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("Enter the Search Number:");
scanf("%d",&no);
c=binary(a,no,n);
if(c!=0)
printf("\nNumber is in the list\n");
else
printf("\nNumber is not in the list\
n");
printf("\nEnter any key to continue.\n");
getch();
break;
case 2 : exit(0); break;
default : printf("\nNot a Valid Choice\n");
}
}
}
int binary(int a[], int no,int n)
{
Data Structures [G Stalin Babu,Dept of CSE] Page 9
int mid,low,high,c=0;
high=n-1,low=0;
while(low <=high)
{
mid=(low+high)/2;
if(a[mid]==no)
{
c++;
break;
}
else if(a[mid]<no)
low=mid+1;
Else
high=mid-1;
}
return(c);
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i] > a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
Data Structures [G Stalin Babu,Dept of CSE] Page 10
}
}
}
Data Structures [G Stalin Babu,Dept of CSE] Page 11