C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
benchmark.c File Reference

Empirical comparison of all five sort algorithms. More...

#include "sorts.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

Go to the source code of this file.

Classes

struct  sort_entry_t
 

Typedefs

typedef void(* sort_fn) (int *, size_t)
 

Functions

static void fill_random (int *out, size_t n, unsigned seed)
 
int main (void)
 
static double time_sort_ms (sort_fn fn, int *arr, size_t n)
 

Variables

static const size_t N_SIZES = sizeof(SIZES) / sizeof(SIZES[0])
 
static const size_t N_SORTS = sizeof(SORTS) / sizeof(SORTS[0])
 
static const size_t SIZES [] = {100, 1000, 10000}
 
static const sort_entry_t SORTS []
 

Detailed Description

Empirical comparison of all five sort algorithms.

For each of {100, 1000, 10000} random integers, run each sort against the same input and report wall-clock time in milliseconds. Output is a Markdown table so it can be pasted straight into READMEs.

Definition in file benchmark.c.

Typedef Documentation

◆ sort_fn

typedef void(* sort_fn) (int *, size_t)

Definition at line 16 of file benchmark.c.

Function Documentation

◆ fill_random()

static void fill_random ( int *  out,
size_t  n,
unsigned  seed 
)
static

Fills out with n deterministically-shuffled integers in [0, n).

Definition at line 36 of file benchmark.c.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 50 of file benchmark.c.

References fill_random(), N_SIZES, N_SORTS, SIZES, SORTS, and time_sort_ms().

◆ time_sort_ms()

static double time_sort_ms ( sort_fn  fn,
int *  arr,
size_t  n 
)
static

Definition at line 41 of file benchmark.c.

Referenced by main().

Variable Documentation

◆ N_SIZES

const size_t N_SIZES = sizeof(SIZES) / sizeof(SIZES[0])
static

Definition at line 33 of file benchmark.c.

Referenced by main().

◆ N_SORTS

const size_t N_SORTS = sizeof(SORTS) / sizeof(SORTS[0])
static

Definition at line 30 of file benchmark.c.

Referenced by main().

◆ SIZES

const size_t SIZES[] = {100, 1000, 10000}
static

Definition at line 32 of file benchmark.c.

Referenced by main().

◆ SORTS

const sort_entry_t SORTS[]
static
Initial value:
= {
{"Selection", selection_sort_ints},
{"Bubble", bubble_sort_ints},
{"Insertion", insertion_sort_ints},
{"Quicksort", quicksort_ints},
{"Merge", merge_sort_ints},
}
void bubble_sort_ints(int *arr, size_t n)
Definition bubble_sort.c:30
void insertion_sort_ints(int *arr, size_t n)
void merge_sort_ints(int *arr, size_t n)
Definition merge_sort.c:66
void quicksort_ints(int *arr, size_t n)
Definition quicksort.c:76
void selection_sort_ints(int *arr, size_t n)

Definition at line 23 of file benchmark.c.

Referenced by main().