Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6vDJ5fWY5uEZaJFNi
90 lines
3.3 KiB
Java
90 lines
3.3 KiB
Java
import java.io.*;
|
|
|
|
class MergeSort {
|
|
|
|
/**
|
|
* Merges two adjacent sorted sub-arrays of arr[] into a single sorted section.
|
|
*
|
|
* @param arr the array containing both sub-arrays
|
|
* @param leftStart starting index of the left sub-array
|
|
* @param rightStart starting index of the right sub-array
|
|
* @param rightEnd ending index (inclusive) of the right sub-array
|
|
*/
|
|
static void merge(int[] arr, int leftStart, int rightStart, int rightEnd) {
|
|
int[] tempArray = new int[arr.length]; // temporary buffer for merged output
|
|
int leftPointer = leftStart; // pointer into the left sub-array
|
|
int rightPointer = rightStart; // pointer into the right sub-array
|
|
int tempIndex = -1; // current write position in tempArray
|
|
|
|
// Compare elements from both halves and place the smaller one into tempArray
|
|
while (leftPointer <= rightStart - 1 && rightPointer <= rightEnd) {
|
|
if (arr[leftPointer] < arr[rightPointer]) {
|
|
tempIndex++;
|
|
tempArray[tempIndex] = arr[leftPointer];
|
|
leftPointer++;
|
|
} else {
|
|
tempIndex++;
|
|
tempArray[tempIndex] = arr[rightPointer];
|
|
rightPointer++;
|
|
}
|
|
}
|
|
|
|
// Copy any remaining elements from the left half
|
|
if (leftPointer > rightStart - 1) {
|
|
for (int i = rightPointer; i <= rightEnd; i++) {
|
|
tempIndex++;
|
|
tempArray[tempIndex] = arr[i];
|
|
}
|
|
} else {
|
|
// Copy any remaining elements from the right half
|
|
for (int i = leftPointer; i <= rightStart - 1; i++) {
|
|
tempIndex++;
|
|
tempArray[tempIndex] = arr[i];
|
|
}
|
|
}
|
|
|
|
// Copy the merged result back into the original array
|
|
for (int i = 0; i <= tempIndex; i++) {
|
|
arr[leftStart + i] = tempArray[i];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Recursively sorts arr[leftIndex..rightIndex] using the merge sort algorithm.
|
|
*
|
|
* @param arr the array to sort
|
|
* @param leftIndex starting index of the portion to sort
|
|
* @param rightIndex ending index (inclusive) of the portion to sort
|
|
*/
|
|
static void mergeSort(int[] arr, int leftIndex, int rightIndex) {
|
|
if (leftIndex < rightIndex) {
|
|
int midIndex = (leftIndex + rightIndex) / 2;
|
|
|
|
mergeSort(arr, leftIndex, midIndex); // sort the left half
|
|
mergeSort(arr, midIndex + 1, rightIndex); // sort the right half
|
|
merge(arr, leftIndex, midIndex + 1, rightIndex); // merge both halves
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
System.out.println("Enter the number of elements:");
|
|
int elementCount = Integer.parseInt(reader.readLine());
|
|
|
|
int[] arr = new int[elementCount];
|
|
|
|
for (int i = 0; i < elementCount; i++) {
|
|
System.out.println("Enter element " + (i + 1) + ":");
|
|
arr[i] = Integer.parseInt(reader.readLine());
|
|
}
|
|
|
|
mergeSort(arr, 0, elementCount - 1);
|
|
|
|
System.out.println("Sorted array:");
|
|
for (int i = 0; i < elementCount; i++) {
|
|
System.out.println(arr[i]);
|
|
}
|
|
}
|
|
}
|