diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp index 8158052f8..b592e5931 100644 --- a/others/GCD_of_n_numbers.cpp +++ b/others/GCD_of_n_numbers.cpp @@ -1,23 +1,22 @@ -//This program aims at calculating the GCD of n numbers by division method +// This program aims at calculating the GCD of n numbers by division method #include -using namepsace std; -int main() -{ - cout << "Enter value of n:" << endl; - cin >> n; - int a[n]; - int i, j, gcd; - cout << "Enter the n numbers:" << endl; - for (i = 0; i < n; i++) - cin >> a[i]; - j = 1; //to access all elements of the array starting from 1 - gcd = a[0]; - while (j < n) - { - if (a[j] % gcd == 0) //value of gcd is as needed so far - j++; //so we check for next element - else - gcd = a[j] % gcd; //calculating GCD by division method - } - cout << "GCD of entered n numbers:" << gcd; + +int main() { + int n; + std::cout << "Enter value of n:" << std::endl; + std::cin >> n; + int a[n]; + int i, j, gcd; + std::cout << "Enter the n numbers:" << std::endl; + for (i = 0; i < n; i++) std::cin >> a[i]; + j = 1; // to access all elements of the array starting from 1 + gcd = a[0]; + while (j < n) { + if (a[j] % gcd == 0) // value of gcd is as needed so far + j++; // so we check for next element + else + gcd = a[j] % gcd; // calculating GCD by division method + } + std::cout << "GCD of entered n numbers:" << gcd; + return 0; } diff --git a/others/Decimal To Binary.cpp b/others/decimal_to_binary.cpp similarity index 100% rename from others/Decimal To Binary.cpp rename to others/decimal_to_binary.cpp diff --git a/others/Decimal To Hexadecimal .cpp b/others/decimal_to_hexadecimal.cpp similarity index 100% rename from others/Decimal To Hexadecimal .cpp rename to others/decimal_to_hexadecimal.cpp diff --git a/others/Decimal to Roman Numeral.cpp b/others/decimal_to_roman_numeral.cpp similarity index 100% rename from others/Decimal to Roman Numeral.cpp rename to others/decimal_to_roman_numeral.cpp diff --git a/others/fibonacci.cpp b/others/fibonacci.cpp deleted file mode 100644 index 87ccda6d3..000000000 --- a/others/fibonacci.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation -//This works by using both recursion and dynamic programming. -//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci -//we can use it for 10000th fibonacci etc, if we implement bigintegers. -//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci -//It is a property of fibonacci similar to matrix exponentiation. - -#include -#include -using namespace std; - -const long long MAX = 93; - -long long f[MAX] = {0}; - -long long fib(long long n) -{ - - if (n == 0) - return 0; - if (n == 1 || n == 2) - return (f[n] = 1); - - if (f[n]) - return f[n]; - - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - - f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) - : (2 * fib(k - 1) + fib(k)) * fib(k); - return f[n]; -} - -int main() -{ - //Main Function - for (long long i = 1; i < 93; i++) - { - cout << i << " th fibonacci number is " << fib(i) << "\n"; - } - return 0; -} diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp new file mode 100644 index 000000000..dd3481422 --- /dev/null +++ b/others/fibonacci_fast.cpp @@ -0,0 +1,37 @@ +// An efficient way to calculate nth fibonacci number faster and simpler than +// O(nlogn) method of matrix exponentiation This works by using both recursion +// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be +// stored in a single long long variable, we can only use it till 92nd fibonacci +// we can use it for 10000th fibonacci etc, if we implement bigintegers. +// This algorithm works with the fact that nth fibonacci can easily found if we +// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci +// similar to matrix exponentiation. + +#include +#include +using namespace std; + +const long long MAX = 93; + +long long f[MAX] = {0}; + +long long fib(long long n) { + if (n == 0) return 0; + if (n == 1 || n == 2) return (f[n] = 1); + + if (f[n]) return f[n]; + + long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + + f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) + : (2 * fib(k - 1) + fib(k)) * fib(k); + return f[n]; +} + +int main() { + // Main Function + for (long long i = 1; i < 93; i++) { + cout << i << " th fibonacci number is " << fib(i) << "\n"; + } + return 0; +} diff --git a/others/matrix_exponentiation.cpp b/others/matrix_exponentiation.cpp index 25c411dda..f9b4997e9 100644 --- a/others/matrix_exponentiation.cpp +++ b/others/matrix_exponentiation.cpp @@ -19,6 +19,8 @@ The first element of this matrix is the required result. */ #include +#include + using std::cin; using std::cout; using std::vector; @@ -46,8 +48,7 @@ vector> multiply(vector> A, vector> B) { // computing power of a matrix vector> power(vector> A, ll p) { - if (p == 1) - return A; + if (p == 1) return A; if (p % 2 == 1) { return multiply(A, power(A, p - 1)); } else { @@ -58,14 +59,11 @@ vector> power(vector> A, ll p) { // main function ll ans(ll n) { - if (n == 0) - return 0; - if (n <= k) - return b[n - 1]; + if (n == 0) return 0; + if (n <= k) return b[n - 1]; // F1 vector F1(k + 1); - for (ll i = 1; i <= k; i++) - F1[i] = b[i - 1]; + for (ll i = 1; i <= k; i++) F1[i] = b[i - 1]; // Transpose matrix vector> T(k + 1, vector(k + 1)); diff --git a/others/Paranthesis Matching.cpp b/others/paranthesis_matching.cpp similarity index 100% rename from others/Paranthesis Matching.cpp rename to others/paranthesis_matching.cpp diff --git a/others/pascal_triangle.cpp b/others/pascal_triangle.cpp index 101100018..063a6666a 100644 --- a/others/pascal_triangle.cpp +++ b/others/pascal_triangle.cpp @@ -1,63 +1,53 @@ -#include +#include +#include -using namespace std; - -void show_pascal(int **arr, int n) -{ - //pint Pascal's Triangle - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n + i; ++j) - { - if (arr[i][j] == 0) - cout << " "; - else - cout << arr[i][j]; - } - cout << endl; - } +void show_pascal(int **arr, int n) { + // pint Pascal's Triangle + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n + i; ++j) { + if (arr[i][j] == 0) + std::cout << " "; + else + std::cout << arr[i][j]; + } + std::cout << std::endl; + } } -int **pascal_triangle(int **arr, int n) -{ - for (int i = 0; i < n; ++i) - { - for (int j = n - i - 1; j < n + i; ++j) - { - if (j == n - i - 1 || j == n + i - 1) - arr[i][j] = 1; //The edge of the Pascal triangle goes in 1 - else - arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; - } - } +int **pascal_triangle(int **arr, int n) { + for (int i = 0; i < n; ++i) { + for (int j = n - i - 1; j < n + i; ++j) { + if (j == n - i - 1 || j == n + i - 1) + arr[i][j] = 1; // The edge of the Pascal triangle goes in 1 + else + arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; + } + } - return arr; + return arr; } -int main() -{ - int n = 0; +int main() { + int n = 0; - cout << "Set Pascal's Triangle Height" << endl; - cin >> n; - - //memory allocation (Assign two-dimensional array to store Pascal triangle) - int **arr = new int*[n]; - for (int i = 0; i < n; ++i) - { - arr[i] = new int[2 * n - 1]; - memset(arr[i], 0, sizeof(int)*(2 * n - 1)); - } - - pascal_triangle(arr, n); - show_pascal(arr, n); + std::cout << "Set Pascal's Triangle Height" << std::endl; + std::cin >> n; - //deallocation - for (int i = 0; i < n; ++i) - { - delete[] arr[i]; - } - delete[] arr; + // memory allocation (Assign two-dimensional array to store Pascal triangle) + int **arr = new int *[n]; + for (int i = 0; i < n; ++i) { + arr[i] = new int[2 * n - 1]; + memset(arr[i], 0, sizeof(int) * (2 * n - 1)); + } - return 0; + pascal_triangle(arr, n); + show_pascal(arr, n); + + // deallocation + for (int i = 0; i < n; ++i) { + delete[] arr[i]; + } + delete[] arr; + + return 0; } diff --git a/others/Primality Test.cpp b/others/primality_test.cpp similarity index 100% rename from others/Primality Test.cpp rename to others/primality_test.cpp diff --git a/others/Sparse matrix.cpp b/others/sparse_matrix.cpp similarity index 100% rename from others/Sparse matrix.cpp rename to others/sparse_matrix.cpp diff --git a/others/String Fibonacci.cpp b/others/string_fibonacci.cpp similarity index 100% rename from others/String Fibonacci.cpp rename to others/string_fibonacci.cpp diff --git a/others/Tower of Hanoi.cpp b/others/tower_of_hanoi.cpp similarity index 100% rename from others/Tower of Hanoi.cpp rename to others/tower_of_hanoi.cpp diff --git a/sorting/shell_sort2.cpp b/sorting/shell_sort2.cpp new file mode 100644 index 000000000..1268c7a50 --- /dev/null +++ b/sorting/shell_sort2.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +// for std::swap +#include + +template void show_data(T *arr, size_t LEN) { + size_t i; + + for (i = 0; i < LEN; i++) + std::cout << arr[i] << ", "; + std::cout << std::endl; +} + +template void show_data(T (&arr)[N]) { show_data(arr, N); } + +/** + * Optimized algorithm - takes half the time by utilizing + * Mar + **/ +template void shell_sort(T *arr, size_t LEN) { + const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; + const unsigned int gap_len = 8; + size_t i, j, g; + + for (g = 0; g < gap_len; g++) { + unsigned int gap = gaps[g]; + for (i = gap; i < LEN; i++) { + T tmp = arr[i]; + + for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) + arr[j] = arr[j - gap]; + + arr[j] = tmp; + } + } +} + +template void shell_sort(T (&arr)[N]) { + shell_sort(arr, N); +} + +/** + * function to compare sorting using cstdlib's qsort + **/ +int compare(const void *a, const void *b) { + int arg1 = *static_cast(a); + int arg2 = *static_cast(b); + + if (arg1 < arg2) + return -1; + if (arg1 > arg2) + return 1; + return 0; + + // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut + // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) +} + +int main(int argc, char *argv[]) { + int i, NUM_DATA; + + if (argc == 2) + NUM_DATA = atoi(argv[1]); + else + NUM_DATA = 200; + + // int array = new int[NUM_DATA]; + int *data = new int[NUM_DATA]; + int *data2 = new int[NUM_DATA]; + // int array2 = new int[NUM_DATA]; + int range = 1800; + + std::srand(time(NULL)); + for (i = 0; i < NUM_DATA; i++) + data[i] = data2[i] = (std::rand() % range) - (range >> 1); + + std::cout << "Unsorted original data: " << std::endl; + show_data(data, NUM_DATA); + std::clock_t start = std::clock(); + shell_sort(data, NUM_DATA); + std::clock_t end = std::clock(); + + std::cout << std::endl + << "Data Sorted using custom implementation: " << std::endl; + show_data(data, NUM_DATA); + + double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + start = std::clock(); + qsort(data2, NUM_DATA, sizeof(data2[0]), compare); + end = std::clock(); + std::cout << "Data Sorted using cstdlib qsort: " << std::endl; + show_data(data2, NUM_DATA); + + elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + free(data); + free(data2); + return 0; +}