How to make Bubble Sort in Java to output the sorted numbers?

前端 未结 4 790
野趣味
野趣味 2021-01-26 12:07

This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted. I\'m not sure what I

4条回答
  •  难免孤独
    2021-01-26 12:41

    You are printing the actual numbers in the order the user entered. Try this instead:

    int[] sortedNumbers = new int[15];
    
    sortedNumbers = BubbleSort (num);
    
        for (int i=0; i < sortedNumbers.length; i++)
        {
            System.out.println("The sorted numbers are: ");
            System.out.print(sortedNumbers[i]+ " ");
        }
    
    
    
     public static int[] BubbleSort(int [] num)
    {
        int temp;   
        for (int i=1; i num [j+1])
                {
                    temp = num [j];
                    num [j] = num [j+1];
                    num [j+1] = temp;
                }
            }
        }
    
        return num;
    }
    

提交回复
热议问题