Please help me with this algorithm assignment using Java
Write 2 functions to calculate the MSS of a given array one with running time of O(n) and one with O(nlogn) .Request the user to enter a positive integer, and call it n.
1.Generate n random integers between -100 to 100 and save them in array a.
2.Print the generated array and the outputsof your two functions
Solution:
code:
package abc
import java.util.Random;
import java.util.Scanner;
public class MSS {
/**
* @param args
*/
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
Random random = new Random();
int low = -100;
int high = 100;
System.out.println("Enter positive integer: ");
int n = userInput.nextInt();
int arr[] = new int[n];
for (int i=0;i<n;i++) {
int randomNumber = random.nextInt(high - low) + low;
arr[i] = randomNumber;
}
System.out.println("Original Array: ");
printArray(arr);
sort(arr,0,arr.length-1);
userInput.close();
System.out.println("Sorted Array: ");
printArray(arr);
}
public static void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void merge(int arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
Output:

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Please help me with this algorithm assignment using Java Write 2 functions to calculate the MSS...
Please use the algorithm to generate 1000 random numbers in
JAVA. (Print every 50th)
integer array lDon real array (aiim eo ← any integer such that l < 10 < 231-1 for i = 1 to n do 4 ← remainder when /.til IS divided by 2. 31-1 231 end for
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
Using Java, write a program that teachers can use to enter and calculate grades of individual students by utilizing an array, Scanner object, casting, and the print method. First, let the user choose the size for the integer array by using a Scanner object. The integer array will hold individual assignment grades of a fictional student. Next, use a loop to populate the integer array with individual grades. Make sure each individual grade entered by the user fills just one...
I need help with this in JAVA. please help me. Thank
you.
In this project. you are given a unimodal array of n integer and your task is to find the maximum integer in the array in theta(logn) time. An unimodal array of integers is an array with entries that monotonically increase up to the maximum integer value and then monotonically decrease for the rest of the array. For example: (2, 5, 8, 9, 12, 15, 21, 17, 10, 4)...
2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...
In java create a program to test and measure an actual running time of Insertion sort and Merge sort on your machine. Each sorting algorithm must be a separate method: a. Randomly generate 100 integers between 0 and 500. b. Store those integers in an array and display them on screen. c. Using each sorting algorithm, sort numbers and display the sorted numbers. The array must be passed as an argument to call a method. d. For each sorting algorithm,...
C++ Program Int Main First Please Write one program that does the following: 1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average. a. Declare an integer array with the name of “grades” of size 10 in the main function. b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array. i. The function will receive...
Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...
Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...
Write a program in either C++ or Java meeting these requirements Description: In this program, we assume that our data comes in dynamically. We need to maintain our data in a heap, after every insertion and deletion. We also need to handle the underlying array dynamically. whenever we detect an overflow in our array, we will create a new array with size doubling the previous size. Requirements: 1. (Dynamic Data) when we generate the data, we simulate dynamic data. We...