0

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 (i.e. 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

2 Answers2

0

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);

Daniel
  • 11
  • 2
  • I still receive the same error: "java:20: error: cannot find symbol bubblesort(values); ^ symbol: method bubblesort(int[]) location: class Chapter7AHomework" – Sam Malone Oct 07 '18 at 02:18
  • make sure that your file name and the class that wrap your main function has the same name. – Daniel Oct 07 '18 at 02:42
0

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;
                }
            }
        }
    }
}
Roozbeh
  • 462
  • 3
  • 14
  • While this compiles and run, it does not print the desired results. For the "Original Order" it prints 10,000 random integers. And then for the "Sorted Order" it only prints integers from 0 to 49. – Sam Malone Oct 07 '18 at 02:32
  • @SamMalone You asked that you want to print first 50 numbers – Roozbeh Oct 07 '18 at 02:40