Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AdarshAddee committed Oct 4, 2022
2 parents 3b7f763 + 0476e9a commit 980e33f
Show file tree
Hide file tree
Showing 17 changed files with 851 additions and 16 deletions.
77 changes: 77 additions & 0 deletions C++/Connected_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░
*/

// Github: GarvitV957

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<long long>
#define vb vector<bool>
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()

int N=1e6 +1;
vvi adj(N);
vi vis(N,0);

vvi cc;
vi current_comp;

void dfs(int i){
vis[i]=1;
current_comp.pb(i);
for(auto v:adj[i]){
if(!vis[v]){
dfs(v);
}
}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n,e;
cin>>n>>e;
for(int i=0;i<e;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y),adj[y].pb(x);
}
int c=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
current_comp.clear();
dfs(i);
cc.pb(current_comp);
c++;
}
}
cout<<c<<endl;
cout<<cc.size()<<endl;
return 0;
}
65 changes: 65 additions & 0 deletions C++/Counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// C++ Program for counting sort
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define RANGE 200

// The main function that sort
// the given string arr[] in
// alphabetical order
void countSort(char arr[])
{
// The output character array
// that will have sorted arr
char output[strlen(arr)];

// Create a count array to store count of individual
// characters and initialize count array as 0
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));

// Store count of each character
for (i = 0; arr[i]; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains actual
// position of this character in output array
for (i = 1; i <= RANGE; ++i)
count[i] += count[i - 1];

// Build the output character array
for (i = 0; arr[i]; ++i) {
output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
}

/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
For Logic : See implementation
*/

// Copy the output array to arr, so that arr now
// contains sorted characters
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}

// Driver code
int main()
{
char arr[] = "countingsortincpp";

countSort(arr);

cout << "Sorted character array is " << arr;
return 0;
}
//github : atinder11


61 changes: 61 additions & 0 deletions C++/Merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
chiragchandnani10
https://github.com/chiragchandnani10
*/

#include<bits/stdc++.h>
using namespace std;
void merging(int input[],int start,int end){
int mid = (start+end)/2;
int i=start, j=mid+1, k=start;
int ans[end+1];
while(i<=mid&&j<=end){
if(input[i]<input[j]){
ans[k]=input[i];
i++;
k++;
}
else{
ans[k]=input[j];
j++;
k++;
}
}
while(i<=mid){
ans[k]=input[i];
i++;
k++;
}
while(j<=end){
ans[k]=input[j];
j++;
k++;
}
for(int s=start;s<=end;s++){
input[s]=ans[s];
}
}

void merge_sort(int input[],int start,int end){
if(start>=end){
return;
}
int mid = ((start+end)/2);
merge_sort(input,start,mid);
merge_sort(input,mid+1,end);
merging(input,start,end);


}



void mergeSort(int input[], int size){
// Write your code here

merge_sort(input,0,size-1);



}

18 changes: 18 additions & 0 deletions C++/Power_of_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Author: ADARSH
// Date Modified: 01/10/2022

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

bool isPowerOf2(int n) {
return (n && !(n & (n - 1)));
}

int main(int argc, char const *argv[])
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << ((isPowerOf2(n)) ? "It's a power of 2." : "It's not a power of 2.");
return 0;
}
50 changes: 50 additions & 0 deletions C++/bucketSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Authors Name : Utkarsh Tyagi
Date Modified: 1 October, 2022
*/
// C++ program to sort an
// array using bucket sort
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to sort arr[] of
// size n using bucket sort
void bucketSort(float arr[], int n)
{

// 1) Create n empty buckets
vector<float> b[n];

// 2) Put array elements
// in different buckets
for (int i = 0; i < n; i++) {
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}

// 3) Sort individual buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());

// 4) Concatenate all buckets into arr[]
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}

/* Driver program to test above function */
int main()
{
float arr[]
= { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 };
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);

cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
17 changes: 17 additions & 0 deletions C++/factorialprogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;
// github username navdeepk037 https://github.com/navdeepk037
int fact(int n)
{
int factorial=1;
for(int i=n;i>=1;i--)
factorial=factorial*i;
return factorial;
}
int main(){
int n;
cout<<"enter the number ";
cin>>n;
cout<<"the factorial of the number is "<<fact(n);

}
40 changes: 40 additions & 0 deletions C/BubbleSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// PradeepKhatri - https://github.com/PradeepKhatri

#include <stdio.h>

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

void BubbleSort(int *A,int n)
{
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}

int main()
{
int A[] = {5,7,3,1,2};
int n = 5;
printArray(A,n);
BubbleSort(A,n);
printArray(A,n);

}

56 changes: 56 additions & 0 deletions C/selection_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Author : Prathamesh Patil
In selection sort,
we get the least from array and place it at first position, then recursively leaving the first element do the same
*/


#include<stdio.h>

//declaration of display function
void display(int array[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
}

//declaration of funtion :- selection sort
void selection_sort(int array[], int size)
{
int least, i, j, temp, l;

for ( i = 0; i < size; i++) //loop for accessing the ith element from array
{
least = array[i]; //taking the i element as the least starting from 0
printf("Least : %d")

for ( j = i + 1; j < size; j++) //loop for comparing the array elements with ith element
{
if (least > array[j]) //getting the least element from the array
{
least = array[j]; //storing it in the least variable
l=j; //storing the index of least variable in l
}
}
//swap logic in least and ith element
temp = array[i];
array[i] = array[l];
array[l] = temp;
}
}

int main()
{
int array[5] = {23,11,1,35,21};
int size = 5;

//calling the selection_sort funtion
selection_sort(array, size);

//calling the display function
display(array, size);

return 0;
}
Loading

0 comments on commit 980e33f

Please sign in to comment.