面試:手寫個(gè)冒泡排序吧

public class Sort {public static void sort() {Scanner input = new Scanner(System.in);int sort[] = new int[10];int temp;System.out.println("請(qǐng)輸入10個(gè)排序的數(shù)據(jù):");for (int i = 0; i < sort.length; i++) {sort[i] = input.nextInt();}for (int i = 0; i < sort.length - 1; i++) {for (int j = 0; j < sort.length - i - 1; j++) {if (sort[j] < sort[j + 1]) {temp = sort[j];sort[j] = sort[j + 1];sort[j + 1] = temp;}}}System.out.println("排列后的順序?yàn)椋?);for(int i=0;i<sort.length;i++){System.out.print(sort[i]+" ");}}public static void main(String[] args) {sort();}}
評(píng)論
圖片
表情
