// MergeSortTest.java // Sorting an array with merge sort. import java.security.SecureRandom; import java.util.Arrays; public class MergeSortTest { // calls recursive sortArray method to begin merge sorting public static void mergeSort(int[] data) { sortArray(data, 0, data.length - 1); // sort entire array } // splits array, sorts subarrays and merges subarrays into sorted array private static void sortArray(int[] data, int low, int high) { // test base case; size of array equals 1 if ((high - low) >= 1) { // if not base case int middle1 = (low + high) / 2; // calculate middle of array int middle2 = middle1 + 1; // calculate next element over // split array in half; sort each half (recursive calls) sortArray(data, low, middle1); // first half of array sortArray(data, middle2, high); // second half of array // merge two sorted arrays after split calls return merge (data, low, middle1, middle2, high); } } // merge two sorted subarrays into one sorted subarray private static void merge(int[] data, int left, int middle1, int middle2, int right) { int leftIndex = left; // index into left subarray int rightIndex = middle2; // index into right subarray int combinedIndex = left; // index into temporary working array int[] combined = new int[data.length]; // working array // merge arrays until reaching end of either while (leftIndex <= middle1 && rightIndex <= right) { // place smaller of two current elements into result // and move to next space in arrays if (data[leftIndex] <= data[rightIndex]) { combined[combinedIndex++] = data[leftIndex++]; } else { combined[combinedIndex++] = data[rightIndex++]; } } // if left array is empty if (leftIndex == middle2) { // copy in rest of right array while (rightIndex <= right) { combined[combinedIndex++] = data[rightIndex++]; } } else { // right array is empty // copy in rest of left array while (leftIndex <= middle1) { combined[combinedIndex++] = data[leftIndex++]; } } // copy values back into original array for (int i = left; i <= right; i++) { data[i] = combined[i]; } // output merged array } // method to output certain values in array public static void main(String[] args) { SecureRandom generator = new SecureRandom(); // create unordered array of 10 random ints int[] data = generator.ints(10, 10, 91).toArray(); System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data)); mergeSort(data); // sort array System.out.printf("Sorted array: %s%n", Arrays.toString(data)); } } //we get 2000 - 2001 = -1... A Negative number, that means m1 < m2. //If we get a positive number then m
// MergeSortTest.java
// Sorting an array with merge sort.
import java.security.SecureRandom;
import java.util.Arrays;
public class MergeSortTest {
// calls recursive sortArray method to begin merge sorting
public static void mergeSort(int[] data) {
sortArray(data, 0, data.length - 1); // sort entire array
}
// splits array, sorts subarrays and merges subarrays into sorted array
private static void sortArray(int[] data, int low, int high) {
// test base case; size of array equals 1
if ((high - low) >= 1) { // if not base case
int middle1 = (low + high) / 2; // calculate middle of array
int middle2 = middle1 + 1; // calculate next element over
// split array in half; sort each half (recursive calls)
sortArray(data, low, middle1); // first half of array
sortArray(data, middle2, high); // second half of array
// merge two sorted arrays after split calls return
merge (data, low, middle1, middle2, high);
}
}
// merge two sorted subarrays into one sorted subarray
private static void merge(int[] data, int left, int middle1,
int middle2, int right) {
int leftIndex = left; // index into left subarray
int rightIndex = middle2; // index into right subarray
int combinedIndex = left; // index into temporary working array
int[] combined = new int[data.length]; // working array
// merge arrays until reaching end of either
while (leftIndex <= middle1 && rightIndex <= right) {
// place smaller of two current elements into result
// and move to next space in arrays
if (data[leftIndex] <= data[rightIndex]) {
combined[combinedIndex++] = data[leftIndex++];
}
else {
combined[combinedIndex++] = data[rightIndex++];
}
}
// if left array is empty
if (leftIndex == middle2) {
// copy in rest of right array
while (rightIndex <= right) {
combined[combinedIndex++] = data[rightIndex++];
}
}
else { // right array is empty
// copy in rest of left array
while (leftIndex <= middle1) {
combined[combinedIndex++] = data[leftIndex++];
}
}
// copy values back into original array
for (int i = left; i <= right; i++) {
data[i] = combined[i];
}
// output merged array
}
// method to output certain values in array
public static void main(String[] args) {
SecureRandom generator = new SecureRandom();
// create unordered array of 10 random ints
int[] data = generator.ints(10, 10, 91).toArray();
System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data));
mergeSort(data); // sort array
System.out.printf("Sorted array: %s%n", Arrays.toString(data));
}
}
//we get 2000 - 2001 = -1... A Negative number, that means m1 < m2.
//If we get a positive number then m
![### Java Project for Sorting Student Data
**Objective:**
Create a Java project that prints both unsorted and sorted versions of a sample array consisting of at least 5 Comparable student objects. Verify the output to ensure the merge sort functions correctly.
**Project Structure:**
The project should have two Java files.
1. **Student.java**
- **Purpose:** Implement the `Comparable<Student>` interface.
- **Details:**
- Define necessary members: `"FirstName"`, `"LastName"`, and `"StudentID"`, all as `String` data types.
- Implement necessary methods such as `compareTo()` and `toString()`.
- If needed, include `getters` and `setters` methods.
2. **MergeSortTest.java**
- **Purpose:** Serve as the driver class (provided within the question).
- **Content:**
- Contains the `main()` method.
- Implements the merge sort algorithm.
- Demonstrates the merge sort algorithm using a mock sample of comparable student array data.
**Sorting Options:**
Choose to merge sort your sample student array based on:
- `"LastName"`
- `"FirstName"` or `"StudentID"`](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F33367630-baef-4794-a3c7-3f7d1dfa9a18%2F492de4b4-636e-4d14-841c-02259300cd28%2Fe9jgh59_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)