Skip to content

Commit

Permalink
Lab C++ programs added
Browse files Browse the repository at this point in the history
  • Loading branch information
sbshah97 committed Feb 20, 2017
1 parent 1c33857 commit 5c05122
Show file tree
Hide file tree
Showing 23 changed files with 11,476 additions and 25 deletions.
51 changes: 51 additions & 0 deletions labs/lab2/Quick_Sort/Quick_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>

int arr[10];

int part(int low, int high) {
int pivot = arr[high];
int temp, i = low - 1;

for(int j = low; j <= high - 1; j ++) {
if(arr[j] <= pivot) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;

return (i + 1);
}

void quick_sort(int low, int high) {
if(low < high) {
int partition = part(low, high);
quick_sort(low, partition-1);
quick_sort(partition+1, high);
}
}

void print_arr(int n) {
for(int i = 0; i < n; i ++) {
printf("%d ",arr[i]);
}
printf("\n");
}

int main() {
int n;
scanf("%d",&n);

for(int i = 0; i < n; i ++)
scanf("%d",&arr[i]);

quick_sort(0, n - 1);

print_arr(n);

return 0;
}
Binary file removed labs/lab2/Quick_Sort/a.out
Binary file not shown.
Binary file added labs/lab2/Quick_Sort/quick
Binary file not shown.
Binary file added labs/lab4/a.out
Binary file not shown.
Binary file added labs/lab4/dfs
Binary file not shown.
47 changes: 47 additions & 0 deletions labs/lab4/dfs_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>

using namespace std;

vector<int> adj[10];
bool visited[10];
int num;

void dfs(int source) {
visited[source] = true;
num += 1;
for(int i = 0; i < adj[source].size(); ++i)
if(visited[adj[source][i]] == false) {
dfs(adj[source][i]);
}
}

void initialize(int n) {
for(int i = 0; i < n; i ++)
visited[i] = false;
}

int main() {
//Number of nodes and edges resp.
int n, m;
scanf("%d %d", &n, &m);

int x, y;
for(int i = 0 ; i < m; i ++) {
scanf("%d %d",&x,&y);
adj[x].push_back(y);
adj[y].push_back(x);
}

initialize(m);

int source;
scanf("%d",&source);

num = 0;
if(visited[source] == false)
dfs(source);

printf("%d\n",n - num);

return 0;
}
51 changes: 51 additions & 0 deletions labs/lab4/dfs_recursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>

using namespace std;

vector<int> vert[10];
bool visited[10];

void dfs(int s) {
visited[s] = true;
printf("%d->",s);
for(int i = 0; i < vert[s].size(); ++i) {
if(visited[vert[s][i]] == false) {
dfs(vert[s][i]);
}
}
}

void initialize(int v) {
for(int i = 0; i < v; i ++) {
visited[i] = false;
}
}

int main() {
int v, e;
scanf("%d",&v);
scanf("%d",&e);

int x, y;
for(int i = 0; i < e; i ++) {
scanf("%d %d",&x,&y);
vert[x].push_back(y);
vert[y].push_back(x);
}

initialize(v);

printf("Begin->");

for(int i = 0; i < v; i ++) {
if(visited[i] == false) {
dfs(i);
printf("\n");
//connectedComponents++;
}
}

printf("End");
//printf("Number of connected components\n->%d");
return 0;
}
12 changes: 12 additions & 0 deletions labs/lab4/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
10 10
8 1
8 3
7 4
7 5
2 6
10 7
2 8
10 9
2 10
5 10
2
Binary file removed labs/lab5/a.out
Binary file not shown.
Binary file removed labs/lab5/marr
Binary file not shown.
25 changes: 0 additions & 25 deletions labs/lab5/marriage.cpp

This file was deleted.

Binary file removed labs/lab5/stable
Binary file not shown.
Binary file added labs/lab6/Trial/brute
Binary file not shown.
Binary file added labs/lab6/Trial/divndconq
Binary file not shown.
11 changes: 11 additions & 0 deletions labs/lab6/Trial/input_10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10
9061
7432
3768
2305
5318
4837
3457
4761
7827
1528
101 changes: 101 additions & 0 deletions labs/lab6/Trial/input_100.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
100
9061
7432
3768
2305
5318
4837
3457
4761
7827
1528
9830
1030
1079
82
5303
9049
7848
8694
1375
4678
4895
8471
6321
3552
3695
5738
3278
8947
1492
7609
6275
4770
5019
4260
1292
4554
9097
4750
3532
6901
495
3339
2148
1574
7661
1669
4841
5486
4580
6216
4382
9475
8905
680
3005
2577
635
500
1501
6367
8109
7776
1115
3105
6254
6647
1876
5328
5614
5408
6447
327
2964
8595
1901
603
4481
959
306
9062
1393
8928
8514
275
9608
5736
7092
221
6236
8593
6588
4323
563
1920
1645
6817
2785
3522
6363
8399
Loading

0 comments on commit 5c05122

Please sign in to comment.