# Java serialization 70 bubble algorithm and selection algorithm

1, Bubble sorting

1. That is, select the largest one in turn and put it at the back

```package com.bjpowernode.java_learning;

​

public class D70_1_BubbleSort {

public static void main(String[] args) {

/*

* Bubble sorting algorithm: there is an array of type int: 3 1 6 2 5

*/

int[] a = {3,1,6,2,5,45,8,9,86};

//Start sort

for (int i=a.length-1;i>0;i--) {

for (int j=0;j<i;j++) {

if(a[j]>a[j+1]) {

int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

//You can write like that

//for(int j=0;j<i;J++){

//  if(a[j]>a[i]){

//    int temp = a[j];

//    a[j] = a[i];

//    a[i] = temp;

//}

}

for(int i=0;i<a.length;i++) {

System.out.println(a[i]);

}

}

}``` 2, Select sort

1. That is to select the smallest one in turn and put it first

```package com.bjpowernode.java_learning;

​

public class D70_2_SelectionSort {

public static void main(String[] args) {

int [] a = {45,4,8,2,69,31,2,0};

int min = 0;

for(int i=0;i<a.length-1;i++) {

for(int j=i+1;j<a.length;j++) {

if(a[i]>a[j]) {

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

for(int i=0;i<a.length;i++) {

System.out.println(a[i]);

}

}

}``` Conclusion: in a loop, if there is an upper bound in the flag, then use the operation of flag+1 cautiously, which is easy to cause the array to cross the boundary; if there is a lower bound in the flag, then use the operation of flag-1 cautiously, which is easy to cause the cross-border, and the solution is to select the initial number of the loop.

4, Source code:

D70_2_SelectionSort.java
D70_1_BubbleSort.java

https://github.com/ruigege66/Java/blob/master/D70_1_BubbleSort.java

https://github.com/ruigege66/Java/blob/master/D70_2_SelectionSort.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials. Tags: Java github Big Data

Posted on Fri, 10 Jan 2020 14:29:02 -0500 by dezkit