4. 陣列int[] intArr = new int[]{5,9,3,7,2,6},寫出一個函式可根據傳參(引數為需要獲取的陣列型別:1:正序排序陣列;2:倒序排序陣列;)來進行排序,返回值為int
阿新 • • 發佈:2018-12-18
4. 陣列int[] intArr = new int[]{5,9,3,7,2,6},寫出一個函式可根據傳參(引數為需要獲取的陣列型別:1:正序排序陣列;2:倒序排序陣列;)來進行排序,返回值為int陣列,請注意程式碼規範與質量。 [分值:20]
您的回答:(空) (簡答題需要人工評分)
package com.interview; public class Test4 { public static void main(String[] args) { int[] arr = {6, 3, 8, 2, 9, 1}; bubbleSort(arr,1); for(int i:arr){ System.out.print(i+" "); } System.out.println(); System.out.println("******************"); bubbleSort(arr,0); for(int i:arr){ System.out.print(i+" "); } } /** * * @param arr * @param m 0代表正序排列 1代表反序排列 填其他數字預設等於1 * @return */ public static int[] bubbleSort(int[] arr,int m) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if(m==0){ if (arr[j] >= arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } }else { if (arr[j] <= arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } return arr; } }