0

I have written a sorting algorithm (bubble) and I have used 10000 unique values i.e.

int BubArray[] = new int[]{#10000 unique unsorted values#};

I was wondering how I would put the integers into a file and call the file instead of the 10000 integers in the code.

Also in which format would they go (with commas, spaces?) I'm not sure.

Any help would be appreciated, thanks.

6
  • 2
    Use simple file handling also it is upto you to go with commas,spaces. It depends. Commented Dec 7, 2012 at 8:52
  • 2
    why don't you use Random class to generate 10000 random values? Commented Dec 7, 2012 at 8:53
  • @BhavikShah because how do you know the bubble algorithm worked and the Random class did not just generated 1000 random values completely sorted? J/K Commented Dec 7, 2012 at 9:05
  • 1
    @Averroes : it never generates values in sorted order Commented Dec 7, 2012 at 9:22
  • @Averroes it's better to test on random numbers. If you specify the 1000 numbers manually, you only know it worked for that specific input. Commented Dec 7, 2012 at 9:26

4 Answers 4

4

This is not correct answer, just hint how to use file, but you can modify the code and make it usable according to your need.

    try {

        String str;
                    String[] temp;

        BufferedReader br = new BufferedReader(new FileReader("your filepath"));

        while ((str= br.readLine()) != null) {
                            temp = str.split(";"); // seperator words bye;
            System.out.println(str);
                    for(int i = 0; i<temp.lenght; i++)
                            System.out.println(temp[i]);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you need some random numbers to test your sort method:

int amount = 1000;
int[] array = new int[amount];
Random rand = new Random();
for (int i = 0; i < array.length; i++) {
    array[i] = rand.nextInt();
}

Comments

2

If you want to give input 1000 different unique values Random is not an good idea. Random can give same value more than once.

Comments

2

Put it in a File separated by line breaks and use a Scanner to read line by line, putting them into an array.

An example taken from Scanner documentation page:

  Scanner sc = new Scanner(new File("myNumbers"));
  while (sc.hasNextLong()) {
      long aLong = sc.nextLong();
  }

You can easily modify it to get int instead of long using hasNextInt() and nextInt() methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.