From 959e25ca08f19b8877c119170632774a32d1960e Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sat, 7 Mar 2020 10:40:44 -0500 Subject: [PATCH 1/9] add commandline option to FibonacciDP.c --- misc/FibonacciDP.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/misc/FibonacciDP.c b/misc/FibonacciDP.c index de605f78..52d99db1 100644 --- a/misc/FibonacciDP.c +++ b/misc/FibonacciDP.c @@ -31,12 +31,17 @@ int fib(int n) return f[n]; } -int main(){ +int main(int argc, char *argv[]) +{ int number; //Asks for the number/position of term in Fibonnacci sequence - printf("Enter the value of n(n starts from 0 ): "); - scanf("%d", &number); + if (argc == 2) + number = atoi(argv[1]); + else { + printf("Enter the value of n(n starts from 0 ): "); + scanf("%d", &number); + } printf("The nth term is : %d \n", fib(number)); From 83bfb72fcf0f7140085a08b29b502fc46930df29 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sat, 7 Mar 2020 10:45:24 -0500 Subject: [PATCH 2/9] print hashes in HEX --- hash/test_program.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hash/test_program.c b/hash/test_program.c index f9dcd299..3f741bc6 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -8,13 +8,13 @@ int main(void) { - char s[] = "name"; + char s[] = "hello"; /* actual tests */ - printf("sdbm: %s --> %lld\n", s, sdbm(s)); - printf("djb2: %s --> %lld\n", s, djb2(s)); - printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */ - printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */ + printf("sdbm: %s --> %llX\n", s, sdbm(s)); + printf("djb2: %s --> %llX\n", s, djb2(s)); + printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */ + printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */ return 0; -} \ No newline at end of file +} From fce50a9d7c6a6a35e6b06b9e77a4b43f0dec3085 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sat, 7 Mar 2020 10:40:44 -0500 Subject: [PATCH 3/9] add commandline option to FibonacciDP.c --- misc/Fibonacci_DP.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/misc/Fibonacci_DP.c b/misc/Fibonacci_DP.c index de605f78..52d99db1 100644 --- a/misc/Fibonacci_DP.c +++ b/misc/Fibonacci_DP.c @@ -31,12 +31,17 @@ int fib(int n) return f[n]; } -int main(){ +int main(int argc, char *argv[]) +{ int number; //Asks for the number/position of term in Fibonnacci sequence - printf("Enter the value of n(n starts from 0 ): "); - scanf("%d", &number); + if (argc == 2) + number = atoi(argv[1]); + else { + printf("Enter the value of n(n starts from 0 ): "); + scanf("%d", &number); + } printf("The nth term is : %d \n", fib(number)); From fba36e3b15b24480ab3c30e6d850c73c36ca3b3d Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sat, 7 Mar 2020 10:45:24 -0500 Subject: [PATCH 4/9] print hashes in HEX --- hash/test_program.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/hash/test_program.c b/hash/test_program.c index b799135b..64ccba19 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -11,12 +11,10 @@ int main(void) char s[] = "name"; /* actual tests */ - printf("sdbm: %s --> %lld\n", s, sdbm(s)); - printf("djb2: %s --> %lld\n", s, djb2(s)); - printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */ - printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */ - printf("crc32: %s --> %i\n", s, crc32(s)); - + printf("sdbm: %s --> %llX\n", s, sdbm(s)); + printf("djb2: %s --> %llX\n", s, djb2(s)); + printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */ + printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */ return 0; -} \ No newline at end of file +} From fa73fcb98392a8adb209175616b7cf6de300a0dd Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sat, 7 Mar 2020 10:57:52 -0500 Subject: [PATCH 5/9] +a much faster fibonacci computation algorithm --- misc/Fibonacci_fast.c | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 misc/Fibonacci_fast.c diff --git a/misc/Fibonacci_fast.c b/misc/Fibonacci_fast.c new file mode 100644 index 00000000..becb8c88 --- /dev/null +++ b/misc/Fibonacci_fast.c @@ -0,0 +1,76 @@ +/** + @file Fibonacci_fast.c + @author: Krishna Vedala + @date 2 October, 2019 + @brief Compute \f$m^{mth}\f$ Fibonacci number using the formulae: + \f{eqnarray*}{ + F_{2n-1} &=& F_n^2 + F_{n-1}^2 \\ + F_{2n} &=& F_n\left(2F_{n-1} + F_n\right) + \f} +*/ + +#include +#include +#include + +/** + Returns the \f$n^{th}\f$ and \f$n+1^{th}\f$ Fibonacci number. + The return variables are C & D respectively. + */ +void fib(unsigned long n, unsigned long *C, unsigned long *D) +{ + //Out of Range checking + if(n < 0){ + printf("\nNo Such term !\n"); + exit(0); + } + + unsigned long a, b, c, d; + + if (n == 0) + { + C[0] = 0; + if(D) + D[0] = 1; + return; + } + + fib(n >> 1, &c, &d); /**< Compute F(n/2) */ + + a = c * ((d << 1) - c); + b = c * c + d * d; + if (n % 2 == 0) /**< If n is even */ + { + C[0] = a; + if(D) + D[0] = b; + return; + } + + /**< If n is odd */ + C[0] = b; + if(D) + D[0] = a + b; + return; +} + +int main(int argc, char *argv[]) +{ + unsigned long number, result; + + setlocale(LC_NUMERIC, ""); // format the printf output + + //Asks for the number/position of term in Fibonnacci sequence + if (argc == 2) + number = atoi(argv[1]); + else { + printf("Enter the value of n(n starts from 0 ): "); + scanf("%lu", &number); + } + + fib(number, &result, NULL); + + printf("The nth term is : %'lu \n", result); + + return 0; +} \ No newline at end of file From a3b126aee5756ce5d59ae379edc8a104f1f22446 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 7 Mar 2020 15:58:11 +0000 Subject: [PATCH 6/9] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5ef05421..dcf69654 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -185,6 +185,7 @@ * [Factorial Trailing Zeroes](https://github.com/TheAlgorithms/C/blob/master/misc/factorial_trailing_zeroes.c) * [Fibonacci](https://github.com/TheAlgorithms/C/blob/master/misc/Fibonacci.c) * [Fibonacci Dp](https://github.com/TheAlgorithms/C/blob/master/misc/Fibonacci_DP.c) + * [Fibonacci Fast](https://github.com/TheAlgorithms/C/blob/master/misc/Fibonacci_fast.c) * [Gcd](https://github.com/TheAlgorithms/C/blob/master/misc/GCD.c) * [Is Armstrong](https://github.com/TheAlgorithms/C/blob/master/misc/is_Armstrong.c) * [Large Factorials](https://github.com/TheAlgorithms/C/blob/master/misc/Large_Factorials.c) From c69758760504eeb7fa439c0cd5c91c554714a729 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 1 Apr 2020 23:46:59 -0400 Subject: [PATCH 7/9] added new shell-sort algorithm --- sorting/shell_Sort.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sorting/shell_Sort.c b/sorting/shell_Sort.c index c332647d..d48a1ae7 100644 --- a/sorting/shell_Sort.c +++ b/sorting/shell_Sort.c @@ -37,6 +37,33 @@ void shellSort(int array[], int len) swap(&array[j], &array[j + gap]); } +/** + * Optimized algorithm - takes half the time as other + **/ +void shell_sort2(int array[], int LEN) +{ + const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; + const int gap_len = 8; + int i, j, g; + + for (g = 0; g < gap_len; g++) + { + int gap = gaps[g]; + for (i = gap; i < LEN; i++) + { + int tmp = array[i]; + + for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap) + array[j] = array[j - gap]; + array[j] = tmp; + } + } +#ifdef DEBUG + for (i = 0; i < LEN; i++) + printf("%s\t", data[i]); +#endif +} + int main(int argc, char *argv[]) { int i; From 7ed48fffa5280285363f0a935114be0b00a43618 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 1 Apr 2020 23:48:04 -0400 Subject: [PATCH 8/9] create a duplicate array --- sorting/shell_Sort.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sorting/shell_Sort.c b/sorting/shell_Sort.c index d48a1ae7..9899c855 100644 --- a/sorting/shell_Sort.c +++ b/sorting/shell_Sort.c @@ -2,7 +2,7 @@ #include #include -#define ELEMENT_NR 20 +#define ELEMENT_NR 20000 #define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0])) const char *notation = "Shell Sort Big O Notation:\ \n--> Best Case: O(n log(n)) \ @@ -68,6 +68,7 @@ int main(int argc, char *argv[]) { int i; int array[ELEMENT_NR]; + int array2[ELEMENT_NR]; int range = 500; int size; clock_t start, end; @@ -75,7 +76,10 @@ int main(int argc, char *argv[]) srand(time(NULL)); for (i = 0; i < ELEMENT_NR; i++) + { array[i] = rand() % range + 1; + array2[i] = array[i]; + } size = ARRAY_LEN(array); From b4e2c13a8e22d90677cab56c4a0a72c994005444 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 1 Apr 2020 23:48:42 -0400 Subject: [PATCH 9/9] add stats for algo 2 and use milliseconds --- sorting/shell_Sort.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sorting/shell_Sort.c b/sorting/shell_Sort.c index 9899c855..8ebf6fcc 100644 --- a/sorting/shell_Sort.c +++ b/sorting/shell_Sort.c @@ -93,7 +93,19 @@ int main(int argc, char *argv[]) show_data(array, size); printf("%s\n", notation); - printf("Time spent sorting: %f\n", time_spent); + printf("Time spent sorting: %.4g ms\n", time_spent * 1e3); + + printf("--------------------------\n"); + start = clock(); + shell_sort2(array2, size); + end = clock(); + time_spent = (double)(end - start) / CLOCKS_PER_SEC; + + printf("Data Sorted\n"); + show_data(array2, size); + + printf("%s\n", notation); + printf("Time spent sorting: %.4g ms\n", time_spent * 1e3); return 0; }