This article introduces the introduction of bubble sorting of java arrays
package demo; import java.util.Arrays; public class Demo02 { public static void main(String[] args) { int[] arr = new int[10]; for(int i=0; i<arr.length; i++){ arr[i] = (int) (Math.random()*100); } System.out.println("冒泡排序前:"); System.out.println("第"+(0)+"次:"+Arrays.toString(arr)); bubbleSort(arr); System.out.println("冒泡排序前:"); System.out.println("第"+(0)+"次:"+Arrays.toString(arr)); } /* * 冒泡排序 */ public static void bubbleSort(int a[]) { for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } System.out.println("第"+(i+1)+"次:"+Arrays.toString(a)); } } }
The above is the detailed content of Introduction to bubble sorting of java arrays. For more information, please follow other related articles on the PHP Chinese website!