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.
The bubbling Sort algorithm works as follows: (from back to forward)
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
Repeat the above steps for all elements, except for the last one.
Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
public class bubblesorttest { private static void Bubblesort (int[] a) { for (int k=a.length-1; k> 0;&NBSP;--K) { boolean bl = true; for (int i=0; i <k; i++) { //each time the last element was not sorted if (a[i] > a[i+1]) { bl = false; //Exchange occurred int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } if (BL) { //When a step is not exchanged, the description is already sorted break; } system.out.println (" " + (A.LENGTH-K) + " Step !"); for (int i=0; i<a.length; i++) { System.out.print (a[i]+" "); } system.out.println (); } } public static void main (String[] args) { int[] a = {5,2,7,3,4,9,6}; bubblesort (a); }}
Java record -36-bubble sort