mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
added authorship to docs
This commit is contained in:
parent
acbfd4d962
commit
f9d506fdb0
@ -3,6 +3,8 @@
|
||||
* \brief [Adaptive Linear Neuron
|
||||
* (ADALINE)](https://en.wikipedia.org/wiki/ADALINE) implementation
|
||||
*
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* <img
|
||||
* src="https://upload.wikimedia.org/wikipedia/commons/b/be/Adaline_flow_chart.gif"
|
||||
* width="200px">
|
||||
|
@ -3,6 +3,8 @@
|
||||
* \brief [Kohonen self organizing
|
||||
* map](https://en.wikipedia.org/wiki/Self-organizing_map) (data tracing)
|
||||
*
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* This example implements a powerful self organizing map algorithm.
|
||||
* The algorithm creates a connected network of weights that closely
|
||||
* follows the given data points. This this creates a chain of nodes that
|
||||
|
@ -1,11 +1,18 @@
|
||||
/*
|
||||
collatz conjecture: a series for a number n in which if n even then the next
|
||||
number is n/2 ,but if n is odd then the next number is 3n+1. this series
|
||||
continues till it reaches 1*/
|
||||
/**
|
||||
* \file
|
||||
*
|
||||
* \brief Implementation of [Collatz'
|
||||
* conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture)
|
||||
*
|
||||
* Collatz conjecture: a series for a number \f$n\f$ in which if \f$n\f$ even
|
||||
* then the next number is \f$\frac{n}{2}\f$ ,but if n is odd then the next
|
||||
* number is \f$3n+1\f$. This series continues till \f$n\f$ reaches 1
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned long long n, curr_no, num_steps = 0;
|
||||
|
@ -2,6 +2,7 @@
|
||||
* @file
|
||||
* \brief Compute factorial of arbitrarily large numbers by
|
||||
* storing individual digits in a byte.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,6 +1,6 @@
|
||||
/**
|
||||
@file
|
||||
@author Krishna Vedala
|
||||
@author [Krishna Vedala](https://github.com/kvedala)
|
||||
@date 2 October, 2019
|
||||
@brief Compute \f$m^{mth}\f$ Fibonacci number using the formulae:
|
||||
\f{eqnarray*}{
|
||||
|
@ -4,6 +4,8 @@
|
||||
* [Durand Kerner
|
||||
* algorithm](https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method)
|
||||
*
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* Test the algorithm online:
|
||||
* https://gist.github.com/kvedala/27f1b0b6502af935f6917673ec43bcd7
|
||||
*
|
||||
@ -249,4 +251,4 @@ end:
|
||||
free(s0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
* @file
|
||||
* \brief Find approximate solution for \f$f(x) = 0\f$ using
|
||||
* Newton-Raphson interpolation algorithm.
|
||||
*
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
**/
|
||||
|
||||
#include <complex.h> /* requires minimum of C99 */
|
||||
@ -71,4 +73,4 @@ int main(int argc, char **argv)
|
||||
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* \brief Library functions to compute [QR
|
||||
* decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
|
||||
* matrix.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
|
||||
#ifndef QR_DECOMPOSE_H
|
||||
|
@ -3,6 +3,7 @@
|
||||
* \brief Program to compute the [QR
|
||||
* decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
|
||||
* matrix.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
|
||||
#include "qr_decompose.h"
|
||||
@ -76,4 +77,4 @@ int main(void)
|
||||
free(R);
|
||||
free(Q);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
* \brief Compute real eigen values and eigen vectors of a symmetric matrix
|
||||
* using [QR decomposition](https://en.wikipedia.org/wiki/QR_decomposition)
|
||||
* method.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include "qr_decompose.h"
|
||||
#include <math.h>
|
||||
@ -170,4 +171,4 @@ int main(int argc, char **argv)
|
||||
free(Q);
|
||||
free(eigen_vals);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief Compute statistics for data entered in rreal-time
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* This algorithm is really beneficial to compute statistics on data read in
|
||||
* realtime. For example, devices reading biometrics data. The algorithm is
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 10](https://projecteuler.net/problem=10) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 10](https://projecteuler.net/problem=10) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@ -52,4 +53,4 @@ int main(int argc, char *argv[])
|
||||
printf("%ld: %lld\n", n, sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 12](https://projecteuler.net/problem=12) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 13](https://projecteuler.net/problem=13) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 14](https://projecteuler.net/problem=14) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* Since the computational values for each iteration step are independent,
|
||||
* we can compute them in parallel. However, the maximum values should be
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 15](https://projecteuler.net/problem=15) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 16](https://projecteuler.net/problem=16) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 19](https://projecteuler.net/problem=19) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 20](https://projecteuler.net/problem=20) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* Implementation uses a custom `big_int` structure that can store arbitrarilty
|
||||
* large integer numbers.
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 21](https://projecteuler.net/problem=21) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 22](https://projecteuler.net/problem=22) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 23](https://projecteuler.net/problem=23) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -2,6 +2,7 @@
|
||||
* \file
|
||||
* \brief [Problem 23](https://projecteuler.net/problem=23) solution -
|
||||
* optimization using look-up array
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
* Optimization applied - compute & store abundant numbers once
|
||||
* into a look-up array.
|
||||
|
@ -2,6 +2,7 @@
|
||||
* \file
|
||||
* \brief [Problem 25](https://projecteuler.net/problem=25) solution implemented
|
||||
* using arbitrarily large numbers represented as arrays
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 26](https://projecteuler.net/problem=26) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 401](https://projecteuler.net/problem=401) solution
|
||||
*
|
||||
* \brief [Problem 401](https://projecteuler.net/problem=401) solution -
|
||||
* Sum of squares of divisors
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@ -144,4 +144,4 @@ int main(int argc, char **argv)
|
||||
printf("Time taken: %.4gms\n", dtime * 1e3 / CLOCKS_PER_SEC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 8](https://projecteuler.net/problem=8) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 8](https://projecteuler.net/problem=8) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -2,6 +2,7 @@
|
||||
* \file
|
||||
* \brief [Problem 9](https://projecteuler.net/problem=9) solution - A naive
|
||||
* implementation
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
@ -22,4 +23,4 @@ int main(void)
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 9](https://projecteuler.net/problem=9) solution
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*
|
||||
Problem Statement:
|
||||
A Pythagorean triplet is a set of three natural numbers, \f$a < b < c\f$,
|
||||
|
@ -1,20 +1,25 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Shell sort algorithm](https://en.wikipedia.org/wiki/Shell_sort)
|
||||
* implementation.
|
||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define ELEMENT_NR 20000
|
||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
void show_data(int arr[], int len)
|
||||
/** Helper function to print array values */
|
||||
void show_data(int *arr, long len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
for (long i = 0; i < len; i++)
|
||||
printf("%3d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void swap(int *a, int *b)
|
||||
/** Function to swap values of two integers */
|
||||
inline void swap(int *a, int *b)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
@ -26,17 +31,17 @@ void swap(int *a, int *b)
|
||||
/**
|
||||
* Optimized algorithm - takes half the time as other
|
||||
**/
|
||||
void shell_sort(int array[], int LEN)
|
||||
void shell_sort(int *array, long LEN)
|
||||
{
|
||||
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||
const int gap_len = 8;
|
||||
int i, j, g;
|
||||
long i, j, g;
|
||||
|
||||
for (g = 0; g < gap_len; g++)
|
||||
{
|
||||
{ // for each gap
|
||||
int gap = gaps[g];
|
||||
for (i = gap; i < LEN; i++)
|
||||
{
|
||||
{ // from gap position to the end
|
||||
int tmp = array[i];
|
||||
|
||||
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
|
||||
@ -50,27 +55,32 @@ void shell_sort(int array[], int LEN)
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int array[ELEMENT_NR];
|
||||
int range = 500;
|
||||
int size;
|
||||
long size = 500;
|
||||
if (argc == 2)
|
||||
size = atol(argv[1]);
|
||||
else if (argc > 2)
|
||||
fprintf(stderr, "Usage: ./shell_sort [number of values]\n");
|
||||
|
||||
int *array = (int *)malloc(size * sizeof(int));
|
||||
int range = 500; // range of array values
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
srand(time(NULL)); // initialize random number generator
|
||||
for (i = 0; i < size; i++)
|
||||
// fill array with random integers
|
||||
array[i] = rand() % range + 1;
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
show_data(array, size);
|
||||
clock_t t1 = clock();
|
||||
shell_sort(array, size);
|
||||
clock_t t2 = clock();
|
||||
show_data(array, size); // show array before sorting
|
||||
clock_t t1 = clock(); // start timer
|
||||
shell_sort(array, size); // sort the array
|
||||
clock_t t2 = clock(); // end timer
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
show_data(array, size); // display array after sorting
|
||||
|
||||
printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user