[ create a new paste ] login | about

Link: http://codepad.org/515HiGTs    [ raw code | output | fork ]

johannes - C, pasted on Jan 10:
void insertion_sort(int *n, size_t s)
{
   int *i, *j, *top, t;
   i = n;
   top = n + s - 1;
   while (1) {
      while (*(i + 1) > *i && i < top)
         ++i;
      if (i == top)
         break;
      j = i++;
      t = *i;
      while (t < *j && j >= n) {
         *(j + 1) = *j;
         *(j--) = t;
      }
   }
}

int main(void)
{
   unsigned int i, s;
   int n[] = {1, 5, 8, 5, 6, 2, 7, 3, 2, 1, 7};
   s = sizeof(n) / sizeof(int);

   insertion_sort(n, s);

   for (i = 0; i < s; i++) {
       printf("%i\n", n[i]);
   }

   return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
1
1
2
2
3
5
5
6
7
7
8


Create a new paste based on this one


Comments: