# 一步一步写算法

``````int find(int array[], int length, int value)
{
int index = 0;
return index;
}``````

``````static void test1()
{
int array[10] = {0};
assert(FALSE == find(NULL, 10, 10));
assert(FALSE == find(array, 0, 10));
}``````

``````int find(int array[], int length, int value)
{
if(NULL == array || 0 == length)
return FALSE;

int index = 0;
return index;
}``````

``````int find(int array[], int length, int value)
{
if(NULL == array || 0 == length)
return FALSE;

int index = 0;
for(; index < length; index++){
if(value == array[index])
return index;
}

return FALSE;
}``````

``````static void test2()
{
int array[10] = {1, 2};
assert(0 == find(array, 10, 1));
assert(FALSE == find(array, 10, 10));
}``````

``````int find(int array[], int length, int value)
{
if(NULL == array || 0 == length)
return FALSE;

int* start = array;
int* end = array + length;
while(start < end){
if(value == *start)
return ((int)start - (int)array)/(sizeof(int));
start ++;
}

return FALSE;
}``````

``````template
int find(type array[], int length, type value)
{
if(NULL == array || 0 == length)
return FALSE;

type* start = array;
type* end = array + length;
while(start < end){
if(value == *start)
return ((int)start - (int)array)/(sizeof(type));
start ++;
}

return FALSE;
}``````

``````static void test1()
{
int array[10] = {0};
assert(FALSE == find(NULL, 10, 10));
assert(FALSE == find(array, 0, 10));
}

static void test2()
{
int array[10] = {1, 2};
assert(0 == find(array, 10, 1));
assert(FALSE == find(array, 10, 10));
}``````

（1）我们的算法需要测试用例的验证

（2）任何的优化都要建立在测试的基础之上

（3）测试和代码的编写要同步进行

（4）算法的成功运行时一步一步进行得，每一步的成功必须确立在原有的成功之上

2015-06-4 一步一步写算法