冒泡排序

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. public static void main(String[] args) {
  5. for (int i = 0; i < a.length-1; i++) {
  6. for (int j=1;j<a.length-i;j++){
  7. if (a[j-1]>a[j]){
  8. a_swap(j-1,j);
  9. }
  10. }
  11. }
  12. a_printf();
  13. }
  14. public static void a_swap(int index_a,int index_b){
  15. int temp=a[index_a];
  16. a[index_a]=a[index_b];
  17. a[index_b]=temp;
  18. }
  19. public static void a_printf(){
  20. for (int value : a) {
  21. System.out.print(value + " ");
  22. }
  23. }
  24. }

选择排序

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. public static void main(String[] args) {
  5. for (int i = 0; i < a.length; i++) {
  6. int max=0;
  7. for(int j=0;j<a.length-i;j++){
  8. if(a[max]<a[j]){
  9. max=j;
  10. }
  11. }
  12. a_swap(max,a.length-i-1);
  13. }
  14. a_printf();
  15. }
  16. public static void a_swap(int index_a,int index_b){
  17. int temp=a[index_a];
  18. a[index_a]=a[index_b];
  19. a[index_b]=temp;
  20. }
  21. public static void a_printf(){
  22. for (int value : a) {
  23. System.out.print(value + " ");
  24. }
  25. }

插入排序

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. public static void main(String[] args) {
  5. for (int i =1; i < a.length; i++) {
  6. for(int j=i-1;j>0;j--){
  7. if (a[j]>a[j+1]){
  8. a_swap(j,j+1);
  9. }
  10. }
  11. }
  12. a_printf();
  13. }
  14. public static void a_swap(int index_a,int index_b){
  15. int temp=a[index_a];
  16. a[index_a]=a[index_b];
  17. a[index_b]=temp;
  18. }
  19. public static void a_printf(){
  20. for (int value : a) {
  21. System.out.print(value + " ");
  22. }
  23. }
  24. }

二分查找

  1. static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89};
  2. public static void main(String[] args) {
  3. Scanner scanner=new Scanner(System.in);
  4. int minNum=scanner.nextInt();
  5. int left=0,right=a_order.length-1,min=0;
  6. while (left<=right){
  7. min=(left+right)/2;
  8. if (a_order[min]==minNum){
  9. System.out.println("get");
  10. return;
  11. }else if (a_order[min]>minNum){
  12. right=min-1;
  13. }else {
  14. left=min+1;
  15. }
  16. }
  17. System.out.println("don't get");
  18. }

归并排序

递归

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89};
  5. public static void main(String[] args) {
  6. mergeSort(0,a.length-1);
  7. a_printf();
  8. }
  9. public static void mergeSort(int left,int right){
  10. if (left<right){
  11. int mid=(left+right)/2;
  12. mergeSort(left,mid);
  13. mergeSort(mid+1,right);
  14. merge(left,mid,mid+1,right);
  15. }
  16. }
  17. //[l1,r1]+[l2,r2]
  18. public static void merge(int l1,int r1,int l2,int r2){
  19. int i=l1,j=l2,index=0;
  20. int[] temp=new int[10];//tenp临时存放合并后的数组,index为其下标
  21. while (i<=r1&&j<=r2){
  22. if (a[i]<=a[j]){
  23. temp[index++]=a[i++];
  24. }else {
  25. temp[index++]=a[j++];
  26. }
  27. }
  28. while (i<=r1) temp[index++]=a[i++];
  29. while (j<=r2) temp[index++]=a[j++];
  30. for (i = 0; i < index; i++) {
  31. a[l1+i]=temp[i];
  32. }
  33. }
  34. public static void a_swap(int index_a,int index_b){
  35. int temp=a[index_a];
  36. a[index_a]=a[index_b];
  37. a[index_b]=temp;
  38. }
  39. public static void a_printf(){
  40. for (int value : a) {
  41. System.out.print(value + " ");
  42. }
  43. }
  44. }

非递归

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89};
  5. public static void main(String[] args) {
  6. mergeSort();
  7. a_printf();
  8. }
  9. public static void mergeSort(){
  10. //step为组内元素个数,step/2为左子空间元素个数,注意等号可以不取
  11. for (int step=2;step/2<=10;step*=2){
  12. //每step个元素一组,组内前step/2和后step/2个元素进行合并
  13. for (int i=0;i<10;i+=step){//对每一组
  14. int mid=i+step/2-1;//左子区间元素个数为step/2
  15. if (mid+1<10){//有子区间存在元素则合并
  16. //左子空间为[i,mid] , 右子空间为[mid+1,min(i+step-1,n)]
  17. merge(i,mid,mid+1,Integer.min(i+step-1,9));
  18. }
  19. }
  20. }
  21. }
  22. //[l1,r1]+[l2,r2]
  23. public static void merge(int l1,int r1,int l2,int r2){
  24. int i=l1,j=l2,index=0;
  25. int[] temp=new int[10];//tenp临时存放合并后的数组,index为其下标
  26. while (i<=r1&&j<=r2){
  27. if (a[i]<=a[j]){
  28. temp[index++]=a[i++];
  29. }else {
  30. temp[index++]=a[j++];
  31. }
  32. }
  33. while (i<=r1) temp[index++]=a[i++];
  34. while (j<=r2) temp[index++]=a[j++];
  35. for (i = 0; i < index; i++) {
  36. a[l1+i]=temp[i];
  37. }
  38. }
  39. public static void a_swap(int index_a,int index_b){
  40. int temp=a[index_a];
  41. a[index_a]=a[index_b];
  42. a[index_b]=temp;
  43. }
  44. public static void a_printf(){
  45. for (int value : a) {
  46. System.out.print(value + " ");
  47. }
  48. }
  49. }

