Skip to content

Commit c952a4b

Browse files
author
myselfshravan
committed
12th prog added
1 parent 9bc666c commit c952a4b

File tree

6 files changed

+105
-44
lines changed

6 files changed

+105
-44
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"files.associations": {
33
"ostream": "cpp",
4-
"iostream": "cpp"
4+
"iostream": "cpp",
5+
"span": "cpp",
6+
"type_traits": "cpp",
7+
"chrono": "cpp"
58
}
69
}

12.Travelling_Salesman.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*Branch and Bound Algorithm for Travelling Sales Person*/
2+
#include <stdio.h>
3+
4+
int a[10][10], visited[10], n, cost = 0;
5+
void get()
6+
{
7+
int i, j;
8+
printf("Enter No.of Cities: ");
9+
scanf("%d", &n);
10+
printf("\nEnter Cost Matrix: \n");
11+
for (i = 0; i < n; i++)
12+
{
13+
printf("\nEnter Elements of Row #: %d\n", i + 1);
14+
for (j = 0; j < n; j++)
15+
scanf("%d", &a[i][j]);
16+
visited[i] = 0;
17+
}
18+
printf("\nThe cost list is:\n");
19+
for (i = 0; i < n; i++)
20+
{
21+
printf("\n\n");
22+
for (j = 0; j < n; j++)
23+
printf("\t%d", a[i][j]);
24+
}
25+
}
26+
int least(int c)
27+
{
28+
int i, nc = 999;
29+
int min = 999, kmin;
30+
for (i = 0; i < n; i++)
31+
{
32+
if ((a[c][i] != 0) && (visited[i] == 0))
33+
if (a[c][i] < min)
34+
{
35+
min = a[i][0] + a[c][i];
36+
kmin = a[c][i];
37+
nc = i;
38+
}
39+
}
40+
if (min != 999)
41+
cost += kmin;
42+
return nc;
43+
}
44+
void mincost(int city)
45+
{
46+
int i, ncity;
47+
visited[city] = 1;
48+
printf("%d->", city + 1);
49+
ncity = least(city);
50+
if (ncity == 999)
51+
{
52+
ncity = 0;
53+
printf("%d", ncity + 1);
54+
cost += a[city][ncity];
55+
return;
56+
}
57+
mincost(ncity);
58+
}
59+
void put()
60+
{
61+
printf("\n\nMinimum cost: ");
62+
printf("%d", cost);
63+
}
64+
65+
int main()
66+
{
67+
get();
68+
printf("\n\nThe Path is:\n\n");
69+
mincost(0);
70+
put();
71+
return 0;
72+
}

4.merge_sort.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ void Merge(int *a, int low, int high, int mid)
88
{
99
int i, j, k, temp[high - low + 1];
1010
i = low;
11-
k = 0;
1211
j = mid + 1;
12+
k = 0;
1313

1414
while (i <= mid && j <= high)
1515
{

lab-manual/1.GCD.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#include <iostream>
22
using namespace std;
33
#include <time.h>
4+
45
long int middle_school(long int m, long int n)
56
{
67
long int i, gcd;
78
clock_t start, end;
89
start = clock();
910
for (i = 1; i <= m && i <= n; ++i)
1011
{
11-
// Checks if i is factor of both integers
1212
if (m % i == 0 && n % i == 0)
1313
gcd = i;
1414
}
1515
end = clock();
16-
cout << endl
17-
<< "Time taken:" << (end - start) / CLOCKS_PER_SEC << " sec , GCD ";
16+
cout << "Time taken: " << (float(end - start) / CLOCKS_PER_SEC) << " sec , GCD ";
1817
return gcd;
1918
}
2019

@@ -30,8 +29,7 @@ long int euclid(long int m, long int n)
3029
n = r;
3130
}
3231
end = clock();
33-
cout << endl
34-
<< "Time taken:" << (end - start) / CLOCKS_PER_SEC << " sec , GCD ";
32+
cout << "Time taken: " << (float(end - start) / CLOCKS_PER_SEC) << " sec , GCD ";
3533
return m;
3634
}
3735

@@ -55,28 +53,17 @@ long int sub(long int m, long int n)
5553
}
5654

5755
end = clock();
58-
cout << "Time taken :" << (end - start) / CLOCKS_PER_SEC << " sec , GCD ";
56+
cout << "Time taken: " << (float(end - start) / CLOCKS_PER_SEC) << " sec , GCD ";
5957
return m;
6058
}
6159

