diff --git a/c++/LCS/LCS.cpp b/c++/LCS/LCS.cpp new file mode 100644 index 0000000..1582805 --- /dev/null +++ b/c++/LCS/LCS.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include"LongestCommonSubsequence.hh" + +using namespace std; + +int main () { + + int m,n; + + // Reading Text files + string mytext, mysecondtext; + ifstream myfile("text1.txt"); + getline(myfile,mytext); + + ifstream myfile2("text2.txt"); + getline(myfile2,mysecondtext); + + m = int(mytext.length()); + n = int(mysecondtext.length()); + cout << "String 1: " << m << endl; + cout << "String 2: " << n << endl; + + cout <<"LCS: "<< LCS_V2(mytext.c_str(), mysecondtext.c_str(), m, n) << endl; + cout <<"Print LCS: " << LCS_Common(mytext.c_str(), mysecondtext.c_str(), m, n) << endl; + + return 0; +} \ No newline at end of file diff --git a/c++/LCS/LongestCommonSubsequence.hh b/c++/LCS/LongestCommonSubsequence.hh new file mode 100644 index 0000000..dd48269 --- /dev/null +++ b/c++/LCS/LongestCommonSubsequence.hh @@ -0,0 +1,87 @@ +/* + Example: Longest Common Subsequence calculates the number of letters + in common between two strings. + + This example is terrible because it is O(2**n). The key is to use + dynamic programming for this problem or more efficiently, to create + the array and fill it sequentially since it all must be done in any + case. This placeholder is an example of how to contribute an + algorithm, as a header file with a separate test code + that demonstrates its use. + + */ +int LCS(const char* a, const char* b) { + if (*a == '\0' || *b == '\0') + return 0; + if (*a == *b) + return 1 + LCS(a+1,b+1); + return std::max(LCS(a+1,b), LCS(a, b+1)); +} + +int LCS_V2(const char a[], const char b[], int m, int n) { + int memo[m+1][n+1]; + int i, j; + for (i = 0; i < m; i++){ //0(m) + memo[i][0] = 0; + } + for (j = 0; j < n; j++){ //O(n) + memo[0][j] = 0; + } + for (i = 1; i < m + 1 ; i++){ + for (j = 1; j < n + 1; j++){ + if (a[i] == b[j]){ + memo[i][j] = 1 + memo[i-1][j-1]; + } + else { + memo[i][j] = std::max(memo[i][j-1], memo[i-1][j]); + } + } + } + return memo[m][n]; +} + +int LCS_Common(const char a[], const char b[], int m, int n){ + int memo2[m+1][n+1]; + int i, j; + for (i = 0; i < m; i++){ //0(m) + memo2[i][0] = 0; + } + for (j = 0; j < n; j++){ //O(n) + memo2[0][j] = 0; + } + for (i = 1; i < m + 1 ; i++){ + for (j = 1; j < n + 1; j++){ + if (a[i] == b[j]){ + memo2[i][j] = 1 + memo2[i-1][j-1]; + } + else { + memo2[i][j] = std::max(memo2[i][j-1], memo2[i-1][j]); + } + } + } + + int index = memo2[m][n]; + char lcs[index+1]; + lcs[index] = '\0'; + + int x, y; + x = m; + y = n; + while (x > 0 && y > 0) + { + if (a[x-1] == b[y-1]) + { + lcs[index-1] = a[x-1]; + x--; + y--; + index--; + } + else if (memo2[x-1][y] > memo2[x][y-1]) + x--; + else + y--; + } + std::cout << lcs << std::endl; + + return 0; +} \ No newline at end of file diff --git a/c++/LCS/text1.txt b/c++/LCS/text1.txt new file mode 100644 index 0000000..c8e492a --- /dev/null +++ b/c++/LCS/text1.txt @@ -0,0 +1 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \ No newline at end of file diff --git a/c++/LCS/text2.txt b/c++/LCS/text2.txt new file mode 100644 index 0000000..e83bcc9 --- /dev/null +++ b/c++/LCS/text2.txt @@ -0,0 +1 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \ No newline at end of file diff --git a/c++/LongestCommonSubsequence.hh b/c++/LongestCommonSubsequence.hh deleted file mode 100644 index ffebd93..0000000 --- a/c++/LongestCommonSubsequence.hh +++ /dev/null @@ -1,20 +0,0 @@ -/* - Example: Longest Common Subsequence calculates the number of letters - in common between two strings. - - This example is terrible because it is O(2**n). The key is to use - dynamic programming for this problem or more efficiently, to create - the array and fill it sequentially since it all must be done in any - case. This placeholder is an example of how to contribute an - algorithm, as a header file with a separate test code - that demonstrates its use. - - */ -int LCS(const char* a, const char* b) { - if (*a == '\0' || *b == '\0') - return 0; - if (*a == *b) - return 1 + LCS(a+1,b+1); - return max(LCS(a+1,b), LCS(a, b+1)); -} - diff --git a/c++/NumberTheoreticAlgorithms/algorithms.hh b/c++/NumberTheoreticAlgorithms/algorithms.hh new file mode 100644 index 0000000..5ea108b --- /dev/null +++ b/c++/NumberTheoreticAlgorithms/algorithms.hh @@ -0,0 +1,79 @@ +// GCD: greatest common divisor +int gcd(int a, int b) +{ + if (a == 0) + return b; + if (b == 0) + return a; + if (a == b) + return a; + + if (a > b) + return gcd(a - b, b); + + return gcd(a, b - a); +} + +// LCM: lowest common multiple +int lcm(int a, int b) +{ + if (a == 0 && b == 0) + { + std::cout << "A and B can't be both zero for this LCM algorithem."; + return 0; + } + else + { + std::cout << (a * b) / gcd(a, b); + return 0; + } + +} + +// Power +int power(int x, int y) +{ + int prod = 1; + while (y > 0){ + if (y & 1){ + prod = prod * x; + } + x = x * x; + y = y/2; + } + return prod; +} + +/* + Powermod: Modular exponentation + PowerMod[a,b,m] gives the remainder of a^b divided by m. +*/ +int powermod(int a, int b, int m) //O(log n) +{ + int prod = 1; + while (b > 0){ + if (b % 2 != 0){ + prod = (prod * a) % m; + } + a = a * a % m; + b = b/2; + } + return prod; +} + +// Fermat +int fermat(int n, int k) { + return powermod(n, k-2, k); +} + +bool fermat2(int n, int k) { + for (int i = 0; i < k; i++) { + int a = rand() % n+1; + + if (powermod(a, n-1, n) != 1){ + return false; //not prime + } + } + return true; //prime +} + \ No newline at end of file diff --git a/c++/NumberTheoreticAlgorithms/test.cpp b/c++/NumberTheoreticAlgorithms/test.cpp new file mode 100644 index 0000000..9eae9d1 --- /dev/null +++ b/c++/NumberTheoreticAlgorithms/test.cpp @@ -0,0 +1,27 @@ +#include +#include "algorithms.hh" +using namespace std; + +int main(int argc, char *argv[]){ + + int a[argc]; + + cout<<"You have entered: "< +#include +#include +using namespace std; + +bool isPrime(int n) { //O(sqrt n) + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) + return false; // not prime + } + return true; // prime +} + +void Erathosthenes(int num){ + int count = 0; + bool* sieve = new bool [num]; + + for (int i = 2; i <= num; i++){ //O(num) + sieve[i] = 1; + } + +// remove even + for (int j = 4; j <=num; j+=2){ //O(num) + sieve[j] = 0; + } + + for (int k = 3; k <= num; k += 2){ //O(num) + if (sieve[k] == 1){ + for (int l = k*k; l <= num; l = l + 2*k){ + sieve[l] = 0; + } + } + } + + vector list; + // counter of primes + for(int m = 2; m <=num; m++){ + if (sieve[m] == 1){ + list.push_back(m); + count++; + } + } + cout << count << "\n" << "List of Primes: "; + + // if (list.empty()){ + // cout << "No Primes."; + // } + + for (int n = 0; n > N; + + if (isPrime(N) == 0){ + cout << "Number of prime: "; + Erathosthenes(N); + } + else { + cout << N << " is prime." < L) { + x = R - s, y = L + s; + if (data[x] > data[y]){ + R = y; + y = x; + s = (R - L) / phi; + x = R - s; + } + else { + L = x; + x = y; + s = (R - L) / phi; + y = L + s; + } + } + return L; +} + +int binarySearch(int *a, int target) { //O(log n) + if (target < a[0] || target > a[a.size() -1]){ + return -1; + } + int L = 0, R = a.size()-1; + while (R > L) { + int guess = (L + R) / 2; + if (a[guess] > target) { + R = guess - 1; + } + else if (a[guess] < target) { + L = guess + 1; + } + else { + return guess; + } + } + return -1; //not found +} + +int binarySearch_V2(int *a, int L, int R, int target) { //O(log n) + if (L > R){ + return -1; + } + int guess = (L + R) / 2; + if (a[guess] > target) { + return binarySearch_V2(a, L, guess - 1, target); + } + else if (a[guess] < target) { + returnn binarySearch_V2(a, guess + 1 , R, target); + } + else { + return guess; + } +} + +int linearSearch(int *a, int n, int target){ //O(n) + for (int i = 0; i < n; i++){ + if (array[i] == target){ + return i; + } + } + return -1; +} \ No newline at end of file diff --git a/c++/Sorting/Algorithms.hh b/c++/Sorting/Algorithms.hh new file mode 100644 index 0000000..d1c59ba --- /dev/null +++ b/c++/Sorting/Algorithms.hh @@ -0,0 +1,198 @@ +#include +using namespace std; + +//Bubblesort +void bubblesort(int *a, int n) { //O(n^2) + for (int j = n-1; j > 0; j--){ + for (int i = 0; i < j; i++){ + if (a[i] > a[i+1]) { + swap(a[i], a[i+1]); + } + } + } +} + +void bubblesort_Better(int *a, int n) { //O(n) + bool done; + for (int j = n-1; j > 0; j--){ + done = true; + for (int i = 0; i < j; i++){ + if (a[i] > a[i+1]) { + swap(a[i], a[i+1]); + done = false; + } + } + if (done){ + return; + } + } +} + +//Insertion sort +void insertsort(int x[], int n){ + for (int i=1; i < n; i++){ + if (x[i] < x[i-1]) { + int temp = x[i]; + for (int j = i - 1; j >= 0; j--){ + if (temp < x[j] && j != 0) { + x[j+1] = x[j]; + } + else if (temp < x[j] ){ + x[j+1] = x[j]; + x[j] = temp; + } + else { + x[j] = temp ; + break; + } + } + } + } +} + +// Quicksort +void quicksort(int *x, int L, int R){ + int i = L; + int j = R; + int pivot = x[L + (rand() % R-L)]; + if (L >= R) + return; + while (i <= j) { + while (x[i] < pivot) + i++; + while (x[j] > pivot) + j--; + if (i <= j) { + swap (x[i], x[j]); + i++; + j--; + } + } + if (L < j) + quicksort(x, L, j); + if (i < R) + quicksort(x, i, R); +} + +// Heapsort +void makesubheap(int *arr, int i, int n){ + int root = n; + int left = 2*n + 1; + int right = 2*n + 2; + + if (left < i && arr[left] > arr[root]){ //Left > root + root = left; + } + if (right < i && arr[right] > arr[root]){ // Right > root + root = right; + } + if (root != n) //if the largest value is not the root keep swapping and recalling itself + { + swap(arr[n], arr[root]); + makesubheap(arr, i, root); + } +} + +void makeheap (int *a, int n){ + for (int i = n/2; i >= 0; i--){ + makesubheap(a, n, i); + } +} + +void heapsort(int *a, int n){ + makeheap(a, n); + for (int i = n-1; i >= 0; i--){ + swap(a[0], a[i]); // move to correct place + makesubheap(a, i, 0); // heap gets smaller + } +} + +// Mergesort +void MergeSingle(int *A, int low, int mid, int high){ + int i = low; + int j = mid+1; + int k = low; + int *B = new int[high+1]; + while (i <= mid && j <= high){ + if (A[i] < A[j]){ + B[k++] = A[i++]; + } else { + B[k++] = A[j++]; + } + } + while (i <= mid){ + B[k++] = A[i++]; + } + while (j <= high){ + B[k++] = A[j++]; + } + for (int i=low; i<=high; i++){ + A[i] = B[i]; + } + delete [] B; +} + +void mergeSort(int *A, int n){ + int p, l, h ,mid, i; + for(p = 2; p<=n; p=p*2){ + for(i=0; i+p<=n; i=i+p){ + l = i; + h = i+p-1; + mid = (l+h)/2; + MergeSingle(A,l,mid,h); + } + } + if(p/2= 0; i--) { + output[count[(A[i]/radix) % 10] - 1] = A[i]; + count[(A[i]/radix) % 10]--; + } + + for (int i = 0; i < digit; i++){ + A[i] = output[i]; + } +} + +void radix (int *A, int radix){ + int max = A[0]; + for (int i = 1; i < radix; i++){ + if (A[i] > max){ + max = A[i]; + } + } + for (int loc = 1; max/loc > 0; loc *= 10){ + counting_Sort(A, radix, loc); + } +} \ No newline at end of file diff --git a/c++/Sorting/input.txt b/c++/Sorting/input.txt new file mode 100644 index 0000000..83884b9 --- /dev/null +++ b/c++/Sorting/input.txt @@ -0,0 +1 @@ +10 1 \ No newline at end of file diff --git a/c++/Sorting/testing.cpp b/c++/Sorting/testing.cpp new file mode 100644 index 0000000..1ac20fa --- /dev/null +++ b/c++/Sorting/testing.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include "Algorithms.hh" + +using namespace std; + +int main (){ + int a, b, opt; + char cont = 'C'; + + ifstream myfile("input.txt"); + if (myfile.is_open()){ + myfile>>a>>b; + } + + int *c = new int[a]; + int *randA = new int[a]; + + for(int i=0;i> opt; + + switch(opt){ + + case 1: + cout<> cont; + } + +} \ No newline at end of file