模块与群体数据3

发布时间 2023-05-04 21:09:19作者: Misa先生

#include<iostream>
using namespace std;
template<class T>
void mySwap(T&x,T&y)
{
T temp=x;
x=y;
y=temp;
}
template<class T>
void bubblesort(T a[],int n)
{
int i=n-1;
while(i >0)
{
int lastExchangeIndex=0;
for(int j=0;j<i;j++)
if(a[j+1]<a[j])
{
mySwap(a[j],a[j+1]);
lastExchangeIndex=j;
}
i=lastExchangeIndex;
}
}
template<class T>
int BinSearch(const T list[],int n,const T key)
{
int low=0;
int high=n-1;
while(low<=high)
{
int mid=(low+high)/2;
if(key==list[mid])
return mid;
else if(key>list[mid])
low=mid+1;
else
high=mid-1;
}
return -1;
}