forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
848 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.