Skip to content

added some useful codes #39

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 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d7a6bae
ADDED FILE
shivamsahu55 Oct 3, 2020
320986c
Merge pull request #1 from shivamsahu55/master
supershivam13 Oct 3, 2020
eadbbfc
Update README.md
shivamsahu55 Oct 3, 2020
53062b4
Merge pull request #2 from shivamsahu55/master
supershivam13 Oct 3, 2020
516ced7
Add files via upload
manishy20 Oct 3, 2020
07a3d6f
Merge pull request #3 from manishy20/master
supershivam13 Oct 3, 2020
3da5b47
Update README.md
7phalange7 Oct 3, 2020
345d8cd
Merge pull request #4 from 7phalange7/patch-1
supershivam13 Oct 3, 2020
9b9279b
Add files via upload
supershivam13 Oct 3, 2020
a039b67
Order n sorting algorithms
7phalange7 Oct 3, 2020
77beeaa
Merge pull request #5 from 7phalange7/master
supershivam13 Oct 3, 2020
77654e8
Median Selection Algorithm
7phalange7 Oct 3, 2020
1edc502
Merge pull request #6 from 7phalange7/master
supershivam13 Oct 3, 2020
4e709b4
Add files via upload
supershivam786 Oct 4, 2020
bb6c47b
Merge pull request #7 from supershivam786/master
supershivam13 Oct 4, 2020
bbc4166
Update README.md
supershivam786 Oct 4, 2020
7989e62
Merge pull request #8 from supershivam786/master
supershivam13 Oct 4, 2020
0ef82de
Add files via upload
supershivam55 Oct 6, 2020
e7d936f
Merge pull request #9 from supershivam55/master
supershivam13 Oct 6, 2020
7e52af1
Add files via upload
shivamsahuscs55 Oct 9, 2020
6902b2f
Merge pull request #10 from shivamsahuscs55/master
supershivam13 Oct 9, 2020
cfd01c3
improved description
b17byb17 Oct 27, 2020
e2439a9
Merge pull request #11 from b17byb17/patch-2
supershivam13 Oct 28, 2020
d040301
Update README.md
supershivam1303 Oct 29, 2020
6019edd
Merge pull request #12 from supershivam1303/master
supershivam13 Oct 29, 2020
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
34 changes: 34 additions & 0 deletions BinaryNumberUsingQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// CPP function to generate all binary number
// from 0 to given number n
#include<iostream>
#include<stdlib.h>
using namespace std;

// Maximum length of generated binary number
const int MAX = 100;

// CPP function to generate all binary number
// for given number n
char binaryGenerator(int n)
{
char a[MAX];

for (int i = 0; i <= n; i++) {

// use define function itoa() to convert
// in given base
// a is char[] array where value store
// 2 is base, which convert.
itoa(i, a, 2);

cout << a << endl;
}
}

// Driven program
int main()
{
int n = 10;
binaryGenerator(n);
return 0;
}
25 changes: 25 additions & 0 deletions Bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<bits/stdc++.h>
using namespace std;
void bubble_sort(int arr[],int n){
for(int round=1;round<n;round++){

for(int i=0;i<n-round;i++){
if(arr[i]>arr[i+1]){
swap(arr[i],arr[i+1]);
}
}
}
}
int main(){
int n; cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];

bubble_sort(arr,n);

for(int i=0;i<n;i++)
cout<<arr[i]<<" ";


}
60 changes: 60 additions & 0 deletions Bucket Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>

using namespace std;

void Insertionsort(int a[],int n)
{
for(int i=1;i<n;i++)
{
int key = a[i];
int j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j--;
}
a[j+1] = key;
}
}

void BucketSort(int a[], int n, int k)
{
int max = 0;
for (int i = 0; i < n; i++)
if (max < a[i])
max = a[i];
max++;

int bucket[k][n];
int size[k] = {0};

for (int i = 0; i < n; i++)
{
int index = k * a[i] / max;
bucket[index][size[index]] = a[i];
size[index]++;
}
for (int i = 0; i < k; i++)
{
Insertionsort(bucket[i], size[i]);
}
for (int i = 0, j = 0; j < k; j++)
{
for (int l = 0; l < size[j]; l++)
a[i++] = bucket[j][l];
}
}

