--------------------------------------------------------------------------------------------------------
这道题是对排序的考察
--------------------------------------------------------------------------------------------------------
Arrays数组也有sort方法(自动排序)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 for(int i=0;i<n;i++) 8 a[i] = sc.nextInt(); 9 Arrays.sort(a); 10 for(int i:a) 11 System.out.print(i+" "); 12 } 13 }
用容器的方法
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 List<Integer> list = new ArrayList<Integer>(); 5 Scanner sc = new Scanner(System.in); 6 int n = sc.nextInt(); 7 for(int i=0;i<n;i++) 8 list.add(sc.nextInt()); 9 Collections.sort(list); 10 for(int i:list) 11 System.out.print(i+" "); 12 } 13 }
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 List<Integer> list = new ArrayList<Integer>(); 5 Scanner sc = new Scanner(System.in); 6 int n = sc.nextInt(); 7 for(int i=0;i<n;i++) 8 list.add(sc.nextInt()); 9 Collections.sort(list,new Comparator<Integer>() { 10 public int compare(Integer o1, Integer o2) { 11 if(o1<o2) 12 return 1; 13 else if(o1>o2) 14 return -1; 15 else 16 return 0; 17 } 18 }); 19 for(int i:list) 20 System.out.print(i+" "); 21 } 22 }
算法一(冒泡排序)
相邻两个比较,把较大的往上替换循环往复,这样大的会像冒泡一样一个一个冒出来(滑稽0.0)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 for(int i=0;i<n;i++) 8 a[i] = sc.nextInt(); 9 //冒泡排序 10 for(int i=0;i<n-1;i++){ 11 for(int k=0;k<n-1-i;k++){ 12 if(a[k]>a[k+1]){ 13 int tmp = a[k]; 14 a[k] = a[k+1]; 15 a[k+1] = tmp; 16 } 17 } 18 } 19 for (int i : a) { 20 System.out.print(i+" "); 21 } 22 } 23 }
算法二(选择排序)
每次循环找到最小的下标替换到前面去(有临时变量存下标,手动选择qaq)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 for(int i=0;i<n;i++) 8 a[i] = sc.nextInt(); 9 //选择排序 10 for(int k=0; k<n-1; k++) { 11 int min = k; 12 for(int i=k+1; i<n; i++) { 13 if(a[i] < a[min]) { 14 min = i; 15 } 16 } 17 if(k != min) { 18 int temp = a[k]; 19 a[k] = a[min]; 20 a[min] = temp; 21 } 22 } 23 for (int i : a) { 24 System.out.print(i+" "); 25 } 26 } 27 }
算法三(插入排序)
把前面部分看成插入区域,每次把后面一个数有选择的插入到前面去(如果小于前面的数,就把前面的数向后移,如果大于就直接替换)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 for(int i=0;i<n;i++) 8 a[i] = sc.nextInt(); 9 //插入排序 10 for(int i=1;i<n;i++){ 11 int tmp = a[i]; 12 for(int k=i-1;k>=0;k--){ 13 if(a[k]>tmp){ 14 a[k+1] = a[k]; 15 a[k] = tmp; 16 }else 17 break; 18 } 19 } 20 for (int i : a) { 21 System.out.print(i+" "); 22 } 23 } 24 }
其他算法(临时写的)
算法一(类似插入排序,直接在获取数值的时候就排序)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 //单排 8 for(int i=0;i<n;i++){ 9 a[i] = sc.nextInt(); 10 for(int j=0;j<i;j++){ 11 if(a[i]<a[j]){ 12 int tmp = a[i]; 13 a[i] = a[j]; 14 a[j] = tmp; 15 } 16 } 17 } 18 for (int i : a) { 19 System.out.print(i+" "); 20 } 21 } 22 }
算法二(冒泡和插入的结合怪胎,最开始没理解透插入算法就写了一个怪胎0.0)
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int n = sc.nextInt(); 6 int[] a = new int[n]; 7 //冒泡和插入的结合怪胎 8 for(int i=0;i<n;i++) 9 a[i] = sc.nextInt(); 10 for(int i=1;i<n;i++){ 11 for(int k=0;k<i;k++){ 12 if(a[i]<a[k]){ 13 int tmp = a[i]; 14 a[i] = a[k]; 15 a[k] = tmp; 16 } 17 } 18 } 19 for (int i : a) { 20 System.out.print(i+" "); 21 } 22 } 23 }