数组已排序或未排序?基础级别Java

huangapple go评论80阅读模式
英文:

Array sorted or not? Basic level Java

问题

以下是翻译好的代码部分:

package exercici11;

import java.util.Scanner;

public class SortedOrNot {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int[] myArray = new int[10];
        int prev1 = 0;
        int prev2 = 0;
        int cont1 = 0;
        int cont2 = 0;

        System.out.print("输入10个数字:");

        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = sc.nextInt();
        }

        String result = isSorted(myArray, prev1, prev2, cont1, cont2) ? "已排序。" : "未排序。";
        System.out.println(result);

        sc.close();

    }

    private static boolean isSorted(int[] myArray, int prev1, int prev2, int cont1, int cont2) {
        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray.length; j++) {
                prev1 = myArray[i];
                if (prev1 < myArray[i]) {
                    // 升序排序
                    cont1++;
                    if (cont1 == 10) {
                        return true;
                    }
                } else if (prev2 > myArray[i]) {
                    // 降序排序
                    cont2++;
                    if (cont2 == 10) {
                        // 升序排序
                        return true;
                    }
                }
            }
        }
        return false;
    }

}

谢谢!
Alex 您好。

英文:

I'm trying to make a program that tell me if my array is sorted (ascending and descending).

I created a function and if return true, the array is already sorted and if return false, the array is not sorted.

Actually, always get "Is not sorted".
I don't know if logic is correct, but I think yes, I don't know.

Can you help me, guys?

package exercici11;
import java.util.Scanner;
public class SortedOrNot {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] myArray = new int[10];
int prev1 = 0;
int prev2 = 0;
int cont1 = 0;
int cont2 = 0;
System.out.print(&quot;Enter 10 numbers: &quot;);
for (int i = 0; i &lt; myArray.length; i++) {
myArray[i] = sc.nextInt();
}
String result = isSorted(myArray, prev1, prev2, cont1, cont2) ? result = &quot;Is sorted.&quot; : &quot;Is not sorted.&quot;;
System.out.println(result);
sc.close();
}
private static boolean isSorted(int[] myArray, int prev1, int prev2, int cont1, int cont2) {
for (int i = 0; i &lt; myArray.length; i++) {
for (int j = 0; j &lt; myArray.length; j++) {
prev1 = myArray[i];
if (prev1 &lt; myArray[i]) {
// Ascending sort
cont1++;
if (cont1 == 10) {
return true;
}
} else if (prev2 &gt; myArray[i]) {
// Descending sort
cont2++;
if (cont2 == 10) {
// Ascending sort
return true;
}
}
}
}
return false;
}
}

Thank you!

Regards,
Alex.

答案1

得分: 1

我已经修改了```isSorted()```方法并添加了一些注释 &lt;br&gt;

import java.util.Scanner;

public class SortedOrNot {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int[] myArray = new int[10];

        System.out.print("输入10个数字:");

        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = sc.nextInt();
        }

        String result = isSorted(myArray) ? result = "已排序。" : "未排序。";
        System.out.println(result);

        sc.close();

    }

    private static boolean isSorted(int[] myArray) {
        int count = 0;
        int firstElement = myArray[0];
        int lastElement = myArray[myArray.length - 1];
        if(lastElement - firstElement >= 0){ 
        // 假设myArray已排序,如果(lastElement - firstElement) >= 0
        // 那么我检查数组中的元素是升序还是全部相同
            for(int i = 0; i < myArray.length - 1; i++){
                if(myArray[i] <= myArray[i + 1]) {
                    count++;
                }
            }
        }
        else {
        // 否则我检查数组中的元素是否降序排列
            for(int i = 0; i < myArray.length - 1; i++){
                if(myArray[i] >= myArray[i + 1]){
                    count++;
                }
            }
        }

        // 对于n个数字,将进行n - 1次比较
        return count == myArray.length - 1;
    }

}
英文:

I have changed isSorted() method and added some comments. <br>

import java.util.Scanner;
public class SortedOrNot {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] myArray = new int[10];
System.out.print(&quot;Enter 10 numbers: &quot;);
for (int i = 0; i &lt; myArray.length; i++) {
myArray[i] = sc.nextInt();
}
String result = isSorted(myArray) ? result = &quot;Is sorted.&quot; : &quot;Is not sorted.&quot;;
System.out.println(result);
sc.close();
}
private static boolean isSorted(int[] myArray) {
int count = 0;
int firstElement = myArray[0];
int lastElement = myArray[myArray.length - 1];
if(lastElement - firstElement &gt;= 0){ 
// Let&#39;s say myArray is sorted and if (lastElement - firstElement) &gt;= 0
// then I check if the elements from array are in ascending order
// OR are all the same
for(int i = 0; i &lt; myArray.length - 1; i++){
if(myArray[i] &lt;= myArray[i + 1]) {
count++;
}
}
}
else {
// else I check if the elements from array are in descending order
for(int i = 0; i &lt; myArray.length - 1; i++){
if(myArray[i] &gt;= myArray[i + 1]){
count++;
}
}
}
// for n numbers there will be n - 1 comparisons
return count == myArray.length - 1;
}
}

答案2

得分: 1

你可以使用 Arrays.sort() 方法来实现这个:

import java.util.Arrays;
import java.util.Collections;

public class CheckSortedArray {
    public static void main(String[] args) {
        Integer[] arr = { 1, 2, 3, 6, 9, 100 };
        Integer[] arr2 = { 1, -2, 3, 6, 9, 100 };
        Integer[] arr3 = { 9, 7, 0, -1, -100 };

        System.out.println(isArraySorted(arr, true));     // 以升序排序,输出 true
        System.out.println(isArraySorted(arr2, true));    // 未排序,输出 false
        System.out.println(isArraySorted(arr3, false));   // 以降序排序,输出 true
    }

    /**
     * 检查给定数组是否按指定顺序排序
     *
     * @param arr              要检查顺序的输入数组
     * @param isAscendingOrder true 表示升序,false 表示降序
     * @return 数组是否已排序
     */
    public static boolean isArraySorted(Integer[] arr, boolean isAscendingOrder) {
        Integer[] arrCopy = Arrays.copyOf(arr, arr.length);
        if (isAscendingOrder) {
            // 将数组升序排序
            Arrays.sort(arr);
        } else {
            // 将数组降序排序
            Arrays.sort(arr, Collections.reverseOrder());
        }
        // 检查原数组是否与排序后的数组相等
        return Arrays.equals(arr, arrCopy);
    }
}
英文:

You can use Arrays.sort() method for this:

import java.util.Arrays;
import java.util.Collections;
public class CheckSortedArray {
public static void main(String[] args) {
Integer[] arr = { 1, 2, 3, 6, 9, 100 };
Integer[] arr2 = { 1, -2, 3, 6, 9, 100 };
Integer[] arr3 = { 9, 7, 0, -1, -100 };
System.out.println(isArraySorted(arr, true)); 	// Prints true as array is sorted in ascending order
System.out.println(isArraySorted(arr2, true)); 	// Prints false as array is unsorted
System.out.println(isArraySorted(arr3, false)); // Prints true as array is sorted in descending order
}
/**
* Checks if given array is sorted or not in given order
*
* @param arr              Input array to check its order
* @param isAscendingOrder true for ascending order, false for descending
* @return Array is sorted or not
*/
public static boolean isArraySorted(Integer[] arr, boolean isAscendingOrder) {
Integer[] arrCopy = Arrays.copyOf(arr, arr.length);
if (isAscendingOrder) {
// Sorts the array in ascending order
Arrays.sort(arr);
} else {
// Sorts the array in descending order
Arrays.sort(arr, Collections.reverseOrder());
}
// Checks if the original array is equal to sorted array or not
return Arrays.equals(arr, arrCopy);
}
}

答案3

得分: 0

第二个for循环应从i+1开始,你应该将count1和count2与45进行比较。

英文:

the Second for should starts from i+1 and you should compare count1 and count2 with 45.

huangapple
  • 本文由 发表于 2020年10月18日 22:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64414589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定