void bubblesort (void * array, unsigned item_count, unsigned item_size, int (* comparison) (void *, void *)) { // This algorithm is for benchmarking only, not for production use. Bubbles are slow at sorting! if (!(array && item_size && item_count && comparison)) return; // no point in sorting invalid or empty stuff if (item_count == 1) return; // or sorting just one value char * buf = malloc(item_size); // for temporary stuff char * array_ptr = array; unsigned pass, item; for (pass = item_count - 1; pass; pass --) for (item = 0; item < pass; item ++) if (comparison(array_ptr + item_size * item, array_ptr + item_size * (item + 1)) > 0) { // swap the items if they are inverted memcpy(buf, array_ptr + item_size * item, item_size); memcpy(array_ptr + item_size * item, array_ptr + item_size * (item + 1), item_size); memcpy(array_ptr + item_size * (item + 1), buf, item_size); } // array is now sorted free(buf); }