Skip to content

Instantly share code, notes, and snippets.

@liuliu
Last active December 11, 2016 04:23
Show Gist options
  • Save liuliu/45052a277f77f7b68c763a94167c053a to your computer and use it in GitHub Desktop.
Save liuliu/45052a277f77f7b68c763a94167c053a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
static inline int _ccv_nnc_try_sort_and_insert(int* md, const int ins, const int c)
{
if (!c)
{
md[0] = ins;
return 1;
}
int ll = 0, uu = c - 1;
int mm;
do {
mm = ll + ((uu - ll) >> 1);
if (ins == md[mm])
return 0;
else if (ins < md[mm])
uu = mm - 1;
else if (ins > md[mm])
ll = mm + 1;
} while (ll <= uu);
if (ll < c)
memmove(md + ll + 1, md + ll, sizeof(int) * (c - ll));
md[ll] = ins;
return 1;
}
void main()
{
int md[1024];
int c = 0;
c += _ccv_nnc_try_sort_and_insert(md, 3, c);
c += _ccv_nnc_try_sort_and_insert(md, 3, c);
c += _ccv_nnc_try_sort_and_insert(md, 0, c);
c += _ccv_nnc_try_sort_and_insert(md, 10, c);
c += _ccv_nnc_try_sort_and_insert(md, 2, c);
c += _ccv_nnc_try_sort_and_insert(md, 1, c);
c += _ccv_nnc_try_sort_and_insert(md, 0, c);
c += _ccv_nnc_try_sort_and_insert(md, 3, c);
c += _ccv_nnc_try_sort_and_insert(md, 2, c);
c += _ccv_nnc_try_sort_and_insert(md, 1028, c);
c += _ccv_nnc_try_sort_and_insert(md, 1, c);
c += _ccv_nnc_try_sort_and_insert(md, 0, c);
c += _ccv_nnc_try_sort_and_insert(md, 3, c);
c += _ccv_nnc_try_sort_and_insert(md, 8, c);
c += _ccv_nnc_try_sort_and_insert(md, 8, c);
c += _ccv_nnc_try_sort_and_insert(md, 7, c);
c += _ccv_nnc_try_sort_and_insert(md, 1, c);
printf("c: %d\n", c);
int i;
for (i = 0; i < c; i++)
printf("%d\n", md[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment