问题描述
- 请大神们帮帮忙,程序中折半算法怎么不可以输入,直接跳到最后了
-
#include "stdafx.h"
#include
#define MIX_SIZE 20
using namespace std;typedef struct _Data
{
int *elem;
int lengh;
}Data;void Creat(Data &data)
{
data.elem=(int *)malloc(MIX_SIZE*sizeof(int));
data.lengh=0;
return;
}int IputData(Data &data)
{for(int i=0;cin>>data.elem[i]&&i<20;i++) { data.lengh++; if(data.elem[i] == '*') break; } return 0;
}
void MaoPaoPaixu(Data &data)
{
int max;
for(int i=0;i
{
for(int j=0;j
{
if(data.elem[j]>data.elem[j+1])
{
max=data.elem[j];
data.elem[j]=data.elem[j+1];
data.elem[j+1]=max;
}}
}for(int k=0;k<data.lengh;k++)
cout<<data.elem[k]<<" ";
return;
}void TeacherMaoPao(Data &data)
{
int i=0,temp;
int exchange=1;while(i<data.lengh&&exchange) { for(int j=0;j<data.lengh-i;j++) { if(data.elem[j]>data.elem[j+1]) { temp=data.elem[j]; data.elem[j]=data.elem[j+1]; data.elem[j+1]=temp; exchange=1; } i++; } } for(int k=0;k<data.lengh;k++) cout<<data.elem[k]<<" ";
}
void Select_Sort(Data &data)
{
int i,j,k,temp;
for(i=0;i
{
k=i;
for(j=i+1;j
{
if(data.elem[k] > data.elem[j])
k=j;
}
if(k!=i) //如果没有k 实现,则不用执行
{
temp = data.elem[k];
data.elem[k]=data.elem[i];
data.elem[i]=temp;
}
}
for(i=0; i<data.lengh;i++)
{
cout << data.elem[i] <<" ";
}
}int Search(Data &data)
{
int e;
cout<<"请输入要查找的数"<
cin>>e;int low=0,high=data.lengh-1; while(low<=high) { int mid=(low+high)/2; if(e==mid) { cout<<"该数在此列数内位置是data.elem["<<mid<<"]"<<endl; return 0; } else { if(e<data.elem[mid]) high=mid-1; else low=mid+1; } } if(low>high) { cout<<"没有您要查该数列找的数"<<endl; return 0; } return 0;
}
int main(int argc, _TCHAR* argv[])
{
Data data;Creat(data); IputData(data); cout<<"冒泡排序1"<<endl; MaoPaoPaixu(data); cout<<endl; cout<<"冒泡排序2"<<endl; TeacherMaoPao(data); cout<<endl; cout<<"选择排序"<<endl; Select_Sort(data); cout<<endl; cout<<"查找"<<endl; Search(data); return 0;
}