简体   繁体   中英

Java Arrays and Bubble Sort

UPDATE!!

I have managed to make the program generate 50 random integers (from a 10,000 random int array). However, I am struggling to make the bubble sort method sort the full values (ie 4579 and 3457) instead of just the single digits ( 3, 4, 4, 5, 5, 7, 7, 9)

This is the code I am working with:

public class RandomNumbers
{
   public static void main(String[] args)
   {
      int[] randomIntArray = new int[10000];

      for(int i = 0; i<randomIntArray.length; i++)
         randomIntArray[i] = (int)(Math.random() * 10000);

      for(int i = 0; i < 50; i++)
         System.out.println(randomIntArray[i]);

      System.out.println("Original order: ");
      for(int i = 0; i < 50; i++)
         System.out.print(randomIntArray[i] + "  ");

      IntBubbleSorter.bubbleSort(randomIntArray);

      System.out.println("\nSorted order: ");
      for(int i = 0; i < 50; i++)
         System.out.print(randomIntArray[i] + " ");

      System.out.println();
   }

}

and

public class IntBubbleSorter {
   public static void bubbleSort (int[] randomIntArray) {
      int lastPost;
      int index;
      int temp;

      for(lastPost = randomIntArray.length - 1; lastPost >= 0; lastPost--)
      {
         for(index = 0; index <= lastPost - 1; index++)
         {
            if(randomIntArray[index] > randomIntArray[index + 1])
            {
               temp = randomIntArray[index];
               randomIntArray[index] = randomIntArray[index + 1];
               randomIntArray[index + 1] = temp;
            }
         }
      }
   }
}

My current output looks like this (shorted to 5 integers for reading ease):

Original order: 3898  6015  462  1960  8040
Sorted order: 0 1 2 2 3

first of all in the main function in this loop:

 for(int element = 0; element < 50; element++)
          {
             values[element] = randomNumbers.nextInt(10000);
          }

you create only 50 random numbers in your 10,000 array and other number in the arry will assign to 0 by defualt.

second: try instede of this line : IntBubbleSorter.bubbleSort(values); this line: bubbleSort(values);

If your main and bubbleSort functions are in separate classes then make sure that they are in the same package(folder).

randomNumbers.nextInt(10000)

means that next random number should be between 0 and 10000 and you are only generating 50 random numbers.

I created two classes one for main function and the other for bubble sort. You should change them to whatever is good for you but make sure that they are in same folder(same package)

MainClass:

import java.util.Random;

public class MainClass {
    public static void main(String[] args)
    {
        // Initialize Array
        int [] values = new int[10000];
        Random randomNumbers = new Random();

        for(int index = 0; index < values.length; index++)
        {
            values[index] = randomNumbers.nextInt(10000);
        }

        System.out.println("Original order: ");
        for(int index = 0; index < 50; index++)
        {
            System.out.print(values[index] + "  ");
        }

        IntBubbleSorter.bubbleSort(values);

        System.out.println("\nSorted order: ");

        for(int index = 0; index < 50; index++)
        {
            System.out.print(values[index] + "  ");
        }

        System.out.println();
    }
}

IntBubbleSorter class:

public class IntBubbleSorter {
    public static void bubbleSort (int[] array) {
        int lastPost;
        int index;
        int temp;

        for(lastPost = array.length - 1; lastPost >= 0; lastPost--)
        {
            for(index = 0; index <= lastPost - 1; index++)
            {
                if(array[index] > array[index + 1])
                {
                temp = array[index];
                array[index] = array[index + 1];
                array[index + 1] = temp;
                }
            }
        }
    }
}

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