6260
main()
6361
{
6462
long int x, y;
65-
cout << "\t\t ANALYSIS OF THE TWO ALGORITHMS" << endl
66-
<< endl;
67-
cout << "GCD : " << endl;
68-
cout << "enter two numbers:";
63+
cout << "\t\t ANALYSIS OF THE TWO ALGORITHMS" << endl;
64+
cout << "GCD : Enter Two numbers: ";
6965
cin >> x >> y;
70-
cout << endl
71-
<< endl
72-
<< "GCD-Middle School : " << middle_school(x, y);
73-
cout << endl
74-
<< endl
75-
<< "GCD-Euclid : " << euclid(x, y);
76-
cout << endl
77-
<< endl
78-
<< "------------------------------------------------";
79-
cout << endl
80-
<< endl
81-
<< "GCD- subtraction : " << sub(x, y);
66+
cout << "\nGCD-Middle School : " << middle_school(x, y);
67+
cout << "\nGCD-Euclid : " << euclid(x, y);
68+
cout << "\nGCD-Subtraction : " << sub(x, y);
8269
}

lab-manual/2.Linear_Binary.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include <iostream>
22
using namespace std;
33
#include <time.h>
4+
45
int Linear_search()
56
{
67
int arr[10], n, i, num, index;
78
clock_t start, end;
89
cout << "Linear Search: Enter Number of elements: ";
910
cin >> n;
10-
cout << "Enter the Elements";
11+
cout << "Enter the Elements: ";
1112
for (i = 0; i < n; i++)
1213
cin >> arr[i];
1314
cout << "\n Enter a Number to Search: ";
@@ -23,13 +24,12 @@ int Linear_search()
2324
}
2425
cout << "\nFound at Index No." << index;
2526
end = clock();
26-
cout << "Time taken :" << (end - start) / CLOCKS_PER_SEC << " sec , Linear Search ";
27-
cout << endl;
27+
cout << "\nTime taken :" << (end - start) / CLOCKS_PER_SEC << " sec , Linear Search.";
2828
return 0;
2929
}
3030
int Binary_search()
3131
{
32-
int i, arr[10], n, num, first, last, middle;
32+
int i, arr[10], n, num, low, high, mid;
3333
clock_t start, end;
3434
cout << "Binary Search: Enter number of elements";
3535
cin >> n;
@@ -39,31 +39,30 @@ int Binary_search()
3939
cout << "\nEnter Element to be Search: ";
4040
cin >> num;
4141
start = clock();
42-
first = 0;
43-
last = n - 1;
44-
middle = (first + last) / 2;
45-
while (first <= last)
42+
low = 0;
43+
high = n - 1;
44+
while (low <= high)
4645
{
47-
if (arr[middle] < num)
48-
first = middle + 1;
49-
else if (arr[middle] == num)
46+
mid = (low + high) / 2;
47+
if (arr[mid] < num)
48+
low = mid + 1;
49+
else if (arr[mid] == num)
5050
{
51-
cout << "\nThe number, " << num << " found at Position " << middle + 1;
51+
cout << "\nThe number, " << num << " found at Position " << mid + 1;
5252
break;
5353
}
5454
else
55-
last = middle - 1;
56-
middle = (first + last) / 2;
55+
high = mid - 1;
5756
}
58-
if (first > last)
57+
if (low > high)
5958
cout << "\nThe number, " << num << " is not found in given Array";
6059
end = clock();
61-
cout << "Time taken :" << (end - start) / CLOCKS_PER_SEC << " sec , Binary Search ";
60+
cout << "\nTime taken :" << (end - start) / CLOCKS_PER_SEC << " sec , Binary Search ";
6261
cout << endl;
6362
return 0;
6463
}
6564
int main()
6665
{
67-
Binary_search();
6866
Linear_search();
67+
Binary_search();
6968
}

lab-manual/3.Bubble_Insertion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void insertionSort(int *array, int size)
1212
int key, j;
1313
for (int i = 1; i < size; i++)
1414
{
15-
key = array[i]; // take value
15+
key = array[i];
1616
j = i;
1717
while (j > 0 && array[j - 1] > key)
1818
{
@@ -22,16 +22,16 @@ void insertionSort(int *array, int size)
2222
array[j] = key; // insert in right place
2323
}
2424
}
25+
2526
void bubblesort(int *a, int size)
2627
{
2728
int i, j;
2829
for (i = 0; i < size - 1; i++)
29-
30-
// Last i elements are already in place
3130
for (j = 0; j < size - i - 1; j++)
3231
if (a[j] > a[j + 1])
3332
swap(a[j], a[j + 1]);
3433
}
34+
3535
int main()
3636
{
3737
int n;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy