Method within a method (homework)

I have a homework assignment that asks me:

Write a method sort(int[] arr)

that takes an integer array and uses the method from the previous exercise to determine if the array is sorted in ascending order or not. If it is already sorted, it should return an array, if not, the method should sort the array before returning it.

My problem is I don't know how should I approach this assignment? Should I change the previous method, call it in a new method, or create a method in a method? This is the previous method I wrote:

public static void isSorted(int[] checkArray) {
    boolean isSorted = true;
    for (int i = 1; i < checkArray.length; i++) {
        if (checkArray[i - 1] > checkArray[i]) {
            isSorted = false;

      

Thanx in advance

+3


source to share


3 answers


In your interest to actually learn something, here's my solution:

public int[] sort(int[] arr) {
    if (!isSorted(arr))
        Arrays.sort(arr);
    return arr;
}

      

Here is the documentation forArrays.sort

Not only is this more eloquent, it also pretends to be in place. This means that instead of copying the array you are traversing, doing the work and then returning a copy, it just does the work on the original array. This makes it a little faster and means it takes up minimal space. Not a big deal when your input size is 10, but that's when you get to 10 million. Also note the wording of the assignment: If it is already sorted, it should return the array, if it is not, the method should sort the array before returning it.

Technically, returning a copy of the original array is not what the assignment wants. He wants the original.

So it's short, clean, fast, and efficient. It should be good, right?



No, because it has one huge flaw. It violates the implied contract of the method. A function that changes its arguments should not return anything, and a function that returns something should not change its arguments. This is the basic tenet of software development (and one that Arrays.sort demonstrates). Failure to do so can cause serious problems for those who believe you are competent and will not break this rule.

If you follow the rule, it sort

should be as follows:

public void sort(int[] arr) {
    if (!isSorted(arr))
        Arrays.sort(arr);
}

      

So tell your teacher what the abandoned hamster you met on the internet said to stop teaching you bad interface design.

+1


source


I read "uses the [to] method" to mean "invokes a method". However, testing a sort before sorting seems somewhat pointless, so double check with whoever gave you the assignment is worth double checking.



One problem with the previous method is that it doesn't return anything. You will need to do something to make it useful.

+1


source


This is what you want to do:

public int[] sort(int[] arr)
{
    if (isSorted(arr))
        return arr;
    else
    {
         \\Your sorting code here

         return sorted_array;
    }
}

      

+1


source







All Articles