Skip to content

Commit b28994a

Browse files
DAA_Lab
DAA Lab KIIT 5th semester.
1 parent 1a453e8 commit b28994a

File tree

32 files changed

+1308
-0
lines changed

32 files changed

+1308
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
void insertionSort(int arr[], int nElements)
4+
{
5+
int p, key, q;
6+
for (p = 1; p < nElements; p++)
7+
{
8+
key = arr[p];
9+
q = p - 1;
10+
while (q >= 0 && arr[q] > key)
11+
{
12+
arr[q + 1] = arr[q];
13+
q = q - 1;
14+
}
15+
arr[q + 1] = key;
16+
}
17+
}
18+
void printArray(int arr[], int n)
19+
{
20+
int i;
21+
for (i = 0; i < n; i++)
22+
cout << arr[i] << " ";
23+
cout << endl;
24+
}
25+
int main()
26+
{
27+
int arr[] = {5, 8, 9, 6, 7, 18, 15};
28+
int n = sizeof(arr) / sizeof(arr[0]);
29+
insertionSort(arr, n);
30+
printArray(arr, n);
31+
return 0;
32+
}

LAB/Lab1(14th July)/q1.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//WAP to find the smallest and largest element in an array take input from the user and generate random number.
2+
3+
#include<stdio.h>
4+
5+
int main()
6+
{
7+
int a[50],i,n,large,small;
8+
printf("\nEnter the number of elements :");
9+
scanf("%d",&n);
10+
printf("\nInput the array elements :");
11+
for(i=0;i<n;++i)
12+
scanf("%d",&a[i]);
13+
14+
large=small=a[0];
15+
16+
for(i=1;i<n;++i)
17+
{
18+
if(a[i]>large)
19+
large=a[i];
20+
21+
if(a[i]<small)
22+
small=a[i];
23+
}
24+
25+
printf("\nThe smallest element is %d\n",small);
26+
printf("\nThe largest element is %d\n",large);
27+
28+
return 0;
29+
}

LAB/Lab1(14th July)/q1.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
#include<iostream>
3+
4+
using namespace std;
5+
int main()
6+
{
7+
int n;
8+
cin>>n;
9+
int arr[n];
10+
for(int i=0;i<n;i++)
11+
{
12+
arr[i] = rand();
13+
}
14+
for(int i=0;i<n;i++)
15+
{
16+
cout<<arr[i]<<" ";
17+
}
18+
cout<<endl;
19+
int max=INT_MIN,min=INT_MAX;
20+
for(int i=0;i<n;i++)
21+
{
22+
if(arr[i]>max)
23+
max=arr[i];
24+
if(arr[i]<min)
25+
min=arr[i];
26+
}
27+
cout<<"largest number"<<max<<endl;
28+
cout<<"smallest number"<<min<<endl;
29+
30+
}

LAB/Lab1(14th July)/q1random.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
#include<iostream>
3+
4+
using namespace std;
5+
int main()
6+
{
7+
int n;
8+
cin>>n;
9+
int arr[n];
10+
for(int i=0;i<n;i++)
11+
{
12+
arr[i] = rand();
13+
}
14+
for(int i=0;i<n;i++)
15+
{
16+
cout<<arr[i]<<" ";
17+
}
18+
cout<<endl;
19+
int max=INT_MIN,min=INT_MAX;
20+
for(int i=0;i<n;i++)
21+
{
22+
if(arr[i]>max)
23+
max=arr[i];
24+
if(arr[i]<min)
25+
min=arr[i];
26+
}
27+
cout<<"largest number"<<max<<endl;
28+
cout<<"smallest number"<<min<<endl;
29+
30+
}

LAB/Lab1(14th July)/q2.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
void swap1(int &x, int &y) {
5+
int temp = x;
6+
x = y;
7+
y = temp;
8+
9+
}
10+
11+
int main()
12+
{
13+
int a, b, c;
14+
cin>>a>>b>>c;
15+
16+
swap1(a,c);
17+
swap1(b,c);
18+
cout<<a<<b<<c;
19+
20+
21+
return 0;
22+
}

