标签:method public while nbsp ann print arc 序列 end
二分查找:
package annie_ZhangP;
public class BinarySearch {
public static void main(String[] args) {
//二分查找法,针对有序序列的查找,复杂度是log(n)
int a[]={1,2,3,4,5,6,7,8,9};
//想要查找7的位置
int b=7;
int start=0;
int end=a.length-1;
while(b<= a[end]){
int c=(start +end)/2;
if(a[c] == b){
System.out.println(c+1);
break;
}else if(a[c] > b){
end=c-1;
}else{
start= c+1;
}
}
}
}
冒泡排序:
package annie_ZhangP;
public class BubbleSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//冒泡排序,从小到大排序
int[] s={1,3,5,2,6,8,9};
int temp;
for(int i=0;i<s.length-1;i++){
for(int j=0;j<s.length-1-i;j++){
if(s[j] >s[j+1]){
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(int m=0;m<s.length;m++){
System.out.println(s[m]);
}
}
}
标签:method public while nbsp ann print arc 序列 end
原文地址:http://www.cnblogs.com/BeNumberOne/p/6715756.html