将要递归的值放入递归变量中
最短路径
public class text3 {
int p,q;
int count=0;
int min=-1;
int [][] map;
int [][] used;
int[][] s={{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x,int y,int step){
if(x==p&&y==q){
count++;
if(min==-1){
min=step;
}else {
if(min>step){
min++;
}
}
}else {
for(int i=0;i<4;i++){
x=x+s[i][0];
y=y+s[i][1];
if(x>=map.length||x<0||x>=s[0].length||x<0){
continue;
}
if(map[x][y]==1||used[x][y]==1){
continue;
}
used[x][y]=1;
dfs(x,y,step+1);
used[x][y]=0;
}
}
}
}
八皇后问题
public class text5 {
int max=8;
int count=0;
int[] array=new int[8];
public static void main(String[] args) {
text5 text5=new text5();
text5.check(0);
System.out.println(text5.count);
}
void check(int n){
if(n==8){
count++;
return;
}else {
for(int i=0;i<max;i++){
array[n]=i;
if(judge(n)){
check(n+1);
}
}
}
}
boolean judge(int n){
for(int i=0;i<n;i++){
if(array[i]==array[n]||Math.abs(array[i]-array[n])==Math.abs(i-n)){
return false;
}
}
return true;
}
}
排序
归并排序空间O(n)
冒泡排序
public class text6 {
public static void main(String[] args) {
int arr[]=new int[80000];
Random random=new Random();
for(int i=0;i<80000;i++){
arr[i]=random.nextInt(80000);
}
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(date));
text6 text6=new text6();
text6.sort(arr);
Date date1=new Date();
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat1.format(date1));
}
public void sort(int[] arr){
int temp=0;
boolean falg=false;
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
falg=true;
}
}
if(falg==false){
break;
}
falg=false;
}
}
}
选择排序
public class text7 {
public static void main(String[] args) {
Random random=new Random();
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println(simpleDateFormat.format(date));
int[] arr=new int[80000];
for(int i=0;i<80000;i++){
arr[i]=random.nextInt(80000);
}
selectSort(arr);
Date date1=new Date();
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println(simpleDateFormat1.format(date1));
}
public static void selectSort(int[] arr){
int min;
int mixn;
for(int i=0;i<arr.length-1;i++){
min=i;
mixn=arr[i];
for(int j=i;j< arr.length-1;j++){
if(arr[min]>arr[j+1]){
min=j+1;
mixn=arr[j+1];
}
}
arr[min]=arr[i];
arr[i]=mixn;
}
}
}
插入排序
希尔排序
public class text8 {
public static void main(String[] args) {
int[] arr={101,34,119,1};
insertSort(arr);
}
public static void insertSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int val = arr[i+1];
int index = i;
while (index >= 0 && val < arr[index]) {
arr[index + 1] = arr[index];
index--;
}
arr[++index] = val;
}
System.out.println(Arrays.toString(arr));
}
}
public class text9 {
public static void main(String[] args) {
int[] arr={8,9,1,7,2,3,5,4,6,0};
shell(arr);
}
public static void shell(int[] arr){
int temp;
for(int gap=arr.length/2;gap>0;gap=gap/2) {
for (int i = gap; i < arr.length; i++) {
int val=arr[i];
int index=i-gap;
while (index>=0&&val<arr[index]){
arr[index+gap]=arr[index];
index=index-gap;
}
arr[index+gap]=val;
}
}
System.out.println(Arrays.toString(arr));
}
}
快排
public class text10 {
public static void main(String[] args) {
int[] arr={-9,23,23,23,-567,70};
quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr,int left,int right){
int pivot=arr[(left+right)/2];
int l=left;
int r=right;
int temp;
while (l<r){
while (arr[l]<pivot){
l+=1;
}
while (arr[r]>pivot){
r-=1;
}
if(l>=r){
break;
}
temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
if(arr[l]==pivot){
r-=1;
}
if(arr[r]==pivot){
l+=1;
}
}
if(l==r){
l+=1;
r-=1;
}
if(left<r){
quickSort(arr,left,r);
}
if(right>l){
quickSort(arr,l,right);
}
}
}
归并排序
public class text11 {
public static void main(String[] args) {
int arr[]={8,4,5,7,1,3,6,2};
int temp[]=new int[arr.length];
merge(arr,0,arr.length-1,temp);
System.out.println(Arrays.toString(arr));
}
public static void merge(int[] arr,int left,int right,int[] temp){
if(left<right){
int mid=(left+right)/2;
merge(arr,left,mid,temp);
merge(arr,mid+1,right,temp);
merge1(arr,left,mid,right,temp);
}
}
public static void merge1(int[] arr,int left,int mid,int right,int[] temp){
int l=left;
int j=mid+1;
int t=0;
while (l<=mid&&j<=right){
if(arr[l]<=arr[j]){
temp[t]=arr[l];
l++;
t++;
}else {
temp[t]=arr[j];
j++;
t++;
}
}
while (l<=mid){
temp[t]=arr[l];
l++;
t++;
}
while (j<=right){
temp[t]=arr[j];
j++;
t++;
}
int a=left;
int s=0;
while (a<=right){
arr[a]=temp[s];
s++;
a++;
}
}
}
基数排序
public class text12 {
public static void main(String[] args) {
int arr[]={53,3,542,748,14,214};
// int[][] bucket=new int[10][];
radis(arr);
}
public static void radis(int[] arr){
int max=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
}
int maxLength=(max+"").length();
int[][] bucket=new int[10][arr.length];
int[] bucketElment=new int[10];
for(int i=0,n=1;i<maxLength;i++,n*=10){
for(int j=0;j<arr.length;j++){
int digitOf=arr[j]/n%10;
bucket[digitOf][bucketElment[digitOf]]=arr[j];
bucketElment[digitOf]++;
}
int index=0;
for(int k=0;k<bucketElment.length;k++){
if(bucketElment[k]!=0){
for(int l=0;l<bucketElment[k];l++){
arr[index]=bucket[k][l];
index++;
}
}
bucketElment[k]=0;
}
}
System.out.println(Arrays.toString(arr));
}
}
//友负数不要用基数排序
二分查找(适合已经排序好的)
public class text15 {
public static void main(String[] args) {
int arr[]={1,3,4,5,7,8,9};
System.out.println(binary(arr,0,arr.length-1,11));
}
public static int binary(int[] arr,int left,int right,int findValue){
if(left>right){
return -1;
}
int mid=(left+right)/2;
int midVal=arr[mid];
if(findValue>midVal){
return binary(arr,mid+1,right,findValue);
}else if(findValue<midVal){
return binary(arr,left,mid-1,findValue);
}else {
return mid;
}
}
}
返回所有
public static ArrayList<Integer> asd(int[] arr, int left, int right, int findValue){
if(left>right){
return null;
}
int mid=(left+right)/2;
int midVal=arr[mid];
if(findValue>midVal){
return asd(arr,mid+1,right,findValue);
}else if(findValue<midVal){
return asd(arr,left,mid-1,findValue);
}else {
int temp=mid-1;
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(mid);
while (true){
if(temp<0||arr[temp]!=findValue){
break;
}else {
list.add(temp);
temp--;
}
}
temp=mid+1;
while (true){
if(temp>arr.length-1||arr[temp]!=findValue){
break;
}else {
list.add(temp);
temp++;
}
}
return list;
}
}
插值查找(分布比较均匀的有序序列)
public class text16 {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9,10};
System.out.println(insert(arr, 0, arr.length-1, 5));
}
public static int insert(int[] arr,int left,int right,int findVal){
if(left>right||findVal<arr[0]||findVal>arr[arr.length-1]){
return -1;
}
int mid=left+(right-left)*(findVal-arr[left])/(arr[right]-arr[left]);
int midVal=arr[mid];
if(findVal>midVal){
return insert(arr,mid+1,right,findVal);
}else if(findVal<midVal){
return insert(arr,left,mid-1,findVal);
}else {
return mid;
}
}
}
斐波那契查找算法(有序)
public class text17 {
public static int maxSize=20;
public static void main(String[] args) {
int[] arr={1,8,10,89,1000,1234};
System.out.println(fibSearch(arr,1234));
}
public static int[] asd(){
int[] fib=new int[maxSize];
fib[0]=1;
fib[1]=1;
for(int i=2;i<maxSize;i++){
fib[i]=fib[i-1]+fib[i-2];
}
return fib;
}
public static int fibSearch(int[] arr,int key){
int[] fib=asd();
int k=0;
while (arr.length>fib[k]){
k++;
}
int high=arr.length-1;
int low=0;
int[] ints = Arrays.copyOf(arr, fib[k]);
for(int i=arr.length;i<fib[k];i++){
ints[i]=arr[high];
}
while (low<=high){
int mid=low+fib[k-1]-1;
if(key<ints[mid]){
high=mid-1;
k--;
}else if(key>ints[mid]){
low=mid+1;
k-=2;
}else {
if(mid>arr.length-1){
return high;
}else {
return mid;
}
}
}
return -1;
}
}
哈希表
public class text18 {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
String key="";
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("add:添加");
System.out.println("list:显示");
System.out.println("exit:退出");
System.out.println("find:查找");
key=scanner.next();
switch (key){
case "add":
System.out.println("输入id");
int id=scanner.nextInt();
System.out.println("输入名字");
String next = scanner.next();
Emp emp = new Emp(id, next);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "exit":
System.exit(0);
case "find":
System.out.println("输入id");
id=scanner.nextInt();
hashTab.findEmpById(id);
case "dele":
System.out.println("输入id");
id=scanner.nextInt();
hashTab.dele(id);
default:
break;
}
}
}
}
class HashTab{
private EmpLink[] empLinks;
private int size;
public HashTab(int size){
this.size=size;
empLinks=new EmpLink[size];
for(int i=0;i<size;i++){
empLinks[i]=new EmpLink();
}
}
public void add(Emp emp){
int empLinked=hashFun(emp.id);
empLinks[empLinked].add(emp);
}
public void list(){
for(int i=0;i<size;i++){
empLinks[i].list();
}
}
public int hashFun(int id){
return id%size;
}
public void findEmpById(int id){
int empLinked=hashFun(id);
Emp emp=empLinks[empLinked].findById(id);
System.out.println(emp);
}
public void dele(int id){
int empLinked=hashFun(id);
empLinks[empLinked].dele(id);
}
}
class Emp{
public int id;
public String name;
public Emp next;
public Emp(int id,String name){
super();
this.id=id;
this.name=name;
}
}
class EmpLink{
private Emp head;
public void add(Emp emp){
if(head==null){
head=emp;
return;
}
Emp curEmp=head;
while (true){
if(curEmp.next==null){
break;
}
curEmp=curEmp.next;
}
curEmp.next=emp;
}
public void list(){
if(head==null){
System.out.println("为空");
return;
}
System.out.println("为");
Emp curEmp=head;
while (true){
System.out.println(curEmp.id+" "+curEmp.name);
if(curEmp.next==null){
break;
}
curEmp=curEmp.next;
}
}
public Emp findById(int id){
if(head==null){
System.out.println("链表空");
return null;
}
Emp curEmp=head;
while (true){
if(curEmp==null){
break;
}
if(curEmp.id==id){
break;
}
curEmp=curEmp.next;
}
return curEmp;
}
public void dele(int id){
if(head!=null){
if(head.id==id){
head=null;
return;
}
while (true){
if(head.next!=null){
if(head.next.id==id){
head.next=head.next.next;
break;
}
head=head.next;
}else {
break;
}
}
}else {
;
}
}
}