快速排序

  1. package com.company;
  2. public class Main {
  3. static int[] a=new int[]{3,61,14,53,65,12,89,18,11,9};
  4. static int[] a_order=new int[]{3,9,11,12,14,18,53,61,65,89};
  5. public static void main(String[] args) {
  6. quickSort(0,9);
  7. a_printf();
  8. }
  9. public static void quickSort(int left,int right){
  10. if (left<right){ //当前区间长度超过1
  11. //将[left,right]按a[left]一分为二
  12. int pos=Partition(left,right);
  13. quickSort(left,pos-1); //对左子区间递归进行快速排序
  14. quickSort(pos+1,right); //对右子区间递归进行快速排序
  15. }
  16. }
  17. public static int Partition(int left,int right){
  18. int temp=a[left];//将a[left]存放在临时变量temp
  19. while (left<right){ //只要left与right不相遇
  20. while (left<right&&a[right]>temp) right--; //反复左移right
  21. a[left]=a[right]; //将a[right]挪到a[left]
  22. while (left<right&&a[left]<=temp) left++; //反复右移left
  23. a[right]=a[left]; //将a[left]挪到a[right]
  24. }
  25. a[left]=temp; //把temp放到left与right相遇的地方
  26. return left; //返回相遇的下标
  27. }
  28. public static void a_swap(int index_a,int index_b){
  29. int temp=a[index_a];
  30. a[index_a]=a[index_b];
  31. a[index_b]=temp;
  32. }
  33. public static void a_printf(){
  34. for (int value : a) {
  35. System.out.print(value + " ");
  36. }
  37. }
  38. }

深度优先搜索

  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. static int n,V,maxValue=0; //物品件数n,背包容量v,最大价值maxValue
  5. static int[] w=new int[30],c=new int[30]; //w[i]为每件物品的重量,c[i]为每件物品的价值
  6. public static void main(String[] args) {
  7. initData();
  8. DFS(0,0,0);
  9. System.out.println(maxValue);
  10. }
  11. private static void initData() {
  12. Scanner scanner=new Scanner(System.in);
  13. System.out.println("cin n:");
  14. n=scanner.nextInt();
  15. System.out.println("cin V:");
  16. V=scanner.nextInt();
  17. System.out.println("cin w[i]:");
  18. for (int i = 0; i < n; i++) {
  19. w[i]=scanner.nextInt();
  20. }
  21. System.out.println("cin c[i]:");
  22. for (int i = 0; i < n; i++) {
  23. c[i]=scanner.nextInt();
  24. }
  25. }
  26. //DFS ,index为当前处理的物品编号
  27. //sumW和sumC分别为当前总重量和当前总价值
  28. public static void DFS(int index,int sumW,int sumC){
  29. if (index==n){ //已经完成对n件物品的选择(死胡同)
  30. return;
  31. }
  32. //岔道口
  33. DFS(index+1,sumW,sumC);//不选第index件物品
  34. //只有加入第index件物品后未超过容量v,才能继续
  35. if (sumW + w[index] <= V) {
  36. if (sumC+c[index]>maxValue){
  37. maxValue=sumC+c[index]; //更新最大价值maxValue
  38. }
  39. }
  40. DFS(index+1,sumW+w[index],sumC+c[index]); //选第index件物品
  41. }
  42. }
  1. cin n:
  2. 5
  3. cin V:
  4. 8
  5. cin w[i]:
  6. 3 5 1 2 2
  7. cin c[i]:
  8. 4 5 2 1 3
  9. maxValue:10

树的遍历

  1. package com.company;
  2. public class Node {
  3. private Integer id;
  4. private Node left;
  5. private Node right;
  6. public Node(Integer id) {
  7. this.id = id;
  8. }
  9. public Integer getId() {
  10. return id;
  11. }
  12. public void setId(Integer id) {
  13. this.id = id;
  14. }
  15. public Node getLeft() {
  16. return left;
  17. }
  18. public void setLeft(Node left) {
  19. this.left = left;
  20. }
  21. public Node getRight() {
  22. return right;
  23. }
  24. public void setRight(Node right) {
  25. this.right = right;
  26. }
  27. public Node(Integer id, Node left, Node right) {
  28. this.id = id;
  29. this.left = left;
  30. this.right = right;
  31. }
  32. }

递归遍历(前中后)

  1. package com.company;
  2. public class Tree {
  3. private Node root;
  4. public Tree() {
  5. build();
  6. }
  7. /**
  8. * 建树 【V80】
  9. *
  10. * 1
  11. * 2 3
  12. * 4 5 6
  13. * 7
  14. */
  15. public void build() {
  16. Node node7 = new Node(7);
  17. Node node6 = new Node(6);
  18. Node node5 = new Node(5, node7, null);
  19. Node node4 = new Node(4);
  20. Node node3 = new Node(3, node5, node6);
  21. Node node2 = new Node(2, null, node4);
  22. root = new Node(1, node2, node3);
  23. }
  24. //前序遍历
  25. public void DLR(Node root){
  26. if (root==null){
  27. return;
  28. }
  29. System.out.print(root.getId()+" ");
  30. DLR(root.getLeft());
  31. DLR(root.getRight());
  32. }
  33. //中序遍历
  34. public void LDR(Node root){
  35. if (root==null){
  36. return;
  37. }
  38. LDR(root.getLeft());
  39. System.out.print(root.getId()+" ");
  40. LDR(root.getRight());
  41. }
  42. //后序遍历
  43. public void LRD(Node root){
  44. if (root==null){
  45. return;
  46. }
  47. LRD(root.getLeft());
  48. LRD(root.getRight());
  49. System.out.print(root.getId()+" ");
  50. }
  51. public Node getRoot() {
  52. return root;
  53. }
  54. public void setRoot(Node root) {
  55. this.root = root;
  56. }
  57. }

非递归遍历(前中后)

  1. package com.company;
  2. import java.util.*;
  3. public class UDTree {
  4. private Node root;
  5. public UDTree() {
  6. build();
  7. }
  8. /*非递归先序遍历
  9. * 这里的思路是利用栈先进后出的性质,先将根节点入栈
  10. * 循环中,弹出根节点,然后先加入右子节点,后加入左子节点
  11. * 再一次循环,新弹出的节点就是刚压入的左子节点
  12. * */
  13. public void DLR(Node root){
  14. if (root == null) {
  15. return;
  16. }
  17. Stack<Node> stack=new Stack<>();
  18. stack.push(root);
  19. while (!stack.isEmpty()){
  20. Node top=stack.pop();
  21. System.out.print(top.getId()+" ");
  22. if (top.getRight()!=null)
  23. stack.push(top.getRight());
  24. if (top.getLeft()!=null)
  25. stack.push(top.getLeft());
  26. }
  27. }
  28. //非递归中序遍历 中序遍历是 左 根 右
  29. public void LDR(Node root){
  30. if (root == null) {
  31. return;
  32. }
  33. Stack<Node> stack=new Stack<>();
  34. while (root!=null||!stack.isEmpty()){
  35. while (root!=null){
  36. stack.push(root);
  37. root=root.getLeft();
  38. }
  39. if (!stack.isEmpty()){
  40. root=stack.pop();
  41. System.out.print(root.getId()+" ");
  42. root=root.getRight();
  43. }
  44. }
  45. }
  46. //非递归后序遍历 由于后序遍历二叉树的顺序是 左 右 根,故可以使用中间临时栈保存 根 右 左 的遍历结果
  47. public void LRD(Node root){
  48. if (root==null)
  49. return;
  50. Stack<Node> stack=new Stack<>();
  51. Stack<Node> stack1=new Stack<>();
  52. while (root!=null||!stack.isEmpty()){
  53. while (root!=null){
  54. stack.push(root);
  55. stack1.push(root);
  56. root=root.getRight();
  57. }
  58. if (!stack.isEmpty()){
  59. root=stack.pop();
  60. root=root.getLeft();
  61. }
  62. }
  63. while (!stack1.isEmpty()){
  64. System.out.print(stack1.pop().getId()+" ");
  65. }
  66. }
  67. public void build() {
  68. Node node7 = new Node(7);
  69. Node node6 = new Node(6);
  70. Node node5 = new Node(5, node7, null);
  71. Node node4 = new Node(4);
  72. Node node3 = new Node(3, node5, node6);
  73. Node node2 = new Node(2, null, node4);
  74. root = new Node(1, node2, node3);
  75. }
  76. public Node getRoot() {
  77. return root;
  78. }
  79. public void setRoot(Node root) {
  80. this.root = root;
  81. }
  82. }

层次遍历

  1. public void LevelOrder(Node root){
  2. if (root == null) {
  3. return;
  4. }
  5. Queue<Node> queue=new LinkedList<>();
  6. queue.add(root);
  7. while (!queue.isEmpty()){
  8. root=queue.poll();
  9. System.out.print(root.getId()+" ");
  10. if (root.getLeft() != null) {
  11. queue.add(root.getLeft());
  12. }
  13. if (root.getRight() != null) {
  14. queue.add(root.getRight());
  15. }
  16. }
  17. }