简体   繁体   中英

Return a bubble sort as a separate array

I'm trying to write a program that generates an array populated by random numbers, then bubble sorts them and returns the sorted array as a separate array so you can compare the two.

However, once I create my random array and then try to created another sorted one, the sorted array "overwrites" the random one and the random one appears as a sorted one when I try to print it out.

My question is: How can I modify my code so that I can create and array of random doubles, then generate another, separate, array which is the sorted version of that random one?

Main:

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;


public class BubbleMain {

public static void main(String args[])throws IOException{

    int n;
    Scanner keyboard = new Scanner(System.in);

    while(true){
        try{
            System.out.println("Enter the size of the array");
            n = keyboard.nextInt();

            if(n >= 2){
                break;
            }
            System.out.println("Size must be 2 or greater");
        }catch(InputMismatchException e){
            System.out.println("Value must be an integer");
            keyboard.nextLine();
        }
    }


    double[] template = new double[n];
    double[] mess = Bubble.randPop(template);

    double[] tidy = Bubble.bubbleSort(mess);


    Bubble.printOut(mess);
    Bubble.printOut(tidy);


}
}

Bubble Class:

public class Bubble {

private double[] list;

public Bubble(double[] list){

    this.list = list;
}

public double[] getArray(){
    return list;
}

public static double[] randPop(double[] template){

    for(int i = 0; i < template.length; i++){
        template[i] = Math.random();
    }

    return template;
}



public static double[] bubbleSort(double[] mess){

    double[] tidy = new double[mess.length];

    for(int i=0; i<mess.length; i++)
    {
        for(int j=i + 1; j<mess.length; j++)
        {
            if(mess[i] > mess[j])
            {
                double temp = mess[i];
                mess[i] = mess[j];
                mess[j] = temp;
            }
        }
        tidy[i] = mess[i];
    }
    return tidy;
}




public static void printOut(double[] list){

    for(int i = 0; i < list.length; i++){
        System.out.println(list[i]);
    }
}

}

Just create a copy of the array first:

public static double[] bubbleSort(double[] mess){
    // Copy the array    
    double[] tidy = Arrays.copyOf(mess, mess.length);

    // sort
    for(int i=0; i<tidy.length; i++)
    {
        for(int j=i + 1; j<tidy.length; j++)
        {
            if(tidy[i] > tidy[j])
            {
                double temp = tidy[i];
                tidy[i] = tidy[j];
                tidy[j] = temp;
            }
        }
    }
    return tidy;
}

In bubbleSort , you are sorting the mess array and then assigning it to tidy . So mess will be sorted, and tidy will also refer to the sorted array.

You need to copy the array to tidy

double[] tidy = Arrays.copyOf(mess, mess.length);

and then sort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM