Directory:
1. Introduction:
2. Steps:
3, sorting effect:
4, Code display
5, the results show
————————————————————————————
1. Introduction:
Bubble sort (Bubble sort, Taiwan translation: bubble sort or bubble sort) is a simple sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence. 2. Steps:
1, compare the adjacent elements.
2, if the first one is larger than the second one, swap them both until the maximum number appears at the end.
3, repeat "second step", each cycle minus a number 3, sorting effect:
4, Code display
Package COM.DATASTRUCTURE.SOLR;
Import Java.util.Arrays;
/**
* Created by Yuhui on 2017/4/24.
* *
* bubble Sort
*/public
class A0_bubblesort {public
static void Main (String ars[]) {
int[] a={ 3,6,4,2,10,11,5};
SYSTEM.OUT.PRINTLN ("Initial state:" +arrays.tostring (a));
Int[] B = bubblesort (a);
System.out.println ("Final state:" +arrays.tostring (b));
}
public static int[] Bubblesort (int[] a) {
int tmep,num = 0;
int I, J;
for (i = a.length-1;i>0;i--) {for
(j=0;j<i;j++) {
if (A[j] > A[j+1]) {
TMEP = a[j];
A[J] = a[j+1];
A[J+1]=TMEP
}
}
System.out.println ("First" + (++num) + "trip Sort:" +arrays.tostring (a));
}
return A;
}
}
5, the results show
Initial state: [3, 6, 4, 2, 10, 11, 5]
1th trip Sort: [3, 4, 2, 6, 10, 5, 11]
2nd trip Sort: [3, 2, 4, 6, 5, 10, 11]
3rd trip Sort: [2, 3, 4, 5, 6, 10, 11]
4th trip Sort: [2, 3, 4, 5, 6, 10, 11]
5th trip Sort: [2, 3, 4, 5, 6, 10, 11]
6th trip Sort: [2, 3, 4, 5, 6, 10, 11]
Final status: [2, 3, 4, 5, 6, 10, 11]
If you like the blog I wrote, after reading the harvest is very big, may wish to small sponsor me, let me have the power to continue to write High-quality blog, thank you for your appreciation ...