Reputation: 5
I'm supposed to take an array of the numbers: {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96} and sort them from lowest to highest, and then highest to lowest.
When I try to print the highest to lowest it makes the first output the same. Does anyone see any errors in my code?
package l7c14sort;
import java.util.Arrays;
public class L7C14Sort {
public static void main(String a[]){
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
System.out.println("Original input: "+Arrays.toString(arr1)+"\n");
System.out.println("Lowest to highest:\n");
for(int i:arr2)
{
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
for(int k:arr3)
{
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
public static int[] doInsertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
return input;
}
public static int[] doInsertionSortAgain(int[] input2){
int temp2;
for (int k = 1; k < input2.length; k++) {
for(int j = k ; j > 0 ; j--){
if(input2[j] > input2[j-1]){
temp2 = input2[j];
input2[j] = input2[j-1];
input2[j-1] = temp2;
}
}
}
return input2;
}
}
Output:
Original input: [99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51,
48, 36, 36, 32, 32, 30, 4]
Highest to lowest:
99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51, 48, 36, 36, 32, 32, 30, 4,
Lowest to highest:
4,30,32,32,36,36,48,51,63,63,64,73,75,76,89,90,92,96,98,99
Upvotes: 0
Views: 124
Reputation: 136
You are ended up with same results, because arrays are mutable. Because of the following code, input array is mutated and its final value is printed.(Highest to lowest).
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
If you organize your code like:
public static void main(String a[]) {
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
System.out.println("Original input: " + Arrays.toString(arr1) + "\n");
System.out.println("Lowest to highest:\n");
int[] arr2 = doInsertionSort(arr1);
for (int i : arr2) {
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
int[] arr3 = doInsertionSortAgain(arr1);
for (int k : arr3) {
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
You are going to get:
Original input: [51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96]
Lowest to highest: 4, 30, 32, 32, 36, 36, 48, 51, 63, 63, 64, 73, 75, 76, 89, 90, 92, 96, 98, 99,
Highest to lowest: 99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51, 48, 36, 36, 32, 32, 30, 4,
Upvotes: 0
Reputation: 26
Good news: Your algorithm works fine.
In Java, arrays are passed by reference, not by value. What this means is that when you set int[] arr2 = doInsertionSort(arr1);
, the array arr2
is being set to the result of your doInsertionSort
method, which returns its input
parameter after sorting it. Basically, arr1
, arr2
, arr3
, input
and input2
are all pointing to the very same array.
You have two easy options to fix the fact that you're printing:
Restructure main()
so that you use one array: print its contents, sort it lowest to highest, print its contents again, sort it highest to lowest, then print its contents again. (This is probably what your instructor intends for you to do, if this is coursework.)
Make a copy of the input
parameter to operate on. You can do this with System.arraycopy()
like so:
int[] myArray;
System.arraycopy(input, 0, myArray, 0, input.length );
Then, for option 2, you would need to edit your method to use myArray
instead of input
for every other time you use input
.
As a note, you don't need to call your variables input2
, temp2
, etc. Just like i
, j
and k
go out of scope and are forgotten after the end of a for
loop, your variables input
and temp
mean nothing outside of the block you declared them in.
Hope this helps!
Upvotes: 1