Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DAA Lab Ex 1 - 5 PDF - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Ex.

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:

\\ sorts a given array by selection sort


ALGORITHM selection sort (A[0…n-1])
{
\\ Input: An array A[0…n-1] of elements
\\ Output; Array A[0…n-1] in sorted order
for i←0 to n-2 do min←i
for j←i+1 to n-1do
ifA[j]<A[Min]
min←j
swap A[i] and A[min]
end for
}

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:

ALGORITHM selection sort (A[0…n-1])


\\ sorts a given array by bubble sort
\\ Input: An array A[0…n-1] of elements
\\ Output; Array A[0…n-1] in sorted order
for i←0 to n-2 do min←i
for j←0 to n-2-I do
ifA[j+1]<A[j}
swap A[i] and A[j+1]

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:

INPUT: list of size N. Target value T


OUTPUT: position of T in list I.
BEGIN
Set found to false
Set I to 0
While (I==N) and (Found is false)
If list (I) = T
Found = True
Else
I=I+1
End
if found is false
I is not present in list
END

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:

ALGORITHM Binary search


INPUT: list of elements to be given as input
OUTPUT: Displaying the searched elements
A← sorted array
n← Size of array
x← Value to be searched
set lower bound = 1
set upper bound = n
while x not found
if upper bond < low bound
EXIT; x does not exists
set mid point = lower bound+(upper bound-lower bound)/2
if A[mid point]<x
set lower bound = midpoint+1
if A[mid point]>x
set upper bound = mid point-1
if A[mid point]=x
EXIT: x found at location mid point
End while
END
PROGRAM:
#include<stdio.h>
#include<conio.h>
void search();
void main( ){
int a[100],n,i,key,mid,low,high;
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);
void search(){
int low=0
high=n-1;
mid=(low+high)/2;
if(a[mid]==key){
printf{"\n Found the location=%d",mid);
}
else if(key<mid){
n=mid;
serach();
}
else if(key>mid){
low=mid+1;
search();
}
else{
printf("\n Value not found");
}
getch( );
}

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)

//Implement merge sort with a search key

//Input : An array

//Output: The index of the first element in A[0..n-1]

//or-1 if no such element is found

Merge sort (arr,left,right)

If left>right

Return

mid=(left+right)/2

merge sort (arr, left, mid)

merge sort (arr, mid+1,right)

merge (arr, left, mid, right)

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])

//implements brute force using matching

//Input: An array T[0…n-1] of n characters representing a test and an


array

P[0…m-1] of m character representing a pattern.

//Output: The index of the first character in the text that starts a.

//Matching substring or -1 if the search is unsuccessful.

for i←0 to n-m do

j←0

while j<m and P[j]=T[i+j] do

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:

Thus the program to implement Brute-force string matching using c


was executed and verified.
Ex. No: 5
IMPLEMENT AND ANALYZE MIN-MAX ALGORITHM
Dt: USING DIVIDE AND CONQUER APPROACH

AIM:
To develop a c program for the implementation Min – Max by divide and
conquer approach.

ALGORITHM:
Rec _ Min _ Max (A[0…n-1])

//Return min and max element by finding using recursive call

//Input: An array A[0…n-1] of elements

//Output: Min _ Minimum element and max _ Maximum elements.

int[ ] find MinMax (int A[], int Start, int end)

int max, min;

//case 1: only one element in the given array

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:

Thus the min-max Algorithm using Divide and conquer approach


using c implemented successfully.

You might also like