mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
Merge commit '0538404f8652bb8f80d362eab2bf988a3eb545bd'
* commit '0538404f8652bb8f80d362eab2bf988a3eb545bd': added link to compile and run code online Gist to run and test the Durand-Kerner Algorithm online and view the roots convergence better formatting of root values updating DIRECTORY.md another shell-sort implementation updating DIRECTORY.md remove timing calculation compute real eigen values of a square matrix using shit and deflate QR decomposition algorithm QR decomposition of any matrix with real elements Durand Kramer method for roots of any polynomial # Conflicts: # DIRECTORY.md # numerical_methods/durand_kerner_roots.c # numerical_methods/qr_decomposition.c # numerical_methods/qr_eigen_values.c
This commit is contained in:
commit
eea5188389
@ -1,81 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "function_timer.h"
|
||||
|
||||
#define ELEMENT_NR 20000
|
||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
void show_data(int arr[], int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%3d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimized algorithm - takes half the time as other
|
||||
**/
|
||||
void shell_sort(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 range = 500;
|
||||
int size;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
array[i] = (rand() % range) - range;
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
function_timer *timer = new_timer();
|
||||
|
||||
show_data(array, size);
|
||||
start_timer(timer);
|
||||
shell_sort(array, size);
|
||||
time_spent = end_timer(timer);
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
printf("Time spent sorting: %.4g s\n", time_spent);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#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)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%3d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimized algorithm - takes half the time as other
|
||||
**/
|
||||
void shell_sort(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 range = 500;
|
||||
int size;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
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();
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user