LAB/Lab1(14th July)/q3.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
int n;
6+
cin>>n;
7+
int arr[n];
8+
for(int i=0;i<n;i++)
9+
{
10+
cin>>arr[i];
11+
}
12+
int max=INT_MIN,min=INT_MAX;
13+
for(int i=0;i<n;i++)
14+
{
15+
if(arr[i]>max)
16+
max=arr[i];
17+
if(arr[i]<min)
18+
min=arr[i];
19+
}
20+
cout<<"largest number"<<max<<endl;
21+
cout<<"smallest number"<<min<<endl;
22+
23+
}

LAB/Lab1(14th July)/q4.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using namespace std;
2+
3+
int ss(vector<int>&arr, int n) {
4+
int small = arr[0];
5+
int ssmall = arr[1];
6+
for(int i = 0;i<small;i++) {
7+
if(arr[i] < small) {
8+
ssmall = small;
9+
small = arr[i];
10+
}
11+
}
12+
13+
for(int i = 0; i<n;i++) {
14+
if(arr[i] < ssmall && arr[i] != small) {
15+
ssmall = arr[i];
16+
}
17+
}
18+
19+
return ssmall;
20+
}
21+
22+
// second largest
23+
24+
int sl(vector<int>&arr, int n) {
25+
int large = arr[0];
26+
int slarge = arr[1];
27+
for(int i = 0;i<n;i++) {
28+
if(arr[i] > large) {
29+
slarge = large;
30+
large = arr[i];
31+
}
32+
}
33+
34+
for(int i = 0; i<n;i++) {
35+
if(arr[i] > slarge && arr[i] != large) {
36+
slarge = arr[i];
37+
}
38+
}
39+
40+
return slarge;
41+
}
42+
43+
int main()
44+
{
45+
int n;
46+
cin>>n;
47+
48+
vector<int>arr(n);
49+
50+
for(int i = 0; i<n;i++) {
51+
cin>>arr[i];
52+
}
53+
int secondm = ss(arr,n);
54+
int secondl = sl(arr,n);
55+
cout<<"second smallest is :"<<secondm<<" and second large : "<<secondl;
56+
return 0;
57+
}

LAB/Lab10(9th Nov)/DFS BFS.txt

Whitespace-only changes.

LAB/Lab10(9th Nov)/q1.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// BFS DFS
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
vector<int> adj[8];
5+
DFS(int s, int V)
6+
{
7+
vector<bool> visited(V, false);
8+
9+
stack<int> stack;
10+
stack.push(s);
11+
12+
while (!stack.empty())
13+
{
14+
int s = stack.top();
15+
stack.pop();
16+
if (!visited[s])
17+
{
18+
cout << s << " ";
19+
visited[s] = true;
20+
}
21+
for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
22+
if (!visited[*i])
23+
stack.push(*i);
24+
}
25+
}
26+
27+
void BFS(int s, int V)
28+
{
29+
vector<bool> visited;
30+
visited.resize(V, false);
31+
list<int> queue1;
32+
visited[s] = true;
33+
queue1.push_back(s);
34+
35+
while (!queue1.empty())
36+
{
37+
s = queue1.front();
38+
cout << s << " ";
39+
queue1.pop_front();
40+
for (auto adjecent : adj[s])
41+
{
42+
if (!visited[adjecent])
43+
{
44+
visited[adjecent] = true;
45+
queue1.push_back(adjecent);
46+
}
47+
}
48+
}
49+
}
50+
void addEdge(int u, int v)
51+
{
52+
adj[u].push_back(v);
53+
adj[v].push_back(u);
54+
}
55+
56+
int main()
57+
{
58+
addEdge(0, 1);
59+
addEdge(0, 2);
60+
addEdge(1, 3);
61+
addEdge(2, 3);
62+
63+
cout << "BFS= ";
64+
BFS(0, 8);
65+
cout << "\nDFS= ";
66+
DFS(0, 8);
67+
68+
return 0;
69+
}

LAB/Lab2(21 July)/bubble seletion.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)