Это имеет смысл для меня с точки зрения логики, однако мои выходы являются последним числом рандомизированного числа массива снова и снова. Я подозреваю, что переменная постоянно помещается в то, что печатается. понимание был бы рад.bubble sort output error
import java.util.Arrays;
import java.util.Scanner;
class BubbleSort
{
public static void main(String args[]) {
Scanner inputScanner;
inputScanner = new Scanner(System.in);
System.out.println("please enter the size of the array");
int n = Integer.parseInt (inputScanner.nextLine());
//1. call upon array to fill said array with random numbers the size of n.
int[] filledArray = inputArray(n);
//2. print before sort array
printArray(filledArray, n);
//3. call the selection sort function
long startTime = System.currentTimeMillis();
int[] theSortedArray = sortArray(filledArray, n); //calculates milliseconds
long endTime = System.currentTimeMillis();
long timeResult = startTime - endTime;
//4. print sorted array
printSortedArray(theSortedArray, n);
System.out.println("ms(" + timeResult + ")"); // prints millaseconds after sorted numbers are printed out.
}
//1. fill array with random numbers
public static int[] inputArray(int numberOfValues){
int [] arrayToFill = new int [numberOfValues];
for(int i = 0;i<numberOfValues;i++){
double fraction = Math.random() * numberOfValues;
int integer = (int)fraction + 1;
arrayToFill[i] = integer;
//calculate a random number multiply by parameter(n) add one so it dosent
// equal 0 cast it as an integer so it Is useable then return to main.
}
return arrayToFill;
}
//2. print out presort array
public static void printArray(int[]preSortArray, int numberOfValues){
System.out.print("The unsorted numbers are ");
System.out.print("[");
for(int i = 0;i<numberOfValues;i++){
Arrays.toString(preSortArray);
System.out.print(preSortArray[i] + ",");
}
System.out.println("].");
}
// sorts the array
public static int[] sortArray(int[] preSortArray, int numberOfValues) {
for(int i=0;i<numberOfValues;i++) {
int tempNum = i;
for(int j=i+1;j<numberOfValues;j++){
int tempNumTwo = j;
int swap = 0;
if(preSortArray[tempNum] > preSortArray[tempNumTwo]) {
swap = preSortArray[tempNumTwo];
preSortArray[i] = preSortArray[tempNumTwo];
preSortArray[tempNum] = swap;
}
}
}return preSortArray;
}
//prints the sorted array
public static void printSortedArray(int [] sortedArray, int numberOfValues){
System.out.print("The sorted set of numbers is [");
for(int i = 0;i<numberOfValues;i++) {
Arrays.toString(sortedArray);
System.out.print(sortedArray[i] + ", ");
}
System.out.println("].");
}
}