请教下这段代码的编写思路
目标是:在字符串数组中插入指定的字符串并有序输出代码:#include <stdio.h>
#include <string.h>
void sort(char** beg, char** end)
{
char** p, *t;
for(; beg != end; ++beg) {
for(p = end - 1; p != beg; --p)
if(strcmp(*p, p[-1]) < 0) {
t = *p;
*p = p[-1];
p[-1] = t;
}
}
}
char** binary_search(char** beg, char** end, char* val)
{
char** mid;
while(beg < end) {
mid = beg + ((end - beg) >> 1);
if(strcmp(*mid, val) == 0)
return mid;
else if(strcmp(*mid, val) < 0)
beg = mid + 1;
else
end = mid;
}
return beg;
}
int main()
{
char* a[5] = {"this", "is", "an", "example"};
char b[32], **p, **q;
sort(a, a + 4);
for(p = a; p != a + 4; ++p)
printf("%s\n", *p);
gets(b);
p = binary_search(a, a + 4, b);
for(q = a + 4; q != p; --q)
*q = q[-1];
*p = b;
for(p = a; p != a + 5; ++p)
printf("%s\n", *p);
}
这段代码的排序部分看明白了 就是插入字符串那段看不明白他的插入思路,还请看明白的指教下;