题目
冒泡排序
import java.util.Arrays;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();sc.nextLine();int[] res = new int[n];for(int i=0;i<n;i++)res[i] = sc.nextInt();
// Arrays.sort(res);for(int i=0;i<n;i++) {for(int j=i+1;j<n;j++) {if(res[i]>res[j]) {int t = res[i];res[i] = res[j];res[j] = t;}}}for(int i=0;i<n;i++)System.out.print(res[i]+" ");sc.close();}
}
选择排序
import java.util.Arrays;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();sc.nextLine();int[] res = new int[n];for(int i=0;i<n;i++)res[i] = sc.nextInt();for(int i=0;i<n-1;i++) {int min = i;for(int j=i+1;j<n;j++) {if(res[j]<res[min]) {min = j;}}if(min!=i) {int t = res[i];res[i] = res[min];res[min] = t;}}for(int i=0;i<n;i++)System.out.print(res[i]+" ");sc.close();}
}
插入排序
import java.util.Arrays;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();sc.nextLine();int[] res = new int[n];for(int i=0;i<n;i++)res[i] = sc.nextInt();for(int i=1;i<n;i++) {int t = res[i];int j = i;while(j>0 && t<res[j-1]) {res[j] = res[j-1];j--;}if(j!=i) {res[j] = t;}}for(int i=0;i<n;i++)System.out.print(res[i]+" ");sc.close();}
}