Skip to content

Commit cc41005

Browse files
author
=
committed
New codes are addedgit add .!
1 parent e17538f commit cc41005

File tree

12 files changed

+366
-0
lines changed

12 files changed

+366
-0
lines changed

Lab2/a.out

8.66 KB
Binary file not shown.

Lab2/binarysearch.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<stdio.h>
2+
#include<time.h>
3+
int binarysearch(int A[],int n,int el);
4+
int binarysearch(int A[],int n,int el)
5+
{
6+
int l,u,mid,flag=0;
7+
l=0;
8+
u=n-1;
9+
while(l<u)
10+
{
11+
mid=(l+u)/2;
12+
if(el>A[mid])
13+
l=mid+1;
14+
else if (el<A[mid])
15+
u=mid-1;
16+
else
17+
{
18+
flag=1;
19+
break;
20+
}
21+
}
22+
}
23+
int main()
24+
{
25+
clock_t start,end;
26+
double t; int i,n,flag,el;
27+
printf("Program for Binary Search!\n");
28+
printf("PLease note that binary searc assumes that the array is already sorted.\n Hence please enter the array values accordingly else this program might fail.\n");
29+
printf("Enter the array size you want: ");
30+
scanf("%d",&n);
31+
int A[n];
32+
for(i=0;i<n;i++)
33+
{
34+
printf("Enter the %d element of the array: ",i+1);
35+
scanf("%d",&A[i]);
36+
}
37+
printf("Enter the element you want to search: ");
38+
scanf("%d",&el);
39+
printf("Performing the binary search\n");
40+
start=clock();
41+
flag=binarysearch(A,n,el);
42+
end=clock();
43+
if(flag==0)
44+
printf("Element does not exist in the array\n");
45+
else
46+
printf("Element exist in the array\n");
47+
t=(double)(end-start)/CLOCKS_PER_SEC;
48+
printf("Time taken by the process in seconds is : %f\n",t);
49+
return 0;
50+
}

Lab2/linearsearch.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<stdio.h>
2+
#include<time.h>
3+
int linearsearch(int A[],int el,int n)
4+
{
5+
int i,flag=0;
6+
for(i=0;i<n;i++)
7+
{
8+
if(A[i]==el)
9+
{
10+
flag=1;
11+
break;
12+
}
13+
}
14+
return flag;
15+
}
16+
int main()
17+
{
18+
int n,i,el,flag;
19+
clock_t start,end;
20+
double time;
21+
printf("Enter the number of elements in the array: ");
22+
scanf("%d",&n);
23+
int A[n];
24+
for(i=0;i<n;i++)
25+
{
26+
printf("Enter the %d element of the array: ",i+1);
27+
scanf("%d",&A[i]);
28+
}
29+
printf("Enter the element you want to search in the array: ");
30+
scanf("%d",&el);
31+
start=clock();
32+
flag=linearsearch(A,el,n);
33+
end=clock();
34+
time = (double)(start-end)/CLOCKS_PER_SEC;
35+
if(flag==1)
36+
printf("The element exists in the array");
37+
else
38+
printf("The element does not exist in the array\n");
39+
printf("Time taken for linear search is :%f\n",time);
40+
return 0;
41+
}

Lab2/merge.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include<iostream>
2+
using namespace std;
3+
//l is lowest index and r is the max index (left and right)
4+
void merge(int A[],int l,int m ,int r)
5+
{
6+
//this function is to merge the partitioned arrays
7+
//creating temp arrays
8+
int n1 = m-l+1;
9+
int n2 = r-m;
10+
int A1[n1],A2[n2],i,j,k;
11+
//First subarray begins from l to m
12+
//second subarray begins from m+1 to r
13+
//Filling the array
14+
for(i=0;i<n1;i++)
15+
{
16+
A1[i] = A[l+i];
17+
}
18+
for(j=0;j<n2;j++)
19+
{
20+
A2[j] = A[m+1+j];
21+
}
22+
//Refilling the old array with correct order
23+
i=0; //counter for A1
24+
j=0;//counter for A2
25+
k=l;//counter for A
26+
while((i<n1) && (j<n2))
27+
{
28+
if (A1[i]<=A2[j])
29+
{
30+
A[k]=A1[i];
31+
i++;
32+
}
33+
else
34+
{
35+
A[k]=A2[j];
36+
j++;
37+
}
38+
k++;
39+
}
40+
//If any elements remain fill them because above while loop ends when either of the subarryas is filled
41+
while(i<n1)
42+
{
43+
A[k]=A1[i];
44+
i++;
45+
k++;
46+
}
47+
while(j<n2)
48+
{
49+
A[k]=A2[j];
50+
k++;
51+
j++;
52+
}
53+
}
54+
///////////////////////////////////////////////////////////////////
55+
//Main merge sort function
56+
void mergesort(int A[],int l,int r)
57+
{
58+
if(l<r)
59+
{
60+
int m=(l+r)/2;
61+
//Sorting to first and second halves
62+
mergesort(A,l,m);
63+
mergesort(A,m+1,r);
64+
//merging them
65+
merge(A,l,m,r);
66+
}
67+
}
68+
int main()
69+
{
70+
int n,l,r,i;
71+
cout<<"Enter the array size you want: ";
72+
cin>>n;
73+
int A[n];
74+
for(i=0;i<n;i++)
75+
{
76+
cout<<"Enter "<<i+1<<" element of the array: ";
77+
cin>>A[i];
78+
}
79+
cout<<"Lets do merge sorting"<<endl;
80+
mergesort(A,0,n-1);
81+
for(i=0;i<n;i++)
82+
{
83+
cout<<i+1<<" element of the array is: "<<A[i]<<endl;
84+
}
85+
}

a.out

8.59 KB
Binary file not shown.
8.55 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int a,n,i,p=1;
5+
printf("Enter the number whose power you want: ");
6+
scanf("%d",&a);
7+
printf("Enter the number who must be exponent: ");
8+
scanf("%d",&n);
9+
for(i=0;i<n;i++)
10+
p=p*a;
11+
printf("Exponent of %d over %d is: %d\n",a,n,p);
12+
return 0;
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<stdio.h>
2+
int power(int a,int n)
3+
{
4+
int i,p=1;
5+
if(n==1)
6+
return p;
7+
else
8+
{
9+
p=p*a;
10+
power(a,n-1);
11+
}
12+
}
13+
int main()
14+
{
15+
int a,n,p;
16+
printf("Enter the number whose exponent you want: ");
17+
scanf("%d",&a);
18+
printf("Enter the exponent value: ");
19+
scanf("%d",&n);
20+
p=power(a,n);
21+
printf("The value of %d over %d is: %d\n",a,n,p);
22+
return 0;
23+
}

inputtimedependsonuser.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<stdio.h>
2+
#include<time.h>
3+
int main()
4+
{
5+
clock_t start,end;
6+
//Note that clock functions capturers current clock puls... Hence we divide by clocks per sec to get answer in seconds
7+
double t;
8+
int a;
9+
start=clock();
10+
scanf("%d",&a);
11+
end=clock();
12+
t=(double)(end-start)/CLOCKS_PER_SEC;
13+
printf("User took these nanoseconds: %f\n",t*1000000000);
14+
return 0;
15+
}

matrix/a.out

12.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)