冒泡排序
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[10];
int i,t,j;
for (i=0;i<10;i++)
{
printf ("请输入第%d数据!",i+1);
scanf ("%d",&a[i]);
}
for (i=1;i<10;i++)
{
for (j=0;j<10-i;j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for (i=0;i<10;i++)
{
printf ("【%d】",a[i]);
}
system("pause");
return 0;
}
简单选择排序
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[10]={10,45,3,45,46,44,78,66,75,985};
int i,j,min,t;
for(i=0;i<10;i++)
{
min=i;
for(j=i+1;j<10;j++)
if(a[min]>a[j])
min=j;
t=a[i];
a[i]=a[min];
a[min]=t;
}
for(i=0;i<10;i++)
{
printf("%d,",a[i]);
}
system("pause");
return 0;
}
直接插入排序
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[]={45,42,132,78,65,64,71,48,13,137};
int i,t,j;
for (i=1;i<10;i++)
{
t=a[i];
j=i-1;
while (a[j]>t&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
}
for (i=0;i<10;i++)
{
printf ("【%d】",a[i]);
}
system("pause");
return 0;
}
顺序查找
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[10]={45,42,132,78,65,64,71,48,13,137};
int i,x,y;
printf("输入你要查找的数:");
scanf("%d",&x);
y=0;
for(i=0;i<10;i++)
{
if(x==a[i])
{
y=1;
printf("你要查找的数%d在第个%d位置\n",x,i+1);
break;
}
}
if(y==0)printf("无法找到你要查找的数\n");
system("pause");
return 0;
}
二分查找
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[10]={25,29,36,45,78,88,97,136,456,888};
int low,high,mid,x;
printf ("请输入要查找的数据:");
scanf("%d",&x);
low=0;
high=9;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==x) break;
else if(a[mid]>x)high=mid-1;
else low=mid+1;
}
if(low>high)
printf("查找失败\n");
else
printf("a[%d]是查找记录\n",mid);
system("pause");
return 0;
}