1

The main method creates an array of random integers then calls the ascending and descending methods to sort the array. It seems that when I call the ascending and descending methods it alters the value of the original array. The output I receive when printing is three arrays all sorted in descending order.

public static void main(String[] args){

    Random random = new Random();
    Scanner myScan = new Scanner(System.in);

    System.out.println("How many random numbers should be created?");

    int size = myScan.nextInt();
    int[] array = new int[size];

    for(int i = 0; i<array.length; i++){    
        array[i] = random.nextInt(256);
    }

    int[] ascending = BubbleSort.ascending(array);

    int[] descending = BubbleSort.descending(array);

    System.out.print("Original array: ");
    for(int i = 0; i< array.length; i++){
        System.out.print(array[i] + " ");
    }
    System.out.println();   

    System.out.print("Array in ascending order: ");
    for(int i =0; i< ascending.length; i++){
        System.out.print(ascending[i] + " ");
    }
    System.out.println();


    System.out.print("Array in descending order: ");
    for(int i = 0; i<descending.length; i++){
        System.out.print(descending[i] + " ");
    }
    System.out.println();
}

Here are the two sorting methods.

public static int[] ascending(int[] a){

    int temp;

    for(int i = 0; i < a.length - 1; i++){
        for(int j = 0; j < a.length - 1; j++){
            if(a[j] > a[j+1]){
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }

    return(a);
}

public static int[] descending(int[] b){

    int temp;

    for(int i = 0; i < b.length - 1; i++){
        for(int j = 0; j < b.length -1; j++){   
            if(b[j] < b[j+1]){
                temp = b[j];
                b[j] = b[j+1];
                b[j+1] = temp;
            }
        }
    }

    return(b);
}

Instead of

int[] ascending = BubbleSort.ascending(array);

I thought to try

int[] ascending = new int[array.length];
ascending = BubbleSort.ascending(array);

But I receive the same results. Sorry for being such a programming newbie, and thanks for any guidance!

6
  • What is testtwo? And in Java array is an object. Commented Dec 16, 2013 at 18:32
  • Clone your array when passing it to the method. int[] ascending = ascending(array.clone()); Same for descending. Commented Dec 16, 2013 at 18:32
  • You didn't actually ask a question, though. What specifically do you need help with? Commented Dec 16, 2013 at 18:33
  • the clone method can lead to some confusion for people, I recommend reading the specification of the clone Method VERY closely before using it. Commented Dec 16, 2013 at 18:41
  • Sorry, testtwo should read BubbleSort. It looks like cloning the array was the step I was missing. I'm just going to manually clone the array with a for loop, since the clone method is somewhat out of the scope of what I'm learning. Commented Dec 16, 2013 at 18:46

1 Answer 1

1

I haven't looked at the correctness of your sorts, but what I can tell you is that both methods take in an array and do modify it directly.

public static int[] descending(int[] b){

    int temp;

    for(int i = 0; i < b.length - 1; i++){
        for(int j = 0; j < b.length -1; j++){   
            if(b[j] < b[j+1]){
                temp = b[j];
                // both of these calls CHANGE the array
                b[j] = b[j+1];                     
                b[j+1] = temp;
            }
        }
    }

    return(b);
}

Takes in an array (b) and returns that array sorted (return b). That means that whatever array you pass into this function will be changed.

 int[] array = new int[size];

So that array which is getting passed into:

int[] ascending = BubbleSort.ascending(array);

int[] descending = BubbleSort.descending(array);

First get's created, then MODIFIED once by ascending, then modified AGAIn by descending.

What you would need to do to avoid this is to create a copy of this array, or perform your sort in a manner which does not affect the original array.

See this question: Deep copy, shallow copy, clone

For a discussion of copying, deep copying and cloning in java. This should clarify some issues you're running into.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.