DAA Lab Ex 1 - 5 PDF - 1
DAA Lab Ex 1 - 5 PDF - 1
DAA Lab Ex 1 - 5 PDF - 1
No:1(a)
IMPLEMENT AND ANALYZE SORTING ALGORITHMS:
Dt: SELECTION SORT AND BUBBLE SORT
SELECTION SORT
AIM:
To develop a c program for the implementation selection sort.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,i,j,min,x,temp;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
min=a[i];
x=i;
for(j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
x=j;
}
}
temp=a[i];
a[i]=a[x];
a[x]=temp;
}
printf(“sorting elements are:\n”);
for(i=0;i<n;i++)
{
printf (“%d\n”,a[i]);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for selection sort is implemented successfully.
Ex. No:1(b)
IMPLEMENT AND ANALYZE SORTING ALGORITHMS:
Dt: SELECTION SORT AND BUBBLE SORT
BUBBLE SORT
AIM:
To develop a c program for the implementation bubble sort.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[100],n,i,j,swap;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
swap=a[i];
a[i]=a[j];
a[j]=swap;
}
}
}
printf(“sorting elements are:\n”);
for(i=0;i<n;i++)
{
printf (“%d\n”,a[i]);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for bubble sort is implemented successfully.
Ex. No:2(a)
IMPLEMENT AND ANALYZE SEARCHING
Dt: ALGORITHMS:
SEQUENTIAL SEARCH AND BINARY SEARCH
SEQUENTIAL SEARCH
AIM:
To develop a c program for the implementation Sequential search.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[100],n,i,key;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter the number to be searched:”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
if(a[i]==key)
{
printf(“Searching array element=\n a[%d] = %d \n ”,i,a[i]);
break;
}
if(a==n)
{
printf (“The no. is not in Array \n”);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for Sequential search is implemented successfully.
Ex. No:2(b)
IMPLEMENT AND ANALYZE SEARCHING
Dt: ALGORITHMS:
SEQUENTIAL SEARCH AND BINARY SEARCH
BINARY SEARCH
AIM:
To develop a c program for the implementation Binary search.
ALGORITHM:
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for Binary search is implemented successfully.
Ex. No: 3
IMPLEMENT AND ANALYZE RECURSIVE ALGORITHMS
Dt:
AIM:
To develop a c program for the implementation Recursive Algorithms by
merge sort.
ALGORITHM:
Merge sort (left, right)
//Input : An array
If left>right
Return
mid=(left+right)/2
end.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void partition(int [ ], int, int );
void mergesort(int [ ], int, int, int);
int main(){
int a[10],n,i;
printf("Enter the limit:");
scanf("%d",&n);
printf("Enter the array elements are:");
for(i=0;i<n;i++){
scanf("\n%d",&a[i]);
}
partition(a,0,n-1);
printf("after merge sort:\n");
for(i=0;i<n;i++){
printf("\n%d",a[i]);
}
return 0;
}
void partition (int a[ ], int low, int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(a,low,mid);
partition(a,mid+1,high);
mergesort(a,low,mid,high);
}
}
void mergesort(int a[ ], int low, int mid, int high){
int l,mi,k,lo,t[20];
lo=low;
l=low;
mi=mid+1;
while((lo<=mid)&&(mi<=high)){
if(a[10]<=a[mi]){
t[l]=a[lo];
lo++;
}
else{
t[l]=a[mi];
mi++;
}
l++;
}
if(lo>mid){
for(k=mi;k<=high;k++){
t[l]=a[k];
l++;
}
}
else{
for(k=lo;k<=mid;k++){
t[l]=a[k];
l++;
}
}
for(k=low;k<=high;k++){
a[k]=t[k];
}
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement Recursive Algorithm by merge sort
using C was executed and verified.
Ex. No: 4
IMPLEMENT AND ANALYZE BRUTE- FORCE STRING
Dt: MATCHING PROBLEM
AIM:
To develop a c program for the implementation Brute force String
matching problem.
ALGORITHM:
ALGORITM Brute force String match (T[0…n-1],P[0…m-1])
//Output: The index of the first character in the text that starts a.
j←0
j←j+1
if j=m return i
return -1
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int match(char [100],char [100],int l1,int l2);
char a[100],b[100];
void main(){
int i,l1=0,l2=0,res=0;
clrscr();
printf("Enter the string 1:");
scanf("%s",&a);
printf("Enter the string 2:");
scanf("%s",&b);
l1=strlen(a);
l2=strlen(b);
res=match(a,b,l1,l2);
if(res==-1){
printf("\npattern not found");
}
else{
printf("\n pattern is found at position=%d",res+1);
}
getch();
}
int match(char a[100],char b[100],int l1,int l2){
int i,j=0;
for(i=0;i<=l1-l2;i++){
while(j<l2 && b[j]==a[i+j]){
j=j+1;
if(j==l2){
return i;
}
}
}
return -1;
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
AIM:
To develop a c program for the implementation Min – Max by divide and
conquer approach.
ALGORITHM:
Rec _ Min _ Max (A[0…n-1])
if (start==end)
Max =A[start]
Min=A[start]
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1,min1,mid;
if(i==j)
{
max=min=a[i];
}
else
{
if(i==j-1)
{
if(a[i]<a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid=(i+j)/2;
maxmin(i,mid);
max1=max;
min1=min;
maxmin(mid+1, j);
if(max<max1)
{
max=max1;
}
if(min >min1)
{
min=min1;
}
}
}
}
void main ()
{
int i, n;
clrscr();
printf ("\nEnter the limit : ");
scanf ("%d",&n);
printf ("Enter the array numbers are : \n");
for (i=1;i<=n;i++)
{
scanf ("%d",&a[i]);
}
max = a[0];
min = a[0];
maxmin(1,n);
printf ("Minimum element is: %d\n", min);
printf ("Maximum element is : %d\n", max);
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT: