Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple generic sorting interface, an insertion sort implemented through that interface, and some test code that demonstrates its use:</p> <pre><code>#include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct sort_interface { // number of elements size_t nmemb; // passed through to 'arg' of compare() and swap() void *arg; // compares elements at 'i' and 'j' int (*compare)(void *arg, size_t i, size_t j); // swaps elements at 'i' and 'j' void (*swap)(void *arg, size_t i, size_t j); }; static void insertion_sort (struct sort_interface iface) { for (size_t i = 0; i &lt; iface.nmemb; i++) { size_t j = i; while (j &gt; 0) { if (iface.compare(iface.arg, j - 1, j) &lt;= 0) { break; } iface.swap(iface.arg, j - 1, j); j--; } } } static int func_comparator (void *arg, size_t i, size_t j) { int *arr = arg; if (arr[i] &lt; arr[j]) { return -1; } if (arr[i] &gt; arr[j]) { return 1; } return 0; } static void func_swap (void *arg, size_t i, size_t j) { int *arr = arg; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } int main (int argc, char *argv[]) { int arr[] = {7, 6, 8, 2, 9, 1, 156, 1, 62, 1671, 15}; size_t count = sizeof(arr) / sizeof(arr[0]); struct sort_interface iface; iface.nmemb = count; iface.arg = arr; iface.compare = func_comparator; iface.swap = func_swap; insertion_sort(iface); for (size_t i = 0; i &lt; count; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } </code></pre> <p>You might also want to take a look at the <a href="http://linux.die.net/man/3/qsort" rel="nofollow">qsort() function</a> of the C standard library, which too uses a function pointer comparator, but is somewhat limited to compared to the above. In particular, it assumes you're sorting a continuous array, and if you have pointers to elements or their members, those will be broken (but the above interface allows you to fix pointers in swap()).</p> <p>Here's an example for how to use the qsort() interface, and also an insertion sort implementation that uses the same interface as qsort():</p> <pre><code>#include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; static void insertion_sort (void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) { char temp[size]; for (size_t i = 0; i &lt; nmemb; i++) { size_t j = i; while (j &gt; 0) { char *x = (char *)base + (j - 1) * size; char *y = (char *)base + j * size; if (compar(x, y) &lt;= 0) { break; } memcpy(temp, x, size); memcpy(x, y, size); memcpy(y, temp, size); j--; } } } static int int_comparator (const void *ve1, const void *ve2) { const int *e1 = ve1; const int *e2 = ve2; if (*e1 &lt; *e2) { return -1; } if (*e1 &gt; *e2) { return 1; } return 0; } int main (int argc, char *argv[]) { int arr[] = {7, 6, 8, 2, 9, 1, 156, 1, 62, 1671, 15}; size_t count = sizeof(arr) / sizeof(arr[0]); qsort(arr, count, sizeof(arr[0]), int_comparator); // or insertion_sort() for (size_t i = 0; i &lt; count; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload