package com.sxwl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
2019/3/4
author reading
常用基础算法
*/
public class BootDemo {
public static void main(String[] args) {
int[] num={10,20,30,40};
int[] num1={80,20,10,56,12,99,36,5,7};
int b_result =binarySearch(num,30);//b_result=2
// System.out.println(b_result);
BZ(num1);
}
/*
冒泡算排序
*/
public static void BA(int[] num){
for (int i =0; i < num.length; i++) {
for (int j =0; j < num.length-1-i; j++) {
if(num[j]
int temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
/*
选择排序
*/
public static void BZ(int[] num){
for (int i =0; i < num.length; i++) {
for (int j = i; j < num.length; j++) {
if(num[i]
int temp=num[j];
num[j]=num[j];
num[j]=temp;
}
}
}
}
/*
二叉树
@param int[] num 要查找的数组 必须是保证数组是有序的
@param int key 你找查找的数组
@result int 返回你查找的下标 0表示没有
*/
public static int binarySearch(int[] num,int key){
int start=0;
int end=num.length-1;
while(start<=end){
int middle=(start+end)/2;
if(num[middle]>key){
end=middle-1;
}else if(num[middle]
start=middle+1;
}else{
return middle;
}
}
return -1;
}
}
网友评论