二分插入排序

#include<iostream>

using namespace std;


int findIndex(char * v,char key,int index)
{
    int m,left,right;
    left=0;
    right=index;
    while(1)    
    { 
       m=(right-left)/2;
       if(key<*(v+left))
       {
          return left;
       } 
       else if(key>*(v+right))
       {
          return right;
       }
       else
       {
         if(m==0)
         {
           return left+1; 
         }
         if(key==*(v+left+m))
         {
            return left+m;
         }
         else if(key > *(v+left+m) )
         {
            left=left+m;
         }
         else
         {
            right=left+m;
         }
       } 
    }
    return -1;
}
int shortInsertTest2(int c, char * v)
{
   int p,i,j;
   char temp;
   if(c<2)
   {
     return 0;
   }
   for(p=1;p<c;p++)
   {
         int i=findIndex(v,*(v+p),p);
         temp=*(v+p);
         for(j=p;j>i;j--)
         {
           *(v+j)=*(v+j-1);
         }
         *(v+i)=temp; 
   } 
}
原文地址:https://www.cnblogs.com/heling/p/6006345.html