diff --git a/DIRECTORY.md b/DIRECTORY.md index 96143d09..5c17e8d1 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) diff --git a/hash/test_program.c b/hash/test_program.c index b799135b..3f741bc6 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -8,15 +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("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 +} 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)); 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 diff --git a/sorting/shell_Sort.c b/sorting/shell_Sort.c index c332647d..8ebf6fcc 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)) \ @@ -37,10 +37,38 @@ 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; int array[ELEMENT_NR]; + int array2[ELEMENT_NR]; int range = 500; int size; clock_t start, end; @@ -48,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); @@ -62,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; }