2

"To this program we will add the quick sort and the merge sort (non recursive)". I'm not sure how to do this with a random array. I formed this code so far, can anyone help?

import java.util.Random; public class Algo {

public static void main(String[] args) {
Random gen = new Random();
int[] a = new int[20];

for (int i = 0; i < a.length; i++)
a[i] = gen.nextInt(100);

printArray(a);
}

private static void printArray(int[] a){
for (int i : a)
System.out.print(i + " ");
System.out.println("");
}


}

}

1
  • Quick note, I think you want that println call outside of the for loop in printArray. Commented Mar 19, 2012 at 0:40

1 Answer 1

1

To generate an array of random elements, try this:

int[] array = new int[20];
Random random = new Random();
for (int i = 0; i < array.length; i++)
    array[i] = random.nextInt();

... Afterwards you can work on your merge sort and quick sort algorithms. What have you done so far?

public static void mergeSort(int[] array) {
    // sorts the array in-place using merge sort algorithm
}

public static void quickSort(int[] array) {
    // sorts the array in-place using quick sort algorithm
}
Sign up to request clarification or add additional context in comments.

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.