Long time no see

This commit is contained in:
2026-02-21 10:47:00 +07:00
parent 0d54fe176e
commit b7df98a55c
198 changed files with 3249 additions and 1 deletions
+126
View File
@@ -0,0 +1,126 @@
#include "head.h"
#include <time.h>
double get_time();
void fill_rand(uint32_t *arr, int n);
int main(int argc,char **argv) {
srand(get_time());
if(argc>1){
switch(argv[1][0]){
case '1': // Exp what sort is faster
run_exp();
break;
case '2': // Test Sorts
{
int n;
if(!(argv[1][1]>='a'&&argv[1][1]<='z')) return 0;
printf("Write n:");
scanf("%d",&n);
while(getchar()>'\n');
uint32_t *arr = (uint32_t*)malloc(sizeof(uint32_t) * n);
fill_rand(arr, n);
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
putchar('\n');
switch(argv[1][1]){
case 'r': // Radix
sort_radix(arr,n);
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
break;
case 'h': // Heap
sort_heap(arr,n);
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
break;
case 'b': //Bubble
sort_bubble(arr,n);
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
break;
case 'm': //Merge
sort_merge(arr,0,n-1);
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
break;
default:
break;
}
break;
default:
return 0;
}
case '3':
{
printf("\n N - %d",(18-1)%24+1);
break;
}
}
}
else{
run_exp();
}
return 0;
}
void run_exp(void) {
FILE *f = fopen("table.dat", "w");
if (!f) return;
fprintf(f, "# N Radix Heap Bubble \n");
int n = 50000,end=MILLION,step = n;
for (;n<end;n+=step){
uint32_t *arr = (uint32_t*)malloc(sizeof(uint32_t) * n);
double tRadTotal = 0;
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
fill_rand(arr, n);
double t = get_time();
sort_radix(arr,n);
tRadTotal += (get_time() - t);
}
double tRadRes = tRadTotal / AVG_TIME_LOOPS;
double tHeapTotal = 0;
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
fill_rand(arr, n);
double t = get_time();
sort_heap(arr, n);
tHeapTotal += (get_time() - t);
}
double tHeapRes = tHeapTotal / AVG_TIME_LOOPS;
double tBubblTotal = 0;
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
fill_rand(arr, n);
double t = get_time();
sort_bubble(arr,n);
tBubblTotal += (get_time() - t);
}
double tBubblRes = tBubblTotal / AVG_TIME_LOOPS;
fprintf(f, "%d %f %f %f\n", n, tRadRes, tHeapRes, tBubblRes);
free(arr);
printf("Sort: %d/%d \r", n, end);
fflush(stdout);
}
fclose(f);
printf("\nTable 1 done.\n");
return;
}
int get_rand(int min, int max) {
return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
}
void fill_rand(uint32_t *arr, int n) {
for (int i = 0; i < n; i++) arr[i] = get_rand(0, AVG_RAND_MAX);
}
double get_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}