冒泡排序输出错误



这在逻辑上对我来说是完全有意义的,但是我的输出是数组中随机数字的最后一个数字。我怀疑正在打印的内容中不断地添加了一个变量。如果你有真知灼见,不胜感激。

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("].");                                 
    }
}

试试这个

public static int[] sortArray(int[] preSortArray, int numberOfValues) {
    int temp;
    for(int i=0; i < numberOfValues.length-i; j++) {
      for(int j=1; j < numberOfValues.length-i; j++){
        if(numberOfValues[j-1] > numberOfValues[j]);
           swap = preSortArray[tempNumTwo];
           preSortArray[j-1] = numberOfValues[j];
           preSortArray[tempNum] = swap;
            }
    }
}return preSortArray;
}         

首先,您的交换代码是错误的!第二行应该是:

preSortArray[i] = preSortArray[tempNumTwo];

第二,冒泡排序的实现也是错误的。在每次迭代中,我们不会比较第i个数字与第j个数字!我们比较两个连续的数字。下面是你的代码的更正版本:

public static int[] sortArray(int[] preSortArray, int numberOfValues) {
  for(int i=0;i<numberOfValues - 1;i++) {
    int tempNum = i; 
    for(int j=i+1;j<numberOfValues;j++, tempNum++){
      int tempNumTwo = j;
      int swap = 0;
      if(preSortArray[tempNum] > preSortArray[tempNumTwo]) {
        swap = preSortArray[tempNumTwo];
        preSortArray[tempNumTwo] = preSortArray[tempNum];
        preSortArray[tempNum] = swap;
      }
    }
  }return preSortArray;
} 

最新更新