package suan;
/**
* @author yi
* java 算法
* 排序及查找
*/
public class MySort {
public static void main(String[] args) {
int num[]={2,7,9,1,3,11,5,4};
System.out.println(fen(2,num));
}
public static void mao(int []num){//冒泡排序
int tep=0;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length-1-i; j++) {
if(num[j]>num[j+1]){
tep=num[j];
num[j]=num[j+1];
num[j+1]=tep;
}
}
}
for (int k = 0; k < num.length; k++) {
System.out.print(num[k]+",");
}
}
public static void xuan(int []num){//选择排序
int tep=0;
for (int i = 0; i < num.length; i++) {
int k=i;
for (int j = i; j < num.length; j++) {
if(num[k]>num[j]){
k=j;
}
}
tep=num[k];
num[k]=num[i];
num[i]=tep;
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+",");
}
}
public static void cha(int num[]){//插入排序
int tep;
for (int i = 1; i < num.length; i++) {
for (int j = i; j >0; j--) {
if(num[j]<num[j-1]){
tep=num[j];
num[j]=num[j-1];
num[j-1]=tep;
}
}
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+",");
}
}
public static int fen(int i,int num[]){//二分查找
cha(num);
int pre=num[0];
int nex=num[num.length-1];
while(pre<nex){
int mid=(pre+nex)/2;
if(i>num[mid]){
pre=mid+1;
}else if(i<num[mid]){
nex=mid-1;
}else{
return mid;
}
}
return -1;
}
}