DA
DA
INTRODUCTION
An algorithm is a Step By Step process to solve a problem, where
each step indicates an intermediate task. Algorithm contains finite
number of steps that leads to the solution of the problem.
#include<stdio.h>
#include<conio.h>
void main()
{
Thus it happens:
Features Of Algorithm
Data: Data means raw facts that are entered stored into the
computer memory ex- numbers, single characters, strings etc
Image Of A List
Linked List
Array Figure 1
Array Figure 2
Data Structure deals with not only the stored elements but also the
relationship among them.
Data structure mainly specifies the following
four things:-
i) Organization of data
ii) Accessing methods
iii) Degree of associativity
iv) Processing alternatives for information
Data Structure
PRIMITIVE DATA STRUCTURE
int → datatype
a → variable Name
10 → size of the array-based
Concept of array:
• The individual element of an array can be
accessed by
specifying name of the array, followed by index or
subscript
inside square bracket.
Exp. to access the 10th element statement will be
a[9].
• The first element of the array has index zero[0]. So
in an
array of 10 elements the first array will be a[0] and
last a[9]
• The elements of array will always be stored in
consecutive
memory location.
LIST:
STACK:
QUEUES:
ANALYSIS ON AN ALGORITHM:
Complexity
Performance of a program: The performance of a program is
measured based on the amount of computer memory and time
needed to run a program.
The two approaches which are used to measure the performance of
the program are:
1. Analytical method → called the Performance Analysis.
2. Experimental method → called the Performance Measurement.
Space Complexity
Space complexity: The Space complexity of a program is defined
as the amount of memory it needs to run to completion.
As said above the space complexity is one of the factor which
accounts for the performance of the program. The space
complexity can be measured using experimental method, which is
done by running the program and then measuring the actual space
occupied by the program during execution. But this is done very
rarely. We estimate the space complexity of the program before
running the program
ASYMPTOTIC NOTATIONS
TYPES OF TRADEOFF
1.Compressed/Uncompressed
2. Re-rendering/Stored Images
3. Smaller code/Loop Unrolling
4. Lookup Table/Recalculation
2. Re-rendering/Stored Images:
When the images are stored more space will be used and less time
will be spent.
When smaller code is used it occupies less space but takes more
time to execute.
When the program is bigger and complicated with loops occupy
more space in memory but takes less time to be executed.
4. Lookup Table/Recalculation:
Linear Search:
Basic Concept
• Basic idea:
– Start at the beginning of the array.
– Inspect elements one by one to see if it matches the key.
• Time complexity:
– A measure of how long an algorithm takes to run.
– If there are n elements in the array:
• Best case: match found in first element (1 search operation)
• Worst case: no match found, or match found in the last element
(n search operations)
• Average case: (n + 1) / 2 search operations
#include <stdio.h>
int linear_search (int a[], int size, int key)
{
for (int i=0; i<size; i++)
if (a[i] == key) return i;
return -1;
}
int main()
{
int x[]={12,-3,78,67,6,50,19,10}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, linear_search (x,8,val);
}
BINARY SEARCH:
Iterative
#include <stdio.h>
int bin_search (int a[], int size, int key)
{
int L, R, mid;
L = 0; R = size – 1;
while (L <= R) {
mid = (L + R) / 2;
if (a[mid] < key) L = mid + 1;
else if (a[mid] > key) R = mid -1;
else return mid; /* FOUND AT INDEX mid */
}
return -1; /* NOT FOUND */
}
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, bin_search (x,8,val);
}
Recursive
#include <stdio.h>
int bin_search (int a[], int L, int R, int key)
{
int mid;
if (R < L) return -1; /* NOT FOUND */
mid = (L + R) / 2;
if (a[mid] < key) return (bin_search(a,mid+1,R,key));
else if (a[mid] > key) return (bin_search(a,L,mid-1,key));
else return mid; /* FOUND AT INDEX mid */
}
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, bin_search (x,0,7,val);
}
Time Complexity:
• If there are n elements in the array.
– Number of searches required in the worst case: log 2 n
• For n = 64 (say).
– Initially, list size = 64. 2 k = n, where k is the
– After first compare, list size = 32. number of steps.
– After second compare, list size = 16. k = log 2n
– After third compare, list size = 8.
– ……. log2 64 = 6
– After sixth compare, list size = 1. log2 1024 = 10