mercredi 18 octobre 2017

use same random array for each sort

I'm trying to make it so i generate a random array but implement just that one same array(in the random order) with different sorts. I have:

public static void main(String[] args)
{
 int[] array = new int[10];
  for(int i = 0; i < array.length; i++) {
   array[i] = (int)(Math.random()*100);}

   System.out.println("\nBefore Bubble Sort: ");
    for (int element : array)
      System.out.print(element + "  "); 

            bubbleSort(array);


   System.out.println("After Bubble Sort: "); 
    for (int element : array)
      System.out.print(element + "  ");
      System.out.println("\n");

    System.out.println("\nBefore Insertion Sort: ");
     for (int element : array)
      System.out.print(element + "  "); 

            insertionSort(array);


  System.out.println("After Insertion Sort: "); 
    for (int element : array)
      System.out.print(element + "  ");
        System.out.println("\n");

With corresponding code for the sorts(I will post them if necessary). Its output is:

Array Before Bubble Sort: 
2 64 27 1 81 60 72 6 9 82
Array After Bubble Sort: 
1 2 6 9 27 60 64 72 81 82 

Array Before Insertion Sort: 
1 2 6 9 27 60 64 72 81 82 
Array After Insertion Sort: 
1 2 6 9 27 60 64 72 81 82 

So basically i want this array 2 64 27 1 81 60 72 6 9 82 to be in the before insertion line as well. The sorted array from bubble sort is just being put in the insertion sort so its not doing anything. I think i need to make method for the random array and call that with each sort? How would i do that? Or any other solution Id appreciate. I will edit with more information if needed.




Aucun commentaire:

Enregistrer un commentaire