字符串逆序输出,冒泡,二分查找
输入一个字符串,将其逆序后输出
#include<iostream>
#include<string>
using namespace std;
void InverseStr(string &str)
{
int len=str.length();
char temp;
for(int i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
int main()
{
string str;
cout<<"input"<<endl;
cin>>str;
InverseStr(str);
cout<<str<<endl;
return 0;
}
冒泡法排序
void bubble(int[] nums)
{
int i,j,temp,len;
len=nums.length;
for(i=0;i<=len-1;i++)
{
for(j=0;j<=len-i-1;j++)
{
if(nums[j]>=nums[j+1])
{
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
}
二分查找法
public static int BiSearch(int[] nums,key)
{
int len=nums.length;
int start=0;
int end=len-1;
while(end>start)
{
mid=(start+mid)/2;
if(nums[mid]=key)
{
return mid;
}
else if(nums[mid]>key)
{
end=mid-1;
}else
{
start=mid+1;
}
}
return -1;
}