mirror of https://github.com/TheAlgorithms/C
Merge branch 'sorting/shell_sort' into project_euler/master2
* sorting/shell_sort: add stats for algo 2 and use milliseconds create a duplicate array added new shell-sort algorithm updating DIRECTORY.md +a much faster fibonacci computation algorithm print hashes in HEX add commandline option to FibonacciDP.c print hashes in HEX add commandline option to FibonacciDP.c
This commit is contained in:
commit
5d6f7b3876
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
||||
|
|
|
@ -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<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<locale.h>
|
||||
|
||||
/**
|
||||
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;
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue