Bubble sort because of its simplicity, usually we learn a sort algorithm for all kinds of programming languages first choose to learn
1 Public classBubblesort {2 Public Static voidMain (string[] args) {3 //creates an array, which is a random sequence of elements.4 int[] array = {666, 8, 28, 66, 86, 68 };5 //create an object for the bubbling sort class6Bubblesort Sorter =NewBubblesort ();7 //call sort method to sort an array8 Sorter.sort (array);9 }Ten /** One * Bubble Sort A * The array to sort - */ - Public voidSortint[] Array) { the for(inti = 1; i < Array.Length; i++) { - //compares two adjacent elements, and the larger number bubbles up backwards - for(intj = 0; J < Array.length-i; J + +) { - if(Array[j] > array[j + 1]) { + inttemp = Array[j];//keep the first element value in a temporary variable -ARRAY[J] = array[j + 1];//Save the second element value to the first element cell +Array[j + 1] = temp;//keep the temporary variable, which is the first element, in the second element A } at } - } -Showarray (array);//output bubble-sorted array elements - } - //Show all elements of an array - Public voidShowarray (int[] Array) { in for(intI:array) {//The foreach format iterates through the array -System.out.print (">" + i);//output each array element value to } + System.out.println (); - } the}
Conv New, published in the blog Park
Reprint please indicate the source, welcome mail exchange:[email protected]. com
Java's bubble sort