Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The name of the algorithm is because the larger the element will slowly "float" to the top of the sequence, hence the name.
1 PackageEdu.aeon.algorithm;2 3 Importjava.util.Arrays;4 5 /**6 * Bubble Sort7 * @authorLzj8 * @create October 30, 2017 morning 9:43:209 */Ten Public classBubblesort { One /** A * Bubble sorting algorithm implementation - * at October 30, 2017 a.m. 9:44:21 by Lzj - * @Parameters1 int[] Values to bubble sorted array the * @Returns void - */ - Public Static voidSortint[] values) { - inttemp; + for(inti=0;i<values.length;i++) { - for(intj=0;j<values.length-1-i;j++) { + if(values[j]>values[j+1]) { A //swapping with third-party variables attemp =Values[j]; -Values[j]=values[j+1]; -values[j+1]=temp; - } - } - } in } - /** to * at October 30, 2017 a.m. 9:43:20 by Lzj + * @Parameters1 string[] args - * @Returns void the */ * Public Static voidMain (string[] args) { $ //TODO auto-generated Method StubPanax Notoginseng int[] values=New int[10]; - //Initializes a random integer with a length of 10 and an element of 1 to 10 the for(inti=0;i<values.length;i++) { +Values[i]= (int) (Math.random () *10); A } theSYSTEM.OUT.PRINTLN ("The result before sorting is:" +arrays.tostring (values)); + Bubblesort.sort (values); -SYSTEM.OUT.PRINTLN ("The result after sorting is:" +arrays.tostring (values)); $ } $ -}
The results of the execution are attached:
Bubble sort of sorting algorithm