java氣泡排序
阿新 • • 發佈:2018-12-19
import java.util.Arrays;
public class BubbleSort { public static void BubbleSort(int[] arr) { int temp;//定義一個臨時變數 for(int i=0;i<arr.length-1;i++){//冒泡趟數 for(int j=0;j<arr.length-i-1;j++){ if(arr[j+1]<arr[j]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } public static void main(String[] args) { int arr[] = new int[]{1,6,2,2,5}; BubbleSort.BubbleSort(arr); System.out.println(Arrays.toString(arr)); } }