Java record-36-Bubble Sorting
Bubble Sort is a simple Sorting Algorithm in the computer science field. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the larger element will slowly "float" to the top of the series through the exchange. The operation of the Bubble Sorting Algorithm is as follows: (from the back to the front) Compare adjacent elements. If the first is bigger than the second, exchange the two of them. Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number. Repeat the preceding steps for all elements except the last one. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.
Public class BubbleSortTest {private static void bubbleSort (int [] a) {for (int k =. length-1; k> 0; -- k) {boolean bl = true; for (int I = 0; I <k; I ++) {// The last element if (a [I]> a [I + 1]) {bl = false is not sorted each time; // exchange int temp = a [I]; a [I] = a [I + 1]; a [I + 1] = temp ;}} if (bl) {// when no exchange occurs in a step, the break has been sorted;} System. out. println ("Number" + (. length-k) + "step! "); For (int I = 0; I <. 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 ();}}
Next article: http://www.bkjia.com/kf/201510/447451.html