Skip to content

Instantly share code, notes, and snippets.

@lavantien
Created September 30, 2018 08:31
Show Gist options
  • Save lavantien/9a7f41daaf04bd88dbb6abc4a8648cc5 to your computer and use it in GitHub Desktop.
Save lavantien/9a7f41daaf04bd88dbb6abc4a8648cc5 to your computer and use it in GitHub Desktop.
Program for choosing break-point (threshold) for Quick Sort
/*
LaVanTien Present:
Quick-Sort Break-Point Benchmark Program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char *FO = "C:\\Dev\\quicksort_choosingbreakpoint_benchmarks_result.txt"; // Path of Result file.
const int MAX = 1000000; // Maximum array's elements.
const int RT = 200; // Total number of run times.
const int MS = 1000; // Maximum value of stopqs.
int stopqs = 1; // Break-Point to stop Quick Sort recursion and start using Insertion Sort.
void insertionsort(int *a, const int, const int); // Insertion Sort, takes an integer array, left index, right index: sort from left to right.
void swap(int *, int *); // Swap to integer
void quicksort(int *, const int, const int); // Quick Sort, takes an integer array, left index, right index: sort from left to right.
void sort(int *, const int); // A general sort function to generate Random Seed and trigger Quick Sort.
int main(void) {
int *v = malloc(MAX * sizeof *v); // Dynamic allocating an integer array with MAX members.
double mostavg = MAX, mostbest = MAX, mostworst = 0, bestworst = MAX;
int posma = 0, posmb = 0, posmw = 0, posbw = 0;
FILE *f = fopen(FO, "w");
if (!f) {
perror("Error: ");
return EXIT_FAILURE;
}
puts("Running..");
for ( ; stopqs <= MS; stopqs++) {
double t, s = 0, worst = 0, best = MAX, avg;
srand(time(NULL));
for (int u = 0; u < RT; u++) {
for (int i = 0; i < MAX; i++)
v[i] = rand();
clock_t start = clock();
sort(v, MAX);
clock_t end = clock();
t = (double)(end - start) / CLOCKS_PER_SEC; // Calculate run-time of sort function
s += t;
if (t > worst)
worst = t;
if (t < best)
best = t;
}
avg = s / RT;
fprintf(f, "stopqs: %d; MAX: %d; RT: %d\navg: %.3lf\nbest: %.3lf\nworst: %.3lf\n\n", stopqs, MAX, RT, avg, best, worst);
if (avg < mostavg) {
mostavg = avg;
posma = stopqs;
}
if (best < mostbest) {
mostbest = best;
posmb = stopqs;
}
if (worst > mostworst) {
mostworst = worst;
posmw = stopqs;
}
if (worst < bestworst) {
bestworst = worst;
posbw = stopqs;
}
}
fprintf(f, "\nmostavg: %.3lf at %d\nmostbest: %.3lf at %d\nmostworst: %.3lf at %d\n", mostavg, posma, mostbest, posmb, mostworst, posmw);
fprintf(f, "bestworst: %.3lf at %d\n", bestworst, posbw);
free(v);
fclose(f);
printf("Done! Check the results in file '%s'\n", FO);
return 0;
}
void insertionsort(int *v, const int l, const int r) {
for (int i = l + 1; i <= r; i++) {
int x = v[i];
int j = i - 1;
while (v[j] > x && j >= 0) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = x;
}
}
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int *v, const int l, const int r) {
if (r - l < stopqs) {
insertionsort(v, l, r);
return;
}
int p = v[l + rand() % (r - l + 1)], i = l, j = r;
do {
while (v[i] < p)
i++;
while (v[j] > p)
j--;
if (i <= j) {
if (i < j)
swap(&v[i], &v[j]);
i++;
j--;
}
} while (i <= j);
quicksort(v, l, j);
quicksort(v, i, r);
}
void sort(int *v, const int n) {
srand(time(NULL));
quicksort(v, 0, n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment