Bubble sort ——— Array (int []array)
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/89/42/wKioL1gN7HOymTewAAFjK2N9VsQ893.png-wh_500x0-wm_3 -wmp_4-s_25889069.png "title=" Qq20161024162258.png "alt=" Wkiol1gn7hoymtewaafjk2n9vsq893.png-wh_50 "/>
/* Bubble sort implementation (1) Find the maximum value in the array and assign it to the last position of the array (2) n elements require a maximum of n-1 trips (3) to determine whether the elements in the array are unordered or not. (4) Implement any array can be ordered from small to large (method); */ public class bubble{ public static Void main (String[]args) { //statically declares an array int []a={23,45,65,76,7,67,8,32}; // The implementation finds the maximum value in the array and assigns a value to the last position of the array findmax (a); //implementation A.length-1 trip, every trip to Judge A.length-1 rank (a); // Determines whether or not the elements in the array are unordered. The implementation of any array can be ordered from small to large (method) bubble (a); } //implementation finds the maximum value in the array and assigns it to the last position of the array public static Void findmax (int[]b) { int temp=0; for (int i=0;i<b.length-1;i++) { if (b[i]>b[i+1]) { temp=b[i]; b[i]=b[i+1]; b[i+1]=temp; } } SYSTEM.OUT.PRINTLN ("Maximum value assigned to end of array:") for (int i=0;i<b.length;i++) { system.out.print (B[i] + "\ t"); } system.out.println (); } //realize a.length-1 trip, every trip is judged a.length-1 public Static void rank (int []c) { system.out. println ("From small to large output:"); int temp=0;//Exchange Box for (int j =1;j<c.length;j++) {for (int i=0;i<c.length-1;i++) {if (c[i]>c[i+1]) {temp=c[i];c[i]=c[i+1];c[i+1]=temp;}} }for (INT&NBSP;NUM:C) { system.out.print (num+ "\ t"); } System.out.println (); } //determines whether the elements in the array are still unordered. Implementing any array can be ordered from small to large (method) public static void bubble (int[]d) { int temp=0; for (int i=1;i<d.length;i++) { /* do a flag, the function of the flag when the element in the array is already ordered is to jump out of the loop and no longer make the next comparison */ boolean flag=true; for (int j=0;j<d.length-i;j++) { if (d[j]<d[j+1]) { temp= D[J];&NBSP;D[J]=D[J+1];&NBSP;D[J+1]=TEMP;&NBSP;FLAG=FALSE;&NBSP;}IF (flag) {break;} } } system.out.println ("Sort from large to small:"); for (int &NBSP;NUM:D) { system.out.print (num+ "\ t"); } system.out.println (); } }
This article is from the "11890208" blog, please be sure to keep this source http://11900208.blog.51cto.com/11890208/1865161
Java Bubble sort