Reputation: 469
I have created an implementation of the Bubble sort algorithm in Java. The code runs fine and gives a valid output, however, for some reason, when I sort the data in ascending order, it is doing it properly, but I have a problem when I try to print out the statement. Below is my code, as well a slightly better description of the problem!
import java.util.Arrays;
import java.util.Scanner;
//import java.util.regex.Pattern;
//import java.util.stream.Stream;
public class BubbleSortNumeric {
public static void main (String [] args) {
Integer [] unsortedData = getDataInput();
Integer [] sortedDataAscending;
Integer [] sortedDataDescending;
long start = System.nanoTime();
sortedDataAscending = bubbleSortAscending(unsortedData);
sortedDataDescending = bubbleSortDescending(unsortedData);
long stop = System.nanoTime();
System.out.println("Ascending: " + Arrays.toString(sortedDataAscending));
System.out.println("Descening: " + Arrays.toString(sortedDataDescending));
System.out.println("Execution time: " + ((stop - start) / 1e+6) + "ms.");
}
private static Integer [] getDataInput() {
System.out.println("Enter a set of integers seperated by a space.");
Integer [] userInput = {};
String strInput;
try(Scanner sc = new Scanner(System.in)) {
strInput = sc.nextLine();
}
String [] inputData = strInput.split("\\s+");
try {
userInput = Arrays.asList(inputData).stream().map(Integer::valueOf).toArray(Integer[]::new);
}catch(NumberFormatException e) {
System.out.println("ERROR. Invalid input.\n" + e.getMessage());
}
return userInput;
}
private static Integer [] bubbleSortAscending(Integer[] ascendingUnsorted) {
int n = ascendingUnsorted.length;
System.out.println(n);
if(n == 1) {
return ascendingUnsorted;
}
boolean swapped;
int temp;
do {
swapped = false;
for(int i = 1; i < n; i++) {
if(ascendingUnsorted[i - 1] > ascendingUnsorted[i]) {
temp = ascendingUnsorted[i - 1];
ascendingUnsorted[i - 1] = ascendingUnsorted[i];
ascendingUnsorted[i] = temp;
swapped = true;
}
}
n--;
}while(swapped == true);
return ascendingUnsorted;
}
private static Integer [] bubbleSortDescending(Integer [] descendingUnsorted) {
int n = descendingUnsorted.length;
if(n == 1) {
return descendingUnsorted;
}
boolean swapped;
int temp;
do {
swapped = false;
for(int i = 1; i < n; i++) {
if(descendingUnsorted[i - 1] < descendingUnsorted[i]) {
temp = descendingUnsorted[i];
descendingUnsorted[i] = descendingUnsorted[i - 1];
descendingUnsorted[i - 1] = temp;
swapped = true;
}
}
n--;
}while(swapped == true);
return descendingUnsorted;
}
}
when I call bubbleSortAscending
it works fine, and sorts the data in ascending order. Because I am timing the execution time of the program, I do not want to print out the results before also sorting the data in descending order.
My problem is, although both the methods work fine, I have a problem when printing out the results. An example of what happens is below:
Input
1 3 9 2 40 193
Output:
Ascending: [193, 40, 9, 3, 2, 1]
Descening: [193, 40, 9, 3, 2, 1]
Execution time: 0.527142ms.
If I were to move the print statement to just after the line sortedDataAscending = bubbleSortAscending(unsortedData);
then it would give the correct output, however, as I've already stated I do not want that.
So my question, even though I am asigning the results to two distinct variables, why, when I print the answers of both the variables, is the output the same?
Upvotes: 0
Views: 413
Reputation: 4984
You must make copy of input array because in Java You passing values as reference not as copy:
public class BubbleSortNumeric {
public static void main (String [] args) {
Integer [] unsortedData1 = getDataInput();
Integer [] unsortedData2 = new Integer[unsortedData1.length];
System.arraycopy(unsortedData1, 0, unsortedData2, 0, unsortedData1.length);
Integer [] sortedDataAscending;
Integer [] sortedDataDescending;
long start = System.nanoTime();
sortedDataAscending = bubbleSortAscending(unsortedData1);
sortedDataDescending = bubbleSortDescending(unsortedData2);
// ...
Tested and Your algorythm works fine, only array mistake:
$ javac BubbleSortNumeric.java && java BubbleSortNumeric
Enter a set of integers seperated by a space.
1 3 9 2 40 193
6
Ascending: [1, 2, 3, 9, 40, 193]
Descening: [193, 40, 9, 3, 2, 1]
Execution time: 0.068779ms.
Upvotes: 3
Reputation: 11121
I'll give you a hint: swap these two rows, execute, and you will see the magic. From this
sortedDataAscending = bubbleSortAscending(unsortedData);
sortedDataDescending = bubbleSortDescending(unsortedData);
to this
sortedDataDescending = bubbleSortDescending(unsortedData);
sortedDataAscending = bubbleSortAscending(unsortedData);
What does this suggest you?
.
.
.
.
That you're modifying twice the same array! All of your references
unsortedData
sortedDataAscending
sortedDataDescending
are pointing to the same object: an array, that is unsorted at first, then sorted in ascending order and finally in descending order.
At this point you decide to print it out.
Twice.
Upvotes: 0