冒泡排序
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; public static void main(String[] args) { for (int i = 0; i < a.length-1; i++) { for (int j=1;j<a.length-i;j++){ if (a[j-1]>a[j]){ a_swap(j-1,j); } } } a_printf(); } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }}
选择排序
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; public static void main(String[] args) { for (int i = 0; i < a.length; i++) { int max=0; for(int j=0;j<a.length-i;j++){ if(a[max]<a[j]){ max=j; } } a_swap(max,a.length-i-1); } a_printf(); } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }
插入排序
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; public static void main(String[] args) { for (int i =1; i < a.length; i++) { for(int j=i-1;j>0;j--){ if (a[j]>a[j+1]){ a_swap(j,j+1); } } } a_printf(); } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }}
二分查找
static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89}; public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int minNum=scanner.nextInt(); int left=0,right=a_order.length-1,min=0; while (left<=right){ min=(left+right)/2; if (a_order[min]==minNum){ System.out.println("get"); return; }else if (a_order[min]>minNum){ right=min-1; }else { left=min+1; } } System.out.println("don't get"); }
归并排序
递归
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89}; public static void main(String[] args) { mergeSort(0,a.length-1); a_printf(); } public static void mergeSort(int left,int right){ if (left<right){ int mid=(left+right)/2; mergeSort(left,mid); mergeSort(mid+1,right); merge(left,mid,mid+1,right); } } //[l1,r1]+[l2,r2] public static void merge(int l1,int r1,int l2,int r2){ int i=l1,j=l2,index=0; int[] temp=new int[10];//tenp临时存放合并后的数组,index为其下标 while (i<=r1&&j<=r2){ if (a[i]<=a[j]){ temp[index++]=a[i++]; }else { temp[index++]=a[j++]; } } while (i<=r1) temp[index++]=a[i++]; while (j<=r2) temp[index++]=a[j++]; for (i = 0; i < index; i++) { a[l1+i]=temp[i]; } } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }}
非递归
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89}; public static void main(String[] args) { mergeSort(); a_printf(); } public static void mergeSort(){ //step为组内元素个数,step/2为左子空间元素个数,注意等号可以不取 for (int step=2;step/2<=10;step*=2){ //每step个元素一组,组内前step/2和后step/2个元素进行合并 for (int i=0;i<10;i+=step){//对每一组 int mid=i+step/2-1;//左子区间元素个数为step/2 if (mid+1<10){//有子区间存在元素则合并 //左子空间为[i,mid] , 右子空间为[mid+1,min(i+step-1,n)] merge(i,mid,mid+1,Integer.min(i+step-1,9)); } } } } //[l1,r1]+[l2,r2] public static void merge(int l1,int r1,int l2,int r2){ int i=l1,j=l2,index=0; int[] temp=new int[10];//tenp临时存放合并后的数组,index为其下标 while (i<=r1&&j<=r2){ if (a[i]<=a[j]){ temp[index++]=a[i++]; }else { temp[index++]=a[j++]; } } while (i<=r1) temp[index++]=a[i++]; while (j<=r2) temp[index++]=a[j++]; for (i = 0; i < index; i++) { a[l1+i]=temp[i]; } } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }}
快速排序
package com.company;public class Main { static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9}; static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89}; public static void main(String[] args) { quickSort(0,9); a_printf(); } public static void quickSort(int left,int right){ if (left<right){ //当前区间长度超过1 //将[left,right]按a[left]一分为二 int pos=Partition(left,right); quickSort(left,pos-1); //对左子区间递归进行快速排序 quickSort(pos+1,right); //对右子区间递归进行快速排序 } } public static int Partition(int left,int right){ int temp=a[left];//将a[left]存放在临时变量temp while (left<right){ //只要left与right不相遇 while (left<right&&a[right]>temp) right--; //反复左移right a[left]=a[right]; //将a[right]挪到a[left] while (left<right&&a[left]<=temp) left++; //反复右移left a[right]=a[left]; //将a[left]挪到a[right] } a[left]=temp; //把temp放到left与right相遇的地方 return left; //返回相遇的下标 } public static void a_swap(int index_a,int index_b){ int temp=a[index_a]; a[index_a]=a[index_b]; a[index_b]=temp; } public static void a_printf(){ for (int value : a) { System.out.print(value + " "); } }}
深度优先搜索
package com.company;import java.util.Scanner;public class Main { static int n,V,maxValue=0; //物品件数n,背包容量v,最大价值maxValue static int[] w=new int[30],c=new int[30]; //w[i]为每件物品的重量,c[i]为每件物品的价值 public static void main(String[] args) { initData(); DFS(0,0,0); System.out.println(maxValue); } private static void initData() { Scanner scanner=new Scanner(System.in); System.out.println("cin n:"); n=scanner.nextInt(); System.out.println("cin V:"); V=scanner.nextInt(); System.out.println("cin w[i]:"); for (int i = 0; i < n; i++) { w[i]=scanner.nextInt(); } System.out.println("cin c[i]:"); for (int i = 0; i < n; i++) { c[i]=scanner.nextInt(); } } //DFS ,index为当前处理的物品编号 //sumW和sumC分别为当前总重量和当前总价值 public static void DFS(int index,int sumW,int sumC){ if (index==n){ //已经完成对n件物品的选择(死胡同) return; } //岔道口 DFS(index+1,sumW,sumC);//不选第index件物品 //只有加入第index件物品后未超过容量v,才能继续 if (sumW + w[index] <= V) { if (sumC+c[index]>maxValue){ maxValue=sumC+c[index]; //更新最大价值maxValue } } DFS(index+1,sumW+w[index],sumC+c[index]); //选第index件物品 }}
cin n:5cin V:8cin w[i]:3 5 1 2 2cin c[i]:4 5 2 1 3maxValue:10
树的遍历
package com.company;public class Node { private Integer id; private Node left; private Node right; public Node(Integer id) { this.id = id; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public Node(Integer id, Node left, Node right) { this.id = id; this.left = left; this.right = right; }}
递归遍历(前中后)
package com.company;public class Tree { private Node root; public Tree() { build(); } /** * 建树 【V80】 * * 1 * 2 3 * 4 5 6 * 7 */ public void build() { Node node7 = new Node(7); Node node6 = new Node(6); Node node5 = new Node(5, node7, null); Node node4 = new Node(4); Node node3 = new Node(3, node5, node6); Node node2 = new Node(2, null, node4); root = new Node(1, node2, node3); } //前序遍历 public void DLR(Node root){ if (root==null){ return; } System.out.print(root.getId()+" "); DLR(root.getLeft()); DLR(root.getRight()); } //中序遍历 public void LDR(Node root){ if (root==null){ return; } LDR(root.getLeft()); System.out.print(root.getId()+" "); LDR(root.getRight()); } //后序遍历 public void LRD(Node root){ if (root==null){ return; } LRD(root.getLeft()); LRD(root.getRight()); System.out.print(root.getId()+" "); } public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; }}
非递归遍历(前中后)
package com.company;import java.util.*;public class UDTree { private Node root; public UDTree() { build(); } /*非递归先序遍历 * 这里的思路是利用栈先进后出的性质,先将根节点入栈 * 循环中,弹出根节点,然后先加入右子节点,后加入左子节点 * 再一次循环,新弹出的节点就是刚压入的左子节点 * */ public void DLR(Node root){ if (root == null) { return; } Stack<Node> stack=new Stack<>(); stack.push(root); while (!stack.isEmpty()){ Node top=stack.pop(); System.out.print(top.getId()+" "); if (top.getRight()!=null) stack.push(top.getRight()); if (top.getLeft()!=null) stack.push(top.getLeft()); } } //非递归中序遍历 中序遍历是 左 根 右 public void LDR(Node root){ if (root == null) { return; } Stack<Node> stack=new Stack<>(); while (root!=null||!stack.isEmpty()){ while (root!=null){ stack.push(root); root=root.getLeft(); } if (!stack.isEmpty()){ root=stack.pop(); System.out.print(root.getId()+" "); root=root.getRight(); } } } //非递归后序遍历 由于后序遍历二叉树的顺序是 左 右 根,故可以使用中间临时栈保存 根 右 左 的遍历结果 public void LRD(Node root){ if (root==null) return; Stack<Node> stack=new Stack<>(); Stack<Node> stack1=new Stack<>(); while (root!=null||!stack.isEmpty()){ while (root!=null){ stack.push(root); stack1.push(root); root=root.getRight(); } if (!stack.isEmpty()){ root=stack.pop(); root=root.getLeft(); } } while (!stack1.isEmpty()){ System.out.print(stack1.pop().getId()+" "); } } public void build() { Node node7 = new Node(7); Node node6 = new Node(6); Node node5 = new Node(5, node7, null); Node node4 = new Node(4); Node node3 = new Node(3, node5, node6); Node node2 = new Node(2, null, node4); root = new Node(1, node2, node3); } public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; }}
层次遍历
public void LevelOrder(Node root){ if (root == null) { return; } Queue<Node> queue=new LinkedList<>(); queue.add(root); while (!queue.isEmpty()){ root=queue.poll(); System.out.print(root.getId()+" "); if (root.getLeft() != null) { queue.add(root.getLeft()); } if (root.getRight() != null) { queue.add(root.getRight()); } } }