您的位置:首页 > 理论基础 > 数据结构算法

从头开始学习算法和数据结构

2015-08-03 23:18 543 查看
#include<stdio.h>

#define N  (10)

#define SUCCESS 0

#define FAULSE  -1

typedef int Elem;

/*Function  prototype*/

int InsetSort(Elem **e, int iNum);

void PrintList(Elem *e, int iNum);
void Test();

/*

*****************************************

* Function:  insert sort

* Paramen :  e: The number list

*            iNum: number of elements

* Return  :  0:SUCCESS  -1:FAULSE

* Notice  :  None

*****************************************

*/

int InsetSort(Elem *e, int iNum)

{

        int i, j, step;  

if (NULL == e){
return FAULSE;
}

for (i = 1; i < iNum; i++){

        step = e[i];
j = i - 1;
while( (j >= 0) && (e[j] > step))
{
e[j+1] = e[j];
j--;
}
e[j+1] = step;
}

return SUCCESS;

}

/*

*****************************************

* Function:  Output the list 

* Paramen :  e: The number list

*            iNum: number of elements

* Return  :  None

* Notice  :  None

*****************************************

*/

void PrintList(Elem *e, int iNum)

{
int i;
for (i = 0; i < iNum; i++){
printf("%d\n",e[i]);
}

}

/*

*****************************************

* Function:  Test 

* Paramen :  None

* Return  :  None

* Notice  :  None

*****************************************

*/

void Test()

{

    Elem eList
= {11, 3, 4, 6, 7, 62, 45, 24, 54, 9};

    int iResult;
iResult = InsetSort(eList, N);

if (FAULSE == iResult){
printf("The List is Not Exit!\n");
return ;
}

PrintList(eList, N);

}

int main(int argc, char* argv[])

{
Test();
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法