mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
added stdint.h for integer length typedefs
This commit is contained in:
parent
b12e387bce
commit
855c9124b8
@ -1,20 +1,20 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "hash.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = "hello";
|
||||
|
||||
/* actual tests */
|
||||
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;
|
||||
}
|
||||
/*
|
||||
author: Christian Bender
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "hash.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = "hello";
|
||||
|
||||
/* actual tests */
|
||||
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;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
|
@ -1,111 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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)) \
|
||||
\n--> Average Case: depends on gap sequence \
|
||||
\n--> Worst Case: O(n)";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void shellSort(int array[], int len)
|
||||
{
|
||||
int i, j, gap;
|
||||
|
||||
for (gap = len / 2; gap > 0; gap = gap / 2)
|
||||
for (i = gap; i < len; i++)
|
||||
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
||||
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;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
{
|
||||
array[i] = rand() % range + 1;
|
||||
array2[i] = array[i];
|
||||
}
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
show_data(array, size);
|
||||
start = clock();
|
||||
shellSort(array, size);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
printf("%s\n", notation);
|
||||
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;
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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)) \
|
||||
\n--> Average Case: depends on gap sequence \
|
||||
\n--> Worst Case: O(n)";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void shellSort(int array[], int len)
|
||||
{
|
||||
int i, j, gap;
|
||||
|
||||
for (gap = len / 2; gap > 0; gap = gap / 2)
|
||||
for (i = gap; i < len; i++)
|
||||
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
||||
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;
|
||||
double time_spent;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < ELEMENT_NR; i++)
|
||||
{
|
||||
array[i] = rand() % range + 1;
|
||||
array2[i] = array[i];
|
||||
}
|
||||
|
||||
size = ARRAY_LEN(array);
|
||||
|
||||
show_data(array, size);
|
||||
start = clock();
|
||||
shellSort(array, size);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Data Sorted\n");
|
||||
show_data(array, size);
|
||||
|
||||
printf("%s\n", notation);
|
||||
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
Block a user