基本的算法归纳总结一下:
一、排序算法:(1)冒泡排序法(2)选择法(3)插入排序
二、查找算法:(1)顺序查找(2)二分查找(有序数列查找)
三、字符串操作(1)求串长(2)串连接(3)串拷贝(4)求子串(5)串比较
四、斐波那契数列(1)使用单变量(2)使用数组(3)使用递归函数
五、求最大数最小数算法(1)求最大数最小数(2)求最大数最小数所在的位置
六、杨辉三角形(1)使用一维数组(2)使用二维数组
七、倒序算法(1)倒序一个整数数组(2)倒序一个字符串
八、矩阵的操作(1)求最大数的行列下标(2)转置矩阵
这些算法我将一一动手写出来.若有更好的,更快的,高效的算法都可以贴出来.
一、排序算法:(1)冒泡排序法(2)选择法(3)插入排序
(1).#include
#include
int Bubble(int source[],int n)
{
int start=0,end=n-1;
int i;
int count=0;
while(start
for(i=start;i
{
int t;
t=source[i];
source[i]=source[i+1];
source[i+1]=t;
}
end--;
for(i=end;i>start;i--)
if(source[i]
int t;
t=source[i];
source[i]=source[i-1];
source[i-1]=t;
}
start++;
count++;
}
return count;
}
intBubble1(int source[],int n)
{/*新方法*/
int start=0,end=n-1;
int i,j;
int count=0;
while(start
for(i=start;i
{
int t;
t=source[i];
source[i]=source[i+1];
source[i+1]=t;
j=i;/*记录交换位置*/
}
end=j;/*把下一次向后走动的终点限定在最后一次交换的位置*/
for(i=end;i>start;i--)
if(source[i]
int t;
t=source[i];
source[i]=source[i-1];
source[i-1]=t;
j=i;
}
start=j;/*把下一次向前走动的终点限定为最后一次的交换位置*/
count++;
}
return count;
}
void init(int data[],int n)
{
int i;
for(i=0;i
}
void output(int data[],int n)
{
int i;
for(i=0;i
if(i%10==0)
printf("\n");
printf("%4d",data[i]);
}
}
int check(int data[],int n)
{
int i;
for(i=0;i
return 0;
return 1;
}
void main()
{
int data[800];
int count1,count2;
init(data,800);
count1=Bubble(data,800);
if(check(data,800)==1)
printf("\nRight.");
else
printf("\nWrong.");
printf("\nFirst:%d",count1);
init(data,800);
count2=Bubble1(data,800);
if(check(data,800)==1)
printf("\nRight.");
else
printf("\nWrong.");
printf("\nSecond:%d",count2);
}
运行结果:
Right.
First:400
Right.
Second:211
3月21日.编写:
<1>.冒泡排序
void bubble_sort(int a[], int n)
//将a中整数序列重新排列成自小至大有序的整数序列
{ int t;
for(i = n - 1; change = TRUE; i > 1 && change; i--)
{ change = FALSE;
for(j = 0; j < i; j++)
if(a[j] > a[j+1]) {t = a[j]; a[j] = a[j+1]; a[j+1] = t;
change = TRUE; }
}
}//bubble_sort
(2).选择排序
void select_sort(int a[],int n)
{ int t;
//将a中整数序列重新排列成自小至大有序的整数序列.
for(i = 0; i < n -1; i++)
{ j = i;
for( k = i + 1; k < n; k++)
if(a[k] < a[j]) j = k;
if(j!=i) { t=a[j]; a[j]=a[i]; a[i]=t;}
}
}//select_sort
评论