# java select sort and bubble sort code

## Selection sort

The idea of sorting by selection method:
Compare the number one with all the others. If the number one is smaller, change to the first position
After the comparison, the first is the smallest
Then compare the second bit with all the rest. As long as it is smaller than the second bit, change to the second position
After the comparison, the second one is the second one
And so on ```public class HelloWorld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//Print out the content before sorting
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//Sorting by selection

//Step 1: compare the first bit with all other bits
//If data from other locations is found to be smaller than the first bit, it is exchanged

for (int i = 1; i < a.length; i++) {
if(a[i]<a){
int temp = a;
a = a[i];
a[i] = temp;
}
}
//Print out the content
//You can see that the smallest number is at the top
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");

//Step 2: compare the second bit with all the remaining bits
for (int i = 2; i < a.length; i++) {
if(a[i]<a){
int temp = a;
a = a[i];
a[i] = temp;
}
}
//Print out the content
//You can see that the next to last number is in the second position
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");

//We can find a rule
//The moving position is gradually increased from 0
//So we can put a layer of circulation outside

for (int j = 0; j < a.length-1; j++) {
for (int i = j+1; i < a.length; i++) {
if(a[i]<a[j]){
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}

//Print out the content
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
```

## Bubble sort

The idea of bubble sorting:
Step 1: start from the first digit and compare the two adjacent digits
If it is found that the front one is larger than the back one, the large data will be exchanged at the back. After the circular comparison, the last one is the largest one
Step 2: try again, but don't compare the last one
And so on ```public class HelloWorld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//Print out the content before sorting
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//bubble sort

//Step 1: start from the first digit and compare the two adjacent digits
//If you find that the front one is larger than the back one, exchange the big data at the back

for (int i = 0; i < a.length-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//Print out the content
//As you can see, the biggest is at the back
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");

//Step 2: try again, but don't compare the last one
for (int i = 0; i < a.length-2; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//Print out the content
//It can be found that the second from the bottom is the second from the bottom
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");

//We can find a rule
//The back boundary is shrinking
//So we can put a layer of circulation outside

for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}

//Print out the content
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
```

## Final

```/**
* 2020 12:45:25, February 2, 2015
* @author MR li
*   And select Sort Bubble Sort
*/
public class paixu {
//The idea of sorting by group selection method:
//Compare the number one with all the others. If the number one is smaller, change to the first position
//After the comparison, the first is the smallest
//Then compare the second bit with all the rest. As long as it is smaller than the second bit, change to the second position
//After the comparison, the second one is the second one
//And so on
public void paixu1(int []a) {
int c;
System.out.print("Initial array contents");
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
System.out.println();
//Selection sort
System.out.print("=====Selection sort=====");
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++) {
if(a[j]<a[i])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}

}
System.out.println();
System.out.print("Array contents after arrangement");
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
}
//The idea of bubble sorting:
//Step 1: start from the first digit and compare the two adjacent digits
//If it is found that the front one is larger than the back one, the large data will be exchanged at the back. After the circular comparison, the last one is the largest one
//Step 2: try again, but don't compare the last one
//And so on
public void paixu2(int []a) {
int c;
System.out.print("Initial array contents");
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
System.out.println();
//Bubble sort
System.out.print("=====Bubble sort=====");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length-i-1;j++) {
if(a[j+1]<a[j])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}

}
System.out.println();
System.out.print("Array contents after arrangement");
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
}

public static void main(String[] args) {
//Random array of length 5
int []a= {(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100)};
new paixu().paixu1(a);
System.out.println();
int []b= {(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100),(int)(Math.random()*100)};
new paixu().paixu2(b);
}

}

```  Published 5 original articles, won praise 2, visited 143

Tags: REST Big Data

Posted on Sun, 02 Feb 2020 13:02:07 -0500 by pandaweb