Alg 1
Alg 1
Introduction
• What is an algorithm?
.The efficiency of the alg for the worst case input of size n, for
which alg runs the largest among all possible inputs of that
size.
. We analyze the alg to see what kind of input yields the
largest value for basic operations count among all possible
inputs of size n .
Eg: c worst(n)=n
Best case efficiency
. The alg runs the fastest among all possible inputs of that
size.
Θ
The function t(n)= (g(n) iff there exist positive constants c1, c2
and no such that c1*g(n)≤t(n)≤c2*g(n) for all n, n≥no
. The theta notation is more precise than both
the big-oh and omega notations.
. The function t(n)=Θ(g(n)) iff g(n) is both an
upper and lower bound on t(n).
Mathematical Analysis of Non-recursive Algs
int main()
{
.
int a[10],n,i,j,temp,min;
printf("enter n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
#include <stdio.h>
int main()
{
int a[10],n,i,j,temp,min;
printf("enter n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if (a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
return 0;
#include <stdio.h>
#include<string.h>
int main()
{
char t[50]="nobody-noticed-him";
char p[50]="not";
int m,n,i,j,f=0;
n=strlen(t);
m=strlen(p);
for(i=0;i<=n-m;i++)
{
j=0;
while((j<m) && (p[j]==t[i+j]))
j=j+1;
if (j==m)
{
printf("matching location=%d",i);
f=1;
}