是否排序好了 Exercise07_19

 1 import java.util.Scanner;
 2 import java.util.Arrays;
 3 /**
 4  * @author 冰樱梦
 5  * 时间2018年12月
 6  * 题目:是否排序好了
 7  *
 8  */
 9 public class Exercise07_19 {
10     public static void main(String[] args){
11         Scanner input=new Scanner(System.in);
12         System.out.println("Enter the size of the list: ");
13         int sizeOfList=input.nextInt();
14         int[] list=new int[sizeOfList];
15         int[] list1=new int[list.length];
16         System.out.println("Enter the contents of the list: ");
17         for(int i=0;i<list.length;i++){
18             list[i]=input.nextInt();
19         }
20         System.out.print("The list has "+sizeOfList+" integers ");
21         for(int a:list){
22             System.out.print(a+" ");
23         }
24         for(int i=0;i<list.length;i++){
25             list1[i]=list[i];
26         }
27         if(isSorted(list1,list)){
28             System.out.println("
The list is already sorted");
29         }
30         else System.out.println("
The list is not sorted");
31     }
32     
33     
34     /**
35      * @param list1
36      * @param list
37      * @return  true or false 
38      * 判断两个数组是否相等
39      */
40     public static boolean isSorted(int[] list1,int[] list){
41         if(Arrays.equals(list1,bubble(list))){
42             return true;
43         }
44         return false;
45     }
46     
47     
48     
49     /**
50      * @param list
51      * @return list
52      * 修改了Arrays类的sort,变成int[] 类型的返回值
53      */
54     public static int[] bubble(int[] list){
55         
56         for(int i=0;i<list.length;i++){
57             for(int j=0;j<list.length-1-i;j++){
58                 if(list[j]>list[j+1]){
59                     int temp=list[j];
60                     list[j]=list[j+1];
61                     list[j+1]=temp;
62                 }
63             }
64         }
65         return list;
66     }
67 }
原文地址:https://www.cnblogs.com/cherrydream/p/10174145.html