0

I have this bubblesort code that i'm performing a runtime analysis on recording the time it takes to sort the array. I was wondering if there is any way i could increment the size of the array using a loop? Because at the moment i am incrementing it 100 at a time manually and i need to reach an array size of 5000.

public class BubbleSortworking{
public static void main (String[] args) {
    Random rand = new Random();
    int myArray[] = new int[100];  //How to increment this using a loop
    int count, count2;
    count2 = 2;   //amount of times to run the loop

    //repeats the bubble sort, while also producing new arrays each time
    for (count = 0; count < count2; count++){
        for (int i = 0; i < myArray.length; i++){

            myArray[i] = rand.nextInt(100) + 1;  //produce numbers between 1 - ?
            //System.out.print(myArray[i] + ", ");   //displays unsorted array
        }

        bubble(myArray);

        // uncomment below 2 lines to prove each new sorted array cycle is unique 
        //for (int i = 0; i < myArray.length; i++)
        //  System.out.print(myArray[i] + ", ");
    }
}

public static void bubble(int myArray[]){
    int temp;
    long start = System.nanoTime();
    //System.out.println("start " + start);

    //for (count = 0; count < count2; count++){
    for (int i=0; i < myArray.length - 1; i++) {
        for(int j=myArray.length - 1; j > i; j--) {
            if (myArray[j] < myArray[j-1]){
                temp = myArray[j];
                myArray[j] = myArray[j-1];
                myArray[j-1] = temp;
            }
        }
    }

    long end = System.nanoTime();
    System.out.println(end - start);
    //System.out.println("elapsed time " + (end - start));


}

}

4 Answers 4

1

No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new.

Sign up to request clarification or add additional context in comments.

Comments

1

You either need to use an ArrayList, which will do this for you but with extra overheads.

Or, you can allocate the array as size 5000 before you start, and record up to how many elements you have used so far in a variable (rather than relying on array.length)

Or, you can resize the array by making a new array which is bigger and copying all the elements to it (System.arrayCopy(..)), as well as putting the new ones in.

Comments

0

The answer by rizon is correct, you cannot change the size of an array. BTW, nowhere are you re-creating the array, nor do I see where you are processing 5000 elements. If you are concerned about processing time, you would not want to recreate/resize an array, as that would be very inefficient. You would want a different solution.

Comments

0

This may help you

int[] intA = new int[100];
int sizeToIncrement = 100;
for(int i=0;i<5000;i++) {
   if(i== intA.length ) {
      intA = Arrays.copyOf(intA, intA.length + sizeToIncrement);
   }
   intA[i] = i;
}

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.