int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) //input
cin >> arr[i];
BucketSort(arr, n, 5);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++) //output
cout << arr[i] << " ";
}
95 changes: 95 additions & 0 deletions Counting Divisors(using Sieve).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <bits/stdc++.h>
using namespace std;

#define ll long long



//Seive Approach - Generating a array containing prime numbers
vector<int> PrimeSieve(int *p, int n) {

//first all odd numbers as prime as they can be potential prime
for (int i = 3; i <= 1000000; i += 2) {
p[i] = 1;
}
//Sieve
for (ll i = 3; i <= 1000000; i += 2) {
//if current number is not marked (it is prime)
if (p[i] == 1) {
//mark all the multiples of i as not prime
for (ll j = i * i; j <= 1000000; j = j + i) {
p[j] = 0;
}
}
}
//Special cases
p[2] = 1;
p[1] = p[0] = 0;

vector<int> primes;
primes.push_back(2);

for (int i = 3; i <= n; i += 2) {
if (p[i] == 1)
primes.push_back(i);

}

return primes;


}

int no_of_divisors(int m, vector<int> &primes) {

int i = 0;
int p = primes[0];
int ans = 1;

while (p * p <= m) {

if (m % p == 0) {
int count = 0;

while (m % p == 0) {
count++;
m = m / p;
}
ans = ans * (count + 1);
}
// go to next position
i++;
p = primes[i];
}

//if m is not reduced to 1 , it means m also a prime number
if (m != 1) {
ans = ans * 2;
}
return ans;

}




int main() {


int p[1000000] = {0};
vector<int> primes = PrimeSieve(p, 100000);

int t; cin >> t;
while (t--) {
int no; cin >> no;
int divs = no_of_divisors(no, primes);
cout << divs << endl;



}

return 0;


}
36 changes: 36 additions & 0 deletions Counting Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
using namespace std;

void Countsort(int a[], int n)
{
int k = 0; //max element
for (int i = 0; i < n; i++)
if (a[i] > k)
k = a[i];
int count[k+1] = {0}, temp[n];
for (int i = 0; i < n; i++)
count[a[i]]++;
for (int i = 1; i <= k; i++)
count[i] = count[i - 1] + count[i];
for (int i = n - 1, j = k - 1; i >= 0; i--)
{
temp[count[a[i]]-1] = a[i];
count[a[i]]--;
}
for (int i = 0; i < n; i++)
a[i] = temp[i];
}

int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)//input
cin >> arr[i];
Countsort(arr, n);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++)//output
cout << arr[i] << " ";
}
40 changes: 40 additions & 0 deletions Factorial of BIG INTEGER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;

int q=2;
int arr[100000] = {0};
arr[0] = 1;
int len = 1;
int x = 0;
int num = 0;
while(q<=n)
{
x=0;
num =0;
while(x<len)
{
arr[x] = arr[x] *q;
arr[x] = arr[x]+num;
num = arr[x]/10;
arr[x] = arr[x]%10;
x++;
}
while(num!=0)
{
arr[len] = num%10;
num = num/10;
len++;
}
q++;
}
len--;
while(len>=0)
{
cout<<arr[len];
len = len-1;
}
}
14 changes: 14 additions & 0 deletions Fibbonaci Numbers using Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
void fun(int n)
{
IntQueue q = new IntQueue();
q.enqueue(0);
q.enqueue(1);
for (int i = 0; i < n; i++)
{
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
print(a);
}
}
16 changes: 16 additions & 0 deletions GCD - Euclid's Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b) {

return b == 0 ? a : gcd(b, a % b);
}



int main() {
int n1, n2;
cin >> n1 >> n2;

cout << gcd(n1, n2) << endl;
}
26 changes: 26 additions & 0 deletions Insertion Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;
void insertion_sort(int [], int );

int main() {
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

insertion_sort(arr, n);
for (int f = 0; f < n; f++) cout << arr[f] << " ";

}


void insertion_sort(int arr[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
for (j = i - 1; j >= 0 && temp < arr[j]; j--)
{ arr[j + 1] = arr[j]; }
arr[j + 1] = temp;
}
}
Loading