Skip to content

Commit 1fa8815

Browse files
authored
Merge branch 'master' into DoubleLinkedListC
2 parents 9a44ec5 + d4b5b4e commit 1fa8815

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> BreadthFirstSearch(int vertex,vector<int> adjacent[],int start_vertex,int destination){
5+
queue<int> que;
6+
//bfsPath will store the path
7+
vector<int> bfsPath;
8+
//this array will take care of duplicate traversing
9+
bool visited[vertex];
10+
vector<int>::iterator it;
11+
que.push(start_vertex);
12+
bfsPath.push_back(start_vertex);
13+
visited[start_vertex]=true;
14+
int flag=0;
15+
while(!que.empty()){
16+
int tem = que.front();
17+
que.pop();
18+
for(it = adjacent[tem].begin();it<adjacent[tem].end();it++){
19+
if(!visited[*it]){
20+
bfsPath.push_back(*it);
21+
//if destination is found
22+
if(*it==destination){
23+
flag=1;
24+
break;
25+
}
26+
visited[*it] = true;
27+
que.push(*it);
28+
}
29+
}
30+
if(flag==1){
31+
break;
32+
}
33+
}
34+
//if we will not find the destination
35+
if(flag==0){
36+
bfsPath.clear();
37+
}
38+
return bfsPath;
39+
}
40+
41+
//this fn will print the path
42+
void printTraversal(vector<int>bfsPath){
43+
vector<int>::iterator it;
44+
for(it = bfsPath.begin();it<bfsPath.end();it++){
45+
cout<<*it<<endl;
46+
}
47+
}
48+
49+
int main() {
50+
//number of vertex
51+
int vertex = 4;
52+
vector<int> adjacent[vertex];
53+
adjacent[0].push_back(1);
54+
adjacent[0].push_back(3);
55+
adjacent[1].push_back(2);
56+
adjacent[2].push_back(3);
57+
//starting vertex
58+
int start_vertex = 0;
59+
//destination
60+
int destination = 3;
61+
vector<int> bfsPath = BreadthFirstSearch(vertex,adjacent,start_vertex,destination);
62+
if(!bfsPath.empty()){
63+
printTraversal(bfsPath);
64+
}else{
65+
cout<<"path not found";
66+
}
67+
return 0;
68+
}
69+

fibonacci_number/FibonacciNumber.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
3+
long long int FibonacciNumber(long long int n)
4+
{
5+
//Because first fibonacci number is 0 and second fibonacci number is 1.
6+
if (n == 1 || n == 2)
7+
{
8+
return (n - 1);
9+
}
10+
else
11+
{
12+
//store last fibonacci number
13+
long long int a = 1;
14+
//store second last fibonacci number
15+
long long int b = 0;
16+
//store current fibonacci number
17+
long long int nth_Fib;
18+
for (long long int i = 3; i <= n; i++)
19+
{
20+
nth_Fib = a + b;
21+
b = a;
22+
a = nth_Fib;
23+
}
24+
return nth_Fib;
25+
}
26+
}
27+
28+
int main()
29+
{
30+
long long int n;
31+
printf("Enter a Number : ");
32+
scanf("%lli", &n);
33+
if (n < 1)
34+
{
35+
printf("Number must be greater than 0");
36+
}
37+
else
38+
{
39+
long long int nth_Fib = FibonacciNumber(n);
40+
printf("Fibonacci Number is %lli", nth_Fib);
41+
}
42+
return 0;
43+
}

heap_sort/heapsort.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void swap(int array[], int index1, int index2) //swapping function wihout using third variable
5+
{
6+
array[index1] = array[index1] + array[index2];
7+
array[index2] = array[index1] - array[index2];
8+
array[index1] = array[index1] - array[index2];
9+
}
10+
void heapify(int array[], int size, int index)
11+
{
12+
int largest = index; //considering largest as root
13+
int left = 2 * index + 1;
14+
int right = 2 * index + 2;
15+
16+
if (left < size && array[left] > array[largest]) //left child is larger then root
17+
largest = left;
18+
19+
if (right < size && array[right] > array[largest]) //right child is larger then root
20+
largest = right;
21+
22+
if (largest != index) //condition if largest is not root
23+
{
24+
swap(array, index, largest);
25+
heapify(array, size, largest);
26+
}
27+
}
28+
void heapsort(int array[], int size)
29+
{
30+
31+
for (int i = size / 2 - 1; i >= 0; i--) //Create heap from array rearrangement
32+
heapify(array, size, i);
33+
34+
for (int i = size - 1; i > 0; i--) //move current root to end a step for Extract min process
35+
{
36+
37+
swap(array, 0, i); //swap element
38+
39+
heapify(array, i, 0); //excecuting heapify to bring all elements in correct position again
40+
}
41+
}
42+
void display(int array[], int size)
43+
{
44+
int i = 0;
45+
for (i = 0; i < size; i++)
46+
cout << array[i] << " ";
47+
cout << endl;
48+
}
49+
int main()
50+
{
51+
int array[] = {6, 3, 6, 343, 42, 1, 43, 35, 34, 43};
52+
int size = sizeof(array) / sizeof(array[0]);
53+
cout << "Unsorted array is :" << endl;
54+
display(array, size); //display the unsorted array
55+
heapsort(array, size);
56+
cout << "sorted array is :" << endl;
57+
display(array, size); //display the sorted array
58+
}

0 commit comments

Comments
 (0)