Skip to content

Added Sorting algorithm #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Algorithms/Graphs/breadth_first_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Graph Traversal Algorithm
//Language Used: C++
//Breadth First Traversal algorithm to traverse all nodes of a graph considering only one component in graph
//Output: This algorithm will give nodes visited in breadth first manner and distance of each node from source.
//author:namanvats
#include<bits/stdc++.h>
using namespace std;
#define MAX5 100010
#define pb push_back
int dist[MAX5];
bool visit[MAX5];
vector<int> vec[MAX5];
queue<int> q;
void bfs(int src)
{
memset(visit,0,sizeof(visit));
dist[src]=0;
q.push(src);
while(q.size()!=0)
{
int p=q.front();
cout<<p<<" ";
q.pop();
visit[p]=1;
for(int i=0;i<vec[p].size();i++)
{
if(visit[vec[p][i]]==0)
{
q.push(vec[p][i]);
dist[vec[p][i]]=dist[p]+1;
visit[vec[p][i]]=1;
}
}
}
}
int main()
{
//Nodes are numbered from 1
cout<<"Enter number of Nodes and Edges"<<'\n';
int nodes,edges;
cin>>nodes>>edges;
cout<<"Enter edges between nodes"<<'\n';
for(int i=0;i<edges;i++)
{
//undirected edges
int x,y;
cin>>x>>y;
vec[x].pb(y);
vec[y].pb(x);
}
cout<<"Enter Source node"<<'\n';
int src;
cin>>src;
cout<<"The nodes are:"<<'\n';
bfs(src);
cout<<'\n';
cout<<"Distance of each node starting from source:"<<'\n';
for(int i=1;i<=nodes;i++)
{
cout<<"Distance of node"<<" "<<i<<":"<<" "<<dist[i]<<"\n";
}
return 0;
}
44 changes: 44 additions & 0 deletions Algorithms/Searching/binary_searching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Program to search in an array using binary search
// Language used: C++
// Author: ExpressHermes

#include <bits/stdc++.h>
using namespace std;

int binary_search(int *ar, int low, int high, int key)
{
if(high < low)
return -1;

int mid = low + (high - low) / 2;
if(key == ar[mid])
return mid;
else if(key < ar[mid])
return binary_search(ar, low, mid - 1, key);
else if(key > ar[mid])
return binary_search(ar, mid + 1, high, key);

}

int main()
{
int n, key;
cout << "Enter the size of array: ";
cin >> n;
int ar[n];
cout << "Enter the elements: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

cout << "Enter the value to be searched: ";
cin >> key;

int result = binary_search(ar, 0, n-1, key);

if(result == -1)
cout << "The key is not present in the array" << endl;
else
cout << "The key is present in array at index: " << result << endl;


}
42 changes: 42 additions & 0 deletions Algorithms/Searching/linear_searching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Program to search in an array using Linear Search
// Language used: C++
// Author: ExpressHermes


#include <bits/stdc++.h>
using namespace std;

int linear_search(int *ar, int n, int key)
{
int found = 0, i;
for(i = 0; i < n - 1; i++)
{
if(ar[i] == key)
{
found = 1;
break;
}
}

if(found == 1)
cout << "The key is present in the array at index: " << i << endl;
else
cout << "The key is not present in the array" << endl;
}

int main()
{
int n, key;
cout << "Enter the size of array: ";
cin >> n;
int ar[n];
cout << "Enter the elements: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

cout << "Enter the value to be searched: ";
cin >> key;

linear_search(ar, n, key);

}
47 changes: 47 additions & 0 deletions Algorithms/Sorting/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Program to sort an array in ascending order using Binary Sorting
// Language used: C++
// Description: In binary sort, we constantly swap the adjacent elements in wrong order
// Author: ExpressHermes

#include <bits/stdc++.h>
using namespace std;

void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}

void bubble_sort(int *ar, int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if(ar[j] > ar[j+1])
{
// swap elements in the wrong order
swap(&ar[j], &ar[j+1]);
}
}

}
}

int main()
{
int n;
cout << "Enter the number of elements: " << endl;
cin >> n;
int ar[n];
cout << "Enter the elements of array: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

bubble_sort(ar, n);

for(int i = 0; i < n; i++)
cout << ar[i] << " ";
cout << endl;
}
41 changes: 41 additions & 0 deletions Algorithms/Sorting/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Program to sort an array in ascending order using Insertion Sorting
// Language used: C++
// Description: In insertion sort, we pick an element a[i] and we
// insert into it's right position in the sorted subarray ar[0] and ar[i-1]
// Author: ExpressHermes

#include <bits/stdc++.h>
using namespace std;

void insertion_sort(int *ar, int n)
{
for (int i = 1; i < n; i++)
{
int value = ar[i];
int j = i - 1;

while (j >= 0 && ar[j] > value)
{
ar[j + 1] = ar[j];
j = j - 1;
}
ar[j + 1] = value;
}
}

int main()
{
int n;
cout << "Enter the number of elements: " << endl;
cin >> n;
int ar[n];
cout << "Enter the elements of array: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

insertion_sort(ar, n);

for(int i = 0; i < n; i++)
cout << ar[i] << " ";
cout << endl;
}
51 changes: 51 additions & 0 deletions Algorithms/Sorting/quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Program to sort an array in descending order using Quick Sort
// Language used: C++
// Description: Quick Sort works on Divide and Conquer algorithm. In this we take a pivot and partition the
// whole array around this pivot. We put the pivot at correct elements at correct position and
// put all the smaller elements(smaller than pivot) after the pivot and all the greater elements
// before the pivot.
// Author: ExpressHermes

#include <bits/stdc++.h>
using namespace std;

void quick_sort(int *ar, int n)
{
if(n < 2)
return;

int pivot = ar[n/2];
int i, j;
for(i = 0, j = n - 1; ;i++, j--)
{
while(ar[i] > ar[pivot]) i++;
while(ar[j] < ar[pivot]) j--;

if(i >= j)
break;

int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}

quick_sort(ar, i);
quick_sort(ar + i, n - i);
}

int main()
{
int n;
cout << "Enter the number of elements: " << endl;
cin >> n;
int ar[n];
cout << "Enter the elements of array: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

quick_sort(ar, n);

for(int i = 0; i < n; i++)
cout << ar[i] << " ";
cout << endl;
}
48 changes: 48 additions & 0 deletions Algorithms/Sorting/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Program to sort an array in ascending order using Selection Sorting
// Language used: C++
// Description: In every iteration of selection sort, the minimum element (considering ascending order)
// from the unsorted subarray is picked and moved to the sorted subarray.
// Author: ExpressHermes

#include <bits/stdc++.h>
using namespace std;

void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}

void selection_sort(int *ar, int n)
{
for(int i = 0; i < n; i++)
{
int min_index = i;
for(int j = i + 1; j < n; j++)
{
if(ar[j] < ar[min_index])
min_index = j;
}

// place the element in it's right position
swap(&ar[i], &ar[min_index]);
}
}

int main()
{
int n;
cout << "Enter the number of elements: " << endl;
cin >> n;
int ar[n];
cout << "Enter the elements of array: " << endl;
for(int i = 0; i < n; i++)
cin >> ar[i];

selection_sort(ar, n);

for(int i = 0; i < n; i++)
cout << ar[i] << " ";
cout << endl;
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ This is a library of various algorithms that are used in competitive programming

### sample algorithm
[disjoint_set_union.cpp](Algorithms/disjoint_set_union/disjoint_set_union.cpp)


READ instructions before making any pull request.