每日打卡第二周 2
1.8冒泡排序
#include<bits/stdc++.h>
using namespace std;
#define N 10;
int main()
{
int t=0;
int a[N];
for(int i=0;i<N;i++)
{
cin>>a[i];
}
for(int i=1;i<=N-1;i++)
{
for(int j=0;j<N-i;j++)//第一次比较N-1次,每次减少
{
if(a[n]>a[n+1])
{
t=a[n];
a[n]=a[n+1];
a[n+1]=t;
}
}
}
for(i=0;i<N;i++)
{
count++;
cout<<a[i]<<" ";
if(count%5==0)
printf("\n");
}
}
1.9折半查找法
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[N]={3,4,5,6,7,8,9,10};
int low=0;high=N-1;mid;m,k=-1;
for(int i=0;i<N;i++)
{
cout<<" "<<a[i];
}
cout<<"选择你要查找的数"<<endl;
cin>>m;
while(low<=high)//无限循环
{
mid=(low+high)/2;
if(m>a[mid])
low=mid+1;
else if(m<a[mid])
high=mid-1;
else
{
k=mid;
break;
}
}
if(k>=0)
cout<<"您所查找的数位于:"<<k; //防止输入的数不是数列内的
else
cout<<"Not found";
}

浙公网安备 33010602011771号