Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Created June 13, 2023 20:42
Show Gist options
  • Save maxgoren/2294ad9b00e54ceeaa0aac91dbb8a71b to your computer and use it in GitHub Desktop.
Save maxgoren/2294ad9b00e54ceeaa0aac91dbb8a71b to your computer and use it in GitHub Desktop.
its always something
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sorterQ(unsigned long a, int * b) {
unsigned long c = a - 1;
unsigned long d = 0;
unsigned long e;
int f;
while (a != 0) {
d = a;
e = a;
f = b[a];
while (d != 0) {
d--;
if (b[d] > f) {
f = b[d];
e = d;
}
}
b[e] = b[a];
b[a] = f;
a--;
}
return;
}
void insertionsort(int a[], int n) {
for (int i = 1; i < n; i++) {
int j = i; int v = a[j];
while (j > 0 && a[j - 1] > v) {
a[j] = a[j - 1];
j--;
}
a[j] = v;
}
}
void checksort(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
if (a[i+1] < a[i]) {
printf("Sort failed.\n");
return;
}
}
printf("Sorted.\n");
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
int *a = malloc(sizeof(int) * n);
int *b = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
int p = rand() % RAND_MAX;
a[i] = p;
b[i] = p;
}
printf("Sorting: %d items.\n", n);
clock_t start, end;
double time;
start = clock();
insertionsort(b, n);
end = clock();
time = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("insertionsort finished in: %f s\n", time);
checksort(b, n);
start = clock();
sorterQ(n, a);
end = clock();
time = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Redditer sort finished in: %f s\n", time);
checksort(a, n);
return 0;
}
/*
max@MaxGorenLaptop:/mnt/c/Users/mgoren/langtonsJava$ ./stupider 1000; ./stupider 10000 ;./stupider 100000
Sorting: 1000 items.
insertionsort finished in: 0.000595 s
Sorted.
Redditer sort finished in: 0.001022 s
Sorted.
Sorting: 10000 items.
insertionsort finished in: 0.058005 s
Sorted.
Redditer sort finished in: 0.088613 s
Sorted.
Sorting: 100000 items.
insertionsort finished in: 5.183086 s
Sorted.
Redditer sort finished in: 8.904857 s
Sorted.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment