diff --git a/0-1 knapsack.cpp/0-1 knapsack.cpp b/0-1 knapsack.cpp/0-1 knapsack.cpp new file mode 100644 index 0000000..12459c2 --- /dev/null +++ b/0-1 knapsack.cpp/0-1 knapsack.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +// A utility function that returns +// maximum of two integers +int max(int a, int b) { return (a > b) ? a : b; } + +// Returns the maximum value that +// can be put in a knapsack of capacity W +int knapSack(int W, int wt[], int val[], int n) +{ + + // Base Case + if (n == 0 || W == 0) + return 0; + + // If weight of the nth item is more + // than Knapsack capacity W, then + // this item cannot be included + // in the optimal solution + if (wt[n - 1] > W) + return knapSack(W, wt, val, n - 1); + + // Return the maximum of two cases: + // (1) nth item included + // (2) not included + else + return max( + val[n - 1] + + knapSack(W - wt[n - 1], + wt, val, n - 1), + knapSack(W, wt, val, n - 1)); +} + +// Driver code +int main() +{ + int val[] = { 60, 100, 120 }; + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + cout << knapSack(W, wt, val, n); + return 0; +} + +// This diff --git a/Armstrong Number/armstrong_number.js b/Armstrong Number/armstrong_number.js new file mode 100644 index 0000000..bfdbd18 --- /dev/null +++ b/Armstrong Number/armstrong_number.js @@ -0,0 +1,23 @@ +// program to check an Armstrong number of three digits + +let sum = 0; +const number = prompt('Enter a three-digit positive integer: '); + +// create a temporary variable +let temp = number; +while (temp > 0) { + // finding the one's digit + let remainder = temp % 10; + + sum += remainder * remainder * remainder; + + // removing last digit from the number + temp = parseInt(temp / 10); // convert float into integer +} +// check the condition +if (sum == number) { + console.log(`${number} is an Armstrong number`); +} +else { + console.log(`${number} is not an Armstrong number.`); +} diff --git a/BinarySearch/binarysearch.js b/BinarySearch/binarysearch.js new file mode 100644 index 0000000..c2796fb --- /dev/null +++ b/BinarySearch/binarysearch.js @@ -0,0 +1,37 @@ + diff --git a/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp b/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp new file mode 100644 index 0000000..1069d4c --- /dev/null +++ b/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +#define Least_Significant_Bit(i) (i) & -(i) + +vector Binary_Indexed_Tree; + +int TREE_SIZE = 1e5 + 10; + +void update(int index, int value) { + while(index <= TREE_SIZE) { + Binary_Indexed_Tree[index] += value; + index += Least_Significant_Bit(index); + } +} + +int query(int index) { + int sum = 0; + while(index > 0) { + sum += Binary_Indexed_Tree[index]; + index -= Least_Significant_Bit(index); + } + return sum; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + Binary_Indexed_Tree.assign(TREE_SIZE, 0); + int n; + cin >> n; + for(int i = 1; i <= n; i++) { + string operation; + cin >> operation; + if(operation == "U") { + /// Update + int index, value; + cin >> index >> value; + update(index, value); + } + if(operation == "Q") { + /// Query + int index; + cin >> index; + cout << query(index) << "\n"; + } + } + return 0; +} diff --git a/Dijkstra's Algorithm/dijkstra.js b/Dijkstra's Algorithm/dijkstra.js new file mode 100644 index 0000000..9bb6e98 --- /dev/null +++ b/Dijkstra's Algorithm/dijkstra.js @@ -0,0 +1,30 @@ +djikstraAlgorithm(startNode) { + let distances = {}; + + // Stores the reference to previous nodes + let prev = {}; + let pq = new PriorityQueue(this.nodes.length * this.nodes.length); + + // Set distances to all nodes to be infinite except startNode + distances[startNode] = 0; + pq.enqueue(startNode, 0); + this.nodes.forEach(node => { + if (node !== startNode) distances[node] = Infinity; + prev[node] = null; + }); + + while (!pq.isEmpty()) { + let minNode = pq.dequeue(); + let currNode = minNode.data; + let weight = minNode.priority; + this.edges[currNode].forEach(neighbor => { + let alt = distances[currNode] + neighbor.weight; + if (alt < distances[neighbor.node]) { + distances[neighbor.node] = alt; + prev[neighbor.node] = currNode; + pq.enqueue(neighbor.node, distances[neighbor.node]); + } + }); + } + return distances; +} diff --git a/Fibonacci series/fibonacci.c b/Fibonacci series/fibonacci.c new file mode 100644 index 0000000..b59d44f --- /dev/null +++ b/Fibonacci series/fibonacci.c @@ -0,0 +1,14 @@ +#include +int fib(int n) +{ + if(n<=1) + return n; + return fib(n-1)+fib(n-2); +} +int main() +{ + int a; + scanf("%d",&a); + printf("%d",fib(a)); + return 0; +} diff --git a/Fibonacci series/fibonacci.cpp b/Fibonacci series/fibonacci.cpp new file mode 100644 index 0000000..014ee91 --- /dev/null +++ b/Fibonacci series/fibonacci.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; +int main() { + int n1=0,n2=1,n3,i,number; + cout<<"Enter the number of elements: "; + cin>>number; + cout< +// declaration of the variables +var n1 = 0, n2 = 1, next_num, i; +var num = parseInt (prompt (" Enter the limit for Fibonacci Series ")); +document.write( "Fibonacci Series: "); +for ( i = 1; i <= num; i++) +{ document.write ("
" + n1); // print the n1 + next_num = n1 + n2; // sum of n1 and n2 into the next_num + + n1 = n2; // assign the n2 value into n2 + n2 = next_num; // assign the next_num into n2 +} + + diff --git a/Jump Search/jumpSearch.js b/Jump Search/jumpSearch.js new file mode 100644 index 0000000..af2b978 --- /dev/null +++ b/Jump Search/jumpSearch.js @@ -0,0 +1,26 @@ +function jumpSearch(arr, target) { + + let len = arr.length + let step = Math.floor(Math.sqrt(len)); + let blockStart = 0, currentStep = step; + + while (arr[Math.min(currentStep, len) - 1] < target) { + blockStart = currentStep; + currentStep += step; + + if (blockStart >= len) + return -1; + } + + while (arr[blockStart] < target) + { + blockStart++; + if (blockStart == Math.min(currentStep, len)) + return -1; + } + + if (arr[blockStart] == target) + return blockStart + else + return -1; +} diff --git a/Linear Search/linearsearch.js b/Linear Search/linearsearch.js new file mode 100644 index 0000000..5252e70 --- /dev/null +++ b/Linear Search/linearsearch.js @@ -0,0 +1,8 @@ +function linearSearch(arr, key){ + for(let i = 0; i < arr.length; i++){ + if(arr[i] === key){ + return i + } + } + return -1 +} diff --git a/Luhn algorithm/Luhn.py b/Luhn algorithm/Luhn.py new file mode 100644 index 0000000..c267c9b --- /dev/null +++ b/Luhn algorithm/Luhn.py @@ -0,0 +1,23 @@ +""" + Luhn algorithm or Luhn formula + Checksum algorithm. + Source:https://en.wikipedia.org/wiki/Luhn_algorithm + +""" + +def luhn(input_number): + + input_number = [int(i) for i in str(input_number)] + input_number.reverse() + + sum_answer = 0 + for i, number in enumerate(input_number): + temp = number + if i % 2 == 0: + temp = number * 2 + if temp > 9: + temp -= 9 + + sum_answer = sum_answer + temp + + return sum_answer % 10 == 0 diff --git a/README.md b/README.md index 949a3ce..c92c462 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,18 @@ # AlgorithmBank Help us make a deposit of algorithms. -#Rules to Contribute- - +# Rules to Contribute -Check the issues section for new issues.
-Add alogorithms under Algorithm Name/Algorithm File.
-You can contribute in any language.
-You can also raise issues to verify with us.
--You can edit existing algorithms,if it is not right, But please raise an issue if you are doing this to verify with us. - +-You can edit existing algorithms,if it is not right. +# Contributors + + + + If you need the PR for HacktoberFest 2021, please comment it and let us know. diff --git a/Radix sort/Radix_sort.cpp b/Radix sort/Radix_sort.cpp new file mode 100644 index 0000000..927cf97 --- /dev/null +++ b/Radix sort/Radix_sort.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; +void display(int *array, int size) { + for(int i = 0; i pocket[10]; + for(i = 0; i< max; i++) { + m = pow(10, i+1); + p = pow(10, i); + for(j = 0; j> n; + cout << "Enter the maximum digit of elements: "; + cin >> max; + int arr[n]; + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Data before Sorting: "; + display(arr, n); + radixSort(arr, n, max); + cout << "Data after Sorting: "; + display(arr, n); +} diff --git a/Tree/tree_dsa.cpp b/Tree/tree_dsa.cpp new file mode 100644 index 0000000..5b96ed8 --- /dev/null +++ b/Tree/tree_dsa.cpp @@ -0,0 +1,129 @@ +#include +using namespace std; +#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); +typedef long long ll; +const ll mod=1e9+7; +//<------disjoint set-union------> +vectorparent,size; +void make_set(int v) { + parent[v] = v; + size[v] = 1; +} + +void union_sets(int a, int b) { + a = find_set(a); + b = find_set(b); + if (a != b) { + if (size[a] < size[b]) + swap(a, b); + parent[b] = a; + size[a] += size[b]; + } +} + +int find_set(int v) { + if (v == parent[v]) + return v; + return find_set(parent[v]); +} + +//<-----------fenwick tree---------> +struct FenwickTree { + vector bit; // binary indexed tree + int n; + + FenwickTree(int n) { + this->n = n; + bit.assign(n, 0); + } + + FenwickTree(vector a) : FenwickTree(a.size()) { + for (size_t i = 0; i < a.size(); i++) + add(i, a[i]); + } + + int sum(int r) { + int ret = 0; + for (; r >= 0; r = (r & (r + 1)) - 1) + ret += bit[r]; + return ret; + } + + int sum(int l, int r) { + return sum(r) - sum(l - 1); + } + + void add(int idx, int delta) { + for (; idx < n; idx = idx | (idx + 1)) + bit[idx] += delta; + } +}; + +//<-------------square root decomposition--------------> + +void sqrt_decmpO(){ +//input data +int n; +vector a (n); + +// preprocessing +int len = (int) sqrt (n + .0) + 1; // size of the block and the number of blocks +vector b (len); +for (int i=0; i +int MAXN; +int n; +vectort(4*MAXN); +void build(int a[], int v, int tl, int tr) { + if (tl == tr) { + t[v] = a[tl]; + } else { + int tm = (tl + tr) / 2; + build(a, v*2, tl, tm); + build(a, v*2+1, tm+1, tr); + t[v] = t[v*2] + t[v*2+1]; + } +} +int sum(int v, int tl, int tr, int l, int r) { + if (l > r) + return 0; + if (l == tl && r == tr) { + return t[v]; + } + int tm = (tl + tr) / 2; + return sum(v*2, tl, tm, l, min(r, tm)) + + sum(v*2+1, tm+1, tr, max(l, tm+1), r); +} +void update(int v, int tl, int tr, int pos, int new_val) { + if (tl == tr) { + t[v] = new_val; + } else { + int tm = (tl + tr) / 2; + if (pos <= tm) + update(v*2, tl, tm, pos, new_val); + else + update(v*2+1, tm+1, tr, pos, new_val); + t[v] = t[v*2] + t[v*2+1]; + } +} diff --git a/hufman code/hufman_code.c b/hufman code/hufman_code.c new file mode 100644 index 0000000..1972c04 --- /dev/null +++ b/hufman code/hufman_code.c @@ -0,0 +1,181 @@ +// Huffman Coding in C + +#include +#include + +#define MAX_TREE_HT 50 + +struct MinHNode { + char item; + unsigned freq; + struct MinHNode *left, *right; +}; + +struct MinHeap { + unsigned size; + unsigned capacity; + struct MinHNode **array; +}; + +// Create nodes +struct MinHNode *newNode(char item, unsigned freq) { + struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode)); + + temp->left = temp->right = NULL; + temp->item = item; + temp->freq = freq; + + return temp; +} + +// Create min heap +struct MinHeap *createMinH(unsigned capacity) { + struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap)); + + minHeap->size = 0; + + minHeap->capacity = capacity; + + minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct MinHNode *)); + return minHeap; +} + +// Function to swap +void swapMinHNode(struct MinHNode **a, struct MinHNode **b) { + struct MinHNode *t = *a; + *a = *b; + *b = t; +} + +// Heapify +void minHeapify(struct MinHeap *minHeap, int idx) { + int smallest = idx; + int left = 2 * idx + 1; + int right = 2 * idx + 2; + + if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) + smallest = left; + + if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq) + smallest = right; + + if (smallest != idx) { + swapMinHNode(&minHeap->array[smallest], &minHeap->array[idx]); + minHeapify(minHeap, smallest); + } +} + +// Check if size if 1 +int checkSizeOne(struct MinHeap *minHeap) { + return (minHeap->size == 1); +} + +// Extract min +struct MinHNode *extractMin(struct MinHeap *minHeap) { + struct MinHNode *temp = minHeap->array[0]; + minHeap->array[0] = minHeap->array[minHeap->size - 1]; + + --minHeap->size; + minHeapify(minHeap, 0); + + return temp; +} + +// Insertion function +void insertMinHeap(struct MinHeap *minHeap, struct MinHNode *minHeapNode) { + ++minHeap->size; + int i = minHeap->size - 1; + + while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) { + minHeap->array[i] = minHeap->array[(i - 1) / 2]; + i = (i - 1) / 2; + } + minHeap->array[i] = minHeapNode; +} + +void buildMinHeap(struct MinHeap *minHeap) { + int n = minHeap->size - 1; + int i; + + for (i = (n - 1) / 2; i >= 0; --i) + minHeapify(minHeap, i); +} + +int isLeaf(struct MinHNode *root) { + return !(root->left) && !(root->right); +} + +struct MinHeap *createAndBuildMinHeap(char item[], int freq[], int size) { + struct MinHeap *minHeap = createMinH(size); + + for (int i = 0; i < size; ++i) + minHeap->array[i] = newNode(item[i], freq[i]); + + minHeap->size = size; + buildMinHeap(minHeap); + + return minHeap; +} + +struct MinHNode *buildHuffmanTree(char item[], int freq[], int size) { + struct MinHNode *left, *right, *top; + struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size); + + while (!checkSizeOne(minHeap)) { + left = extractMin(minHeap); + right = extractMin(minHeap); + + top = newNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + insertMinHeap(minHeap, top); + } + return extractMin(minHeap); +} + +void printHCodes(struct MinHNode *root, int arr[], int top) { + if (root->left) { + arr[top] = 0; + printHCodes(root->left, arr, top + 1); + } + if (root->right) { + arr[top] = 1; + printHCodes(root->right, arr, top + 1); + } + if (isLeaf(root)) { + printf(" %c | ", root->item); + printArray(arr, top); + } +} + +// Wrapper function +void HuffmanCodes(char item[], int freq[], int size) { + struct MinHNode *root = buildHuffmanTree(item, freq, size); + + int arr[MAX_TREE_HT], top = 0; + + printHCodes(root, arr, top); +} + +// Print the array +void printArray(int arr[], int n) { + int i; + for (i = 0; i < n; ++i) + printf("%d", arr[i]); + + printf("\n"); +} + +int main() { + char arr[] = {'A', 'B', 'C', 'D'}; + int freq[] = {5, 1, 6, 3}; + + int size = sizeof(arr) / sizeof(arr[0]); + + printf(" Char | Huffman code "); + printf("\n--------------------\n"); + + HuffmanCodes(arr, freq, size); +} \ No newline at end of file