二分查找package Test;
public class BinarySearch {
public static void main(String[] args){
System.out.println(f(10));
f(0,"abcd");
int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10};
System.out.println(f(arr, 1, 0, arr.length - 1));
}
public static int f(int n){
if(n==1){
return 1;
}
return n*f(n-1);
}
public static void f(int n,String str){
if(n==str.length()){
return ;
}
f(n+1,str);
System.out.println(str.charAt(n));
}
private static int f(int[] a,int target,int i,int j){
if(i>j)return -1;
int m=(i+j)>>>2;
if(target<a[m]){
return f(a,target,i,m-1);
}else if(target>a[m]){
return f(a,target,m+1,j);
}else{
return m;
}
}
}
上述二分查找(递归)
冒泡排序(递归)
package Test;
public class BubbleMain {
public static void main(String[] args) {
//冒泡排序
}
public static void sort(int[] a,int j){
if(j==0){
return;
}
int x=0;//记录右边界
for (int i = 0; i < j; i++) {
if(a[i]>a[i+1]){
int i1 = a[i];
a[i]=a[i+1];
a[i+1]=a[i];
x=i;
}
}
sort(a,x);
}
}
插入排序(递归)
package Test;
public class insertSortMain {
//插入排序
public static void main(String[] args) {
int[] arr=new int[]{
1,2,4,52,5,2,5,2,423,52,1,2,63
};
insertSort(arr,1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static void insertSort(int[] a,int low){
if(low==a.length){
return;
}
int t=a[low];
int i=low -1;
while(i>=0&&a[i]>t){
a[i+1]=a[i];
i--;
}
a[i+1]=t;
insertSort(a,low+1);
}
}