插入排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

template <class T>
void
insert(T* array, int pos)
{
  int i = pos-1;
  T value = array[pos];
  while(i>=0 && array[i]>value){
    --i;
  }
  memmove((array+i+2), (array+i+1), sizeof(T)*(pos-i-1));
  array[i+1] = value;
}

int
main()
{
  int array[10] = {10, 2, 3, 40, 5, 6, 7, 8, 9, 10};
  const int arraylen = sizeof(array)/sizeof(int);
  for(int i =1; i< arraylen; ++i){
    insert(array, i);
  }
  for(int i =0; i< arraylen; ++i){
    printf("%d ", array[i]);
  }
  printf("\n");
}

原文地址:https://www.cnblogs.com/zjfdlut/p/2033269.html