Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with a method that randomly generates 50 numbers between 1 and 1

ID: 3582730 • Letter: W

Question

Write a program with a method that randomly generates 50 numbers between 1 and 1000 and stores them in an array of integers, sorts each list, and displays them on one line. Your program should create to random arrays of 50 integers, sorted, and displays them with Array1 and Array2 proceeding them. Then write the following method that merges the two sorted lists into a new sorted list.

Public static int[] merg(int[] list1, int[] list2)

Implement the method in a way that takes at most lsit1.length+list2.length comparisons.

Explanation / Answer

MergeArray.java

import java.util.Arrays;
import java.util.Random;


public class MergeArray {

  
   public static void main(String[] args) {
       int array1[] = new int[50];
       int array2[] = new int[50];
       Random r = new Random();
       for(int i=0;i<array1.length; i++){
           array1[i]= r.nextInt(1000)+1;
       }
       bubbleSort(array1);
       System.out.println("Array1 elements: "+Arrays.toString(array1));
       for(int i=0;i<array2.length; i++){
           array2[i]= r.nextInt(1000)+1;
       }
       bubbleSort(array2);
       System.out.println("Array1 elements: "+Arrays.toString(array2));
       int newArray[] = merg(array1, array2);
       System.out.println("New array is "+Arrays.toString(newArray));
   }
   public static void bubbleSort(int[] intArray) {
int n = intArray.length;
int temp = 0;
  
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
  
if(intArray[j-1] > intArray[j]){
//swap the elements!
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
  
}
}

}
   public static int[] merg(int[] list1, int[] list2){
       int newlist[] = new int[list1.length + list2.length];
       for(int i=0; i<newlist.length; i++){
           if(i >= list2.length){
               newlist[i] = list2[i-list2.length];
           }
           else{
               newlist[i] = list1[i];
           }
       }
       return newlist;
   }
}

Output:

Array1 elements: [17, 39, 58, 68, 70, 96, 112, 121, 144, 152, 159, 189, 192, 208, 225, 293, 338, 341, 347, 358, 377, 418, 424, 443, 453, 520, 521, 546, 618, 622, 632, 633, 699, 707, 707, 724, 736, 763, 773, 801, 805, 814, 817, 831, 881, 885, 927, 941, 958, 975]
Array1 elements: [5, 10, 22, 29, 31, 38, 66, 70, 73, 143, 167, 181, 195, 209, 219, 230, 249, 286, 298, 336, 381, 393, 394, 407, 410, 412, 455, 456, 473, 476, 544, 579, 587, 596, 630, 633, 635, 649, 650, 677, 719, 773, 802, 803, 816, 831, 881, 907, 929, 986]
New array is [17, 39, 58, 68, 70, 96, 112, 121, 144, 152, 159, 189, 192, 208, 225, 293, 338, 341, 347, 358, 377, 418, 424, 443, 453, 520, 521, 546, 618, 622, 632, 633, 699, 707, 707, 724, 736, 763, 773, 801, 805, 814, 817, 831, 881, 885, 927, 941, 958, 975, 5, 10, 22, 29, 31, 38, 66, 70, 73, 143, 167, 181, 195, 209, 219, 230, 249, 286, 298, 336, 381, 393, 394, 407, 410, 412, 455, 456, 473, 476, 544, 579, 587, 596, 630, 633, 635, 649, 650, 677, 719, 773, 802, 803, 816, 831, 881, 907, 929, 986]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote