#include <stdioh>
int main()
{
int a[10],i,j,tem;
for(i=0;i<=9;i++)
{
printf("请输入第%d个数:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=8;i++)
for(j=0;j<=1-i;j++)
if(a[j]>a[j+1])
{
tem=a[i];
a[i]=a[i+1];
a[i+1]=tem;
}
for(i=0;i<=9;i++)
printf("%d ",a[i]);
return 0;
}
// hgjkgcpp : 定义控制台应用程序的入口点。
//
#include "stdafxh"
#define n 11
int count=0;
int bin_search(int a[],int low,int high,int x) //折半查找函数
{ count++;
int mid;
if(low>high) return -1;
else
{
mid=(low+high)/2;
if(x==a[mid]) return mid;
else if(x<a[mid])
return bin_search(a,low,mid-1,x);
else
return bin_search(a,mid+1,high,x);
}
}
int _tmain(int argc, _TCHAR argv[])
{
int s;
int a[n]={3,5,8,10,15,22,28,30,31,55};
s=bin_search(a,0,9,28);
if(s==-1){
printf("没有找到要查询的数\n");
printf("本次查询总用了 %d 次\n",count);
}
else
{
printf("本次查询总用了 %d 次\n",count);
printf("本次查找到的下标是: %d \n",s);
}
return 0;
}
这程序我已经运行成功了,我是在VS2005环境下运行的
#include <stdioh>
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;++i)
{
for(j=0;j<n-i-1;++j)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int search(int a[],int n, int s)
{
int i,j;
for(i=0,j=n-1;printf("%5d",(i+j)/2),i<j;)
{
if(a[(i+j)/2]<s)
i=(i+j)/2+1;
else
j=(i+j)/2-1;
}
return a[(i+j)/2]==s(i+j)/2:-1;
}
int main()
{
int a[10],i,s;
for(i=0;i<10;++i)
scanf("%d",&a[i]);
scanf("%d",&s);
sort(a,10);
for(i=0;i<10;++i)
printf("%5d",a[i]);
printf("\n");
printf("\n%s",search(a,10,s)>=0"Success":"Fail");
return 0;
}
折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小于该中点元素,则将待查序列缩小为左半部分,否则为右半部分。通过一次比较,将查找区间缩小一半。 折半查找是一种高效的查找方法。它可以明显减少比较次数,提高查找效率。但是,折半查找的先决条件是查找表中的数据元素必须有序。参考程序,希望对你有所帮助!
#include<stdioh>
void main()
{
int a[20],x,i,start,end;
printf("input 20 numbers:\n");
for(i=0;i<20;i++) scanf("%d",&a[i]);
printf("please enter the number:\n");
scanf("%d",&x);
for(start=0,end=19;start<=end;)
{
i=start+(end-start)/2;
if (x==a[i])
{
printf("%d",i+1);
getch();
return;
}
else if (x>a[i]) end = i-1;
else start=i+1;
}
}
别忘记给分哦
package comadtechinterftest;
public class Test{
public Test(Record a[]) {
int i, j, m, low, high;
Record temp;
for (i = 0; i < alength; i++) {
temp = a[i];
low = 0;
high = i;
while (low <= high) {
m = (low + high) / 2;
if (tempgetStudentID() < a[m]getStudentID()) {
high = m - 1;
} else {
low = m + 1;
}
}
for (j = i; j > high + 1 && j > 0; j--) { // 如果j>high 就会少遍历一个元素 || j>low
a[j] = a[j - 1];
}
a[j] = temp;
}
for (int t = 0; t < alength; t++) {
Systemoutprintln(a[t]getStudentID());
}
}
public static void main(String args[]) {
Record record1 = new Record(2,"张三1",810,18);
Record record2 = new Record(4,"张三2",820,11);
Record record3 = new Record(5,"张三3",830,12);
Record record4 = new Record(43,"张三4",840,13);
Record record5 = new Record(21,"张三5",850,14);
Record record6 = new Record(54,"张三6",860,15);
Record record7 = new Record(22,"张三7",870,16);
Record record8 = new Record(6,"张三4",880,17);
Record record9 = new Record(223,"张三9",890,18);
Record record10 = new Record(545,"张三10",800,19);
Record [] record = new Record[]{record1,record2,record3,record4,
record5,record6,record7,record8,record9,record10};
new Test(record);
}
}
希尔排序程序
void shellsort(int r[],int length)
{
int d,i,j,x,k;
d=length/2;
while(d>=1)
{for(i=d;i<length;i++)
{x=r[i];
j=i-d;
while(j>=0&&x<r[j])
{
r[j+d]=r[j];
j=j-d;
}
r[j+d]=x;
}
d=d/2;
}
}
你通过主函数调用希尔排序的子程序。主函数你自己写吧,我现在没时间把完整的程序写上!
以上就是关于用程序实现直接插入法排序,折半插入法排序、起泡法算法排序全部的内容,包括:用程序实现直接插入法排序,折半插入法排序、起泡法算法排序、程序实现折半查找算法,要求事先建立一个有序的顺序表、c语言折半查找 求助大佬等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)