mirror of https://github.com/evanferrao/dsa
improve logic of merge() function
This commit is contained in:
parent
296ac7e47a
commit
caf8723cba
|
|
@ -1,5 +1,6 @@
|
||||||
// write a code of mergesort using function in java
|
// write a code of mergesort using function in java
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
public class MergeSort{
|
public class MergeSort{
|
||||||
// use notation beginning, mid and end
|
// use notation beginning, mid and end
|
||||||
|
|
@ -34,52 +35,87 @@ public class MergeSort{
|
||||||
merge(arr, beginning, mid, end);
|
merge(arr, beginning, mid, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static void merge(int arr[], int beginning, int mid, int end){
|
// static void merge(int arr[], int beginning, int mid, int end){
|
||||||
// find the size of two subarrays to be merged
|
// // find the size of two subarrays to be merged
|
||||||
int n1 = mid - beginning + 1;
|
// int n1 = mid - beginning + 1;
|
||||||
int n2 = end - mid;
|
// int n2 = end - mid;
|
||||||
|
|
||||||
// create temp arrays
|
// // create temp arrays
|
||||||
int L[] = new int[n1];
|
// int L[] = new int[n1];
|
||||||
int R[] = new int[n2];
|
// int R[] = new int[n2];
|
||||||
|
|
||||||
// copy data to temp arrays
|
// // copy data to temp arrays
|
||||||
for(int i = 0; i < n1; i++){
|
// for(int i = 0; i < n1; i++){
|
||||||
L[i] = arr[beginning + i];
|
// L[i] = arr[beginning + i];
|
||||||
}
|
// }
|
||||||
for(int j = 0; j < n2; j++){
|
// for(int j = 0; j < n2; j++){
|
||||||
R[j] = arr[mid + 1 + j];
|
// R[j] = arr[mid + 1 + j];
|
||||||
}
|
// }
|
||||||
|
|
||||||
// merge the temp arrays
|
// // merge the temp arrays
|
||||||
// initial indexes of first and second subarrays
|
// // initial indexes of first and second subarrays
|
||||||
int i = 0, j = 0;
|
// int i = 0, j = 0;
|
||||||
|
|
||||||
// initial index of merged subarray array
|
// // initial index of merged subarray array
|
||||||
int k = beginning;
|
// int k = beginning;
|
||||||
while(i < n1 && j < n2){
|
// while(i < n1 && j < n2){
|
||||||
if(L[i] <= R[j]){
|
// if(L[i] <= R[j]){
|
||||||
arr[k] = L[i];
|
// arr[k] = L[i];
|
||||||
i++;
|
// i++;
|
||||||
}else{
|
// }else{
|
||||||
arr[k] = R[j];
|
// arr[k] = R[j];
|
||||||
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++;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
static void merge(int[] arr, int low, int mid, int high) {
|
||||||
|
ArrayList<Integer> temp = new ArrayList<>(); // temporary array
|
||||||
|
int left = low; // starting index of left half of arr
|
||||||
|
int right = mid + 1; // starting index of right half of arr
|
||||||
|
|
||||||
|
//storing elements in the temporary array in a sorted manner//
|
||||||
|
|
||||||
|
while (left <= mid && right <= high) {
|
||||||
|
if (arr[left] <= arr[right]) {
|
||||||
|
temp.add(arr[left]);
|
||||||
|
left++;
|
||||||
|
} else {
|
||||||
|
temp.add(arr[right]);
|
||||||
|
right++;
|
||||||
}
|
}
|
||||||
k++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy remaining elements of L[] if any
|
// if elements on the left half are still left //
|
||||||
while(i < n1){
|
|
||||||
arr[k] = L[i];
|
while (left <= mid) {
|
||||||
i++;
|
temp.add(arr[left]);
|
||||||
k++;
|
left++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy remaining elements of R[] if any
|
// if elements on the right half are still left //
|
||||||
while(j < n2){
|
while (right <= high) {
|
||||||
arr[k] = R[j];
|
temp.add(arr[right]);
|
||||||
j++;
|
right++;
|
||||||
k++;
|
}
|
||||||
|
|
||||||
|
// transfering all elements from temporary to arr //
|
||||||
|
for (int i = low; i <= high; i++) {
|
||||||
|
arr[i] = temp.get(i - low);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class VisualMergeSort {
|
public class VisualMergeSort {
|
||||||
static String RESET = "\u001B[0m";
|
static String RESET = "\u001B[0m";
|
||||||
static String RED = "\u001B[31m";
|
static String RED = "\u001B[31m";
|
||||||
|
|
@ -56,71 +58,121 @@ public class VisualMergeSort {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to merge two sorted subarrays with level parameter
|
// Method to merge two sorted subarrays with level parameter
|
||||||
public static void merge(int[] array, int left, int mid, int right, int currentLevel) {
|
// public static void merge(int[] array, int left, int mid, int right, int currentLevel) {
|
||||||
|
// System.out.printf("%sMerging subarrays from index %d to %d and %d to %d with level %d%s\n", UNDERLINE, left, mid, mid + 1, right, currentLevel, RESET);
|
||||||
|
// System.out.printf("Subarrays before merge: ");
|
||||||
|
// printArray(array, left, mid);
|
||||||
|
// System.out.printf("and ");
|
||||||
|
// printArray(array, mid + 1, right);
|
||||||
|
// System.out.println();
|
||||||
|
|
||||||
|
// // Sizes of the two subarrays to merge
|
||||||
|
// int n1 = mid - left + 1;
|
||||||
|
// int n2 = right - mid;
|
||||||
|
|
||||||
|
// // Create temporary arrays
|
||||||
|
// int[] leftArray = new int[n1];
|
||||||
|
// int[] rightArray = new int[n2];
|
||||||
|
|
||||||
|
// // Copy data to temporary arrays
|
||||||
|
// for (int i = 0; i < n1; i++) {
|
||||||
|
// leftArray[i] = array[left + i];
|
||||||
|
// }
|
||||||
|
// for (int j = 0; j < n2; j++) {
|
||||||
|
// rightArray[j] = array[mid + 1 + j];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Display temporary arrays
|
||||||
|
// System.out.printf("Left array: ");
|
||||||
|
// printArray(leftArray);
|
||||||
|
// System.out.printf("Right array: ");
|
||||||
|
// printArray(rightArray);
|
||||||
|
|
||||||
|
// // Merge the temporary arrays back into the original array
|
||||||
|
// int i = 0, j = 0;
|
||||||
|
// int k = left;
|
||||||
|
// while (i < n1 && j < n2) {
|
||||||
|
// if (leftArray[i] <= rightArray[j]) {
|
||||||
|
// array[k] = leftArray[i];
|
||||||
|
// i++;
|
||||||
|
// } else {
|
||||||
|
// array[k] = rightArray[j];
|
||||||
|
// j++;
|
||||||
|
// }
|
||||||
|
// System.out.printf("Placing %d at index %d\n", array[k], k);
|
||||||
|
// k++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Copy remaining elements of leftArray[], if any
|
||||||
|
// while (i < n1) {
|
||||||
|
// array[k] = leftArray[i];
|
||||||
|
// System.out.printf("Placing %d at index %d\n", array[k], k);
|
||||||
|
// i++;
|
||||||
|
// k++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Copy remaining elements of rightArray[], if any
|
||||||
|
// while (j < n2) {
|
||||||
|
// array[k] = rightArray[j];
|
||||||
|
// System.out.printf("Placing %d at index %d\n", array[k], k);
|
||||||
|
// j++;
|
||||||
|
// k++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Display the merged array
|
||||||
|
// System.out.printf("Merged array from index %d to %d: ", left, right);
|
||||||
|
// printArray(array, left, right);
|
||||||
|
// System.out.println();
|
||||||
|
// }
|
||||||
|
|
||||||
|
public static void merge(int[] arr, int low, int mid, int high, int currentLevel) {
|
||||||
|
ArrayList<Integer> temp = new ArrayList<>(); // temporary array
|
||||||
|
int left = low; // starting index of left half of arr
|
||||||
|
int right = mid + 1; // starting index of right half of arr
|
||||||
|
|
||||||
System.out.printf("%sMerging subarrays from index %d to %d and %d to %d with level %d%s\n", UNDERLINE, left, mid, mid + 1, right, currentLevel, RESET);
|
System.out.printf("%sMerging subarrays from index %d to %d and %d to %d with level %d%s\n", UNDERLINE, left, mid, mid + 1, right, currentLevel, RESET);
|
||||||
System.out.printf("Subarrays before merge: ");
|
System.out.printf("Subarrays before merge: ");
|
||||||
printArray(array, left, mid);
|
printArray(arr, left, mid);
|
||||||
printArray(array, mid + 1, right);
|
System.out.printf("and ");
|
||||||
|
printArray(arr, mid + 1, right);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
// Sizes of the two subarrays to merge
|
// Storing elements in the temporary array in a sorted manner
|
||||||
int n1 = mid - left + 1;
|
while (left <= mid && right <= high) {
|
||||||
int n2 = right - mid;
|
if (arr[left] <= arr[right]) {
|
||||||
|
temp.add(arr[left]);
|
||||||
// Create temporary arrays
|
left++;
|
||||||
int[] leftArray = new int[n1];
|
|
||||||
int[] rightArray = new int[n2];
|
|
||||||
|
|
||||||
// Copy data to temporary arrays
|
|
||||||
for (int i = 0; i < n1; i++) {
|
|
||||||
leftArray[i] = array[left + i];
|
|
||||||
}
|
|
||||||
for (int j = 0; j < n2; j++) {
|
|
||||||
rightArray[j] = array[mid + 1 + j];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display temporary arrays
|
|
||||||
System.out.printf("Left array: ");
|
|
||||||
printArray(leftArray);
|
|
||||||
System.out.printf("Right array: ");
|
|
||||||
printArray(rightArray);
|
|
||||||
|
|
||||||
// Merge the temporary arrays back into the original array
|
|
||||||
int i = 0, j = 0;
|
|
||||||
int k = left;
|
|
||||||
while (i < n1 && j < n2) {
|
|
||||||
if (leftArray[i] <= rightArray[j]) {
|
|
||||||
array[k] = leftArray[i];
|
|
||||||
i++;
|
|
||||||
} else {
|
} else {
|
||||||
array[k] = rightArray[j];
|
temp.add(arr[right]);
|
||||||
j++;
|
right++;
|
||||||
}
|
}
|
||||||
System.out.printf("Placing %d at index %d\n", array[k], k);
|
System.out.printf("Placing %d at index %d\n", temp.get(temp.size() - 1), temp.size() - 1);
|
||||||
k++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy remaining elements of leftArray[], if any
|
// If elements on the left half are still left
|
||||||
while (i < n1) {
|
while (left <= mid) {
|
||||||
array[k] = leftArray[i];
|
temp.add(arr[left]);
|
||||||
System.out.printf("Placing %d at index %d\n", array[k], k);
|
System.out.printf("Placing %d at index %d\n", temp.get(temp.size() - 1), temp.size() - 1);
|
||||||
i++;
|
left++;
|
||||||
k++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy remaining elements of rightArray[], if any
|
// If elements on the right half are still left
|
||||||
while (j < n2) {
|
while (right <= high) {
|
||||||
array[k] = rightArray[j];
|
temp.add(arr[right]);
|
||||||
System.out.printf("Placing %d at index %d\n", array[k], k);
|
System.out.printf("Placing %d at index %d\n", temp.get(temp.size() - 1), temp.size() - 1);
|
||||||
j++;
|
right++;
|
||||||
k++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the merged array
|
// Transferring all elements from temporary to arr
|
||||||
System.out.printf("Merged array from index %d to %d: ", left, right);
|
for (int i = low; i <= high; i++) {
|
||||||
printArray(array, left, right);
|
arr[i] = temp.get(i - low);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.printf("Merged array from index %d to %d: ", low, high); // Correct indices
|
||||||
|
printArray(arr, low, high); // Correct indices
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Utility method to print the array
|
// Utility method to print the array
|
||||||
public static void printArray(int[] array) {
|
public static void printArray(int[] array) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue