Skip to main content
Post Reopened by mdfst13, janos
Move first line into code block and add indent to match; edit to match comment.
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68

public class MergeSortImplementation {

public class MergeSortImplementation {

    public static int[] merge(int[] arrayA, int[] arrayB) {
        int n = arrayA.length + arrayB.length;

        int[] mergedArray = new int[n];

        int x = 0;
        int y = 0;
        int a = 0;
        int b = 0;

        for (int i = 0; i < mergedArray.length; i++) {
            
            // Check if arrayA is out of bounds.
            if (x < arrayA.length) {
                a = arrayA[x];
            } else {
                b = arrayB[y];
                mergedArray[i] = b;
                y++;
                continue;
            }
            
            // Check if arrayB is out of bounds.
            if (y < arrayB.length) {
                b = arrayB[y];
            } else {
                a = arrayA[x];
                mergedArray[i] = a;
                x++;
                continue;
            }

            if (a == b) {
                mergedArray[i] = a;
                i++;
                x++;
                mergedArray[i] = b;
                y++;
            } else if (a < b) {
                mergedArray[i] = a;
                x++;
            } else if (b < a) {
                mergedArray[i] = b;
                y++;
            }
        }

        return mergedArray;
    }

    public static int[] mergeSort(int[] inputArr) {

        int n = inputArr.length;

        // Base Case: Array's length is less than 1 
        if (n <= 1) {
            return inputArr;
        }

        // Creating two arrays to store values
        int[] arrayA = new int[n / 2];
        int[] arrayB = new int[n - (n / 2)];

        // Creating resulting array
        int[] outputArr = new int[n];

        // Initializing arrayA
        for (int i = 0; i < arrayA.length; i++) {
            arrayA[i] = inputArr[i];
        }

        // Initializing arrayB
        for (int i = 0; i < arrayB.length; i++) {
            arrayB[i] = inputArr[i + (n / 2)];
        }

        // Divide array into 2 halves
        arrayA = mergeSort(arrayA);
        arrayB = mergeSort(arrayB);
        outputArr = merge(arrayA, arrayB);

        return outputArr;
    }

    public static void main(String[] args) {

        int[] intArr = {49, 23, 59, 23, 49, 32};

        int[] sortedArray = mergeSort(intArr);

        for (int e : sortedArray) {
            System.out.print(e + " ");
        }
    }
 
}

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what's wrongwhat could be better with the way I'm implementing MergeSort? Or why this coding style is not acceptable?

public class MergeSortImplementation {

public static int[] merge(int[] arrayA, int[] arrayB) {
    int n = arrayA.length + arrayB.length;

    int[] mergedArray = new int[n];

    int x = 0;
    int y = 0;
    int a = 0;
    int b = 0;

    for (int i = 0; i < mergedArray.length; i++) {
        
        // Check if arrayA is out of bounds.
        if (x < arrayA.length) {
            a = arrayA[x];
        } else {
            b = arrayB[y];
            mergedArray[i] = b;
            y++;
            continue;
        }
        
        // Check if arrayB is out of bounds.
        if (y < arrayB.length) {
            b = arrayB[y];
        } else {
            a = arrayA[x];
            mergedArray[i] = a;
            x++;
            continue;
        }

        if (a == b) {
            mergedArray[i] = a;
            i++;
            x++;
            mergedArray[i] = b;
            y++;
        } else if (a < b) {
            mergedArray[i] = a;
            x++;
        } else if (b < a) {
            mergedArray[i] = b;
            y++;
        }
    }

    return mergedArray;
}

public static int[] mergeSort(int[] inputArr) {

    int n = inputArr.length;

    // Base Case: Array's length is less than 1 
    if (n <= 1) {
        return inputArr;
    }

    // Creating two arrays to store values
    int[] arrayA = new int[n / 2];
    int[] arrayB = new int[n - (n / 2)];

    // Creating resulting array
    int[] outputArr = new int[n];

    // Initializing arrayA
    for (int i = 0; i < arrayA.length; i++) {
        arrayA[i] = inputArr[i];
    }

    // Initializing arrayB
    for (int i = 0; i < arrayB.length; i++) {
        arrayB[i] = inputArr[i + (n / 2)];
    }

    // Divide array into 2 halves
    arrayA = mergeSort(arrayA);
    arrayB = mergeSort(arrayB);
    outputArr = merge(arrayA, arrayB);

    return outputArr;
}

public static void main(String[] args) {

    int[] intArr = {49, 23, 59, 23, 49, 32};

    int[] sortedArray = mergeSort(intArr);

    for (int e : sortedArray) {
        System.out.print(e + " ");
    }
}
 
}

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what's wrong with the way I'm implementing MergeSort? Or why this coding style is not acceptable?

public class MergeSortImplementation {

    public static int[] merge(int[] arrayA, int[] arrayB) {
        int n = arrayA.length + arrayB.length;

        int[] mergedArray = new int[n];

        int x = 0;
        int y = 0;
        int a = 0;
        int b = 0;

        for (int i = 0; i < mergedArray.length; i++) {
            
            // Check if arrayA is out of bounds.
            if (x < arrayA.length) {
                a = arrayA[x];
            } else {
                b = arrayB[y];
                mergedArray[i] = b;
                y++;
                continue;
            }
            
            // Check if arrayB is out of bounds.
            if (y < arrayB.length) {
                b = arrayB[y];
            } else {
                a = arrayA[x];
                mergedArray[i] = a;
                x++;
                continue;
            }

            if (a == b) {
                mergedArray[i] = a;
                i++;
                x++;
                mergedArray[i] = b;
                y++;
            } else if (a < b) {
                mergedArray[i] = a;
                x++;
            } else if (b < a) {
                mergedArray[i] = b;
                y++;
            }
        }

        return mergedArray;
    }

    public static int[] mergeSort(int[] inputArr) {

        int n = inputArr.length;

        // Base Case: Array's length is less than 1 
        if (n <= 1) {
            return inputArr;
        }

        // Creating two arrays to store values
        int[] arrayA = new int[n / 2];
        int[] arrayB = new int[n - (n / 2)];

        // Creating resulting array
        int[] outputArr = new int[n];

        // Initializing arrayA
        for (int i = 0; i < arrayA.length; i++) {
            arrayA[i] = inputArr[i];
        }

        // Initializing arrayB
        for (int i = 0; i < arrayB.length; i++) {
            arrayB[i] = inputArr[i + (n / 2)];
        }

        // Divide array into 2 halves
        arrayA = mergeSort(arrayA);
        arrayB = mergeSort(arrayB);
        outputArr = merge(arrayA, arrayB);

        return outputArr;
    }

    public static void main(String[] args) {

        int[] intArr = {49, 23, 59, 23, 49, 32};

        int[] sortedArray = mergeSort(intArr);

        for (int e : sortedArray) {
            System.out.print(e + " ");
        }
    }
}

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what could be better with the way I'm implementing MergeSort? Or why this coding style is not acceptable?

added 64 characters in body
Source Link
public static int[] merge(int[] arrayA, int[] arrayB) {
    int n = arrayA.length + arrayB.length;

    int[] mergedArray = new int[n];

    int x = 0;
    int y = 0;
    int a = 0;
    int b = 0;

    for (int i = 0; i < mergedArray.length; i++) {
        
        // Check if arrayA is out of bounds.
        if (x < arrayA.length) {
            a = arrayA[x];
        } else {
            b = arrayB[y];
            mergedArray[i] = b;
            y++;
            continue;
        }
        
        // Check if arrayB is out of bounds.
        if (y < arrayB.length) {
            b = arrayB[y];
        } else {
            a = arrayA[x];
            mergedArray[i] = a;
            x++;
            continue;
        }

        if (a == b) {
            mergedArray[i] = a;
            i++;
            x++;
            mergedArray[i] = b;
            y++;
        } else if (a < b) {
            mergedArray[i] = a;
            x++;
        } else if (b < a) {
            mergedArray[i] = b;
            y++;
        }
    }

    return mergedArray;
}

public static int[] mergeSort(int[] inputArr) {

    int n = inputArr.length;

    // Base Case: Array's length is less than 1 
    if (n <= 1) {
        return inputArr;
    }

    // Creating two arrays to store values
    int[] arrayA = new int[n / 2];
    int[] arrayB = new int[n - (n / 2)];

    // Creating resulting array
    int[] outputArr = new int[n];

    // Initializing arrayA
    for (int i = 0; i < arrayA.length; i++) {
        arrayA[i] = inputArr[i];
    }

    // Initializing arrayB
    for (int i = 0; i < arrayB.length; i++) {
        arrayB[i] = inputArr[i + (n / 2)];
    }

    // Divide array into 2 halves
    arrayA = mergeSort(arrayA);
    arrayB = mergeSort(arrayB);
    outputArr = merge(arrayA, arrayB);

    return outputArr;
}

public static void main(String[] args) {

    int[] intArr = {49, 23, 59, 23, 49, 32};

    int[] sortedArray = mergeSort(intArr);

    for (int e : sortedArray) {
        System.out.print(e + " ");
    }
}

}
public static int[] merge(int[] arrayA, int[] arrayB) {
    int n = arrayA.length + arrayB.length;

    int[] mergedArray = new int[n];

    int x = 0;
    int y = 0;
    int a = 0;
    int b = 0;

    for (int i = 0; i < mergedArray.length; i++) {
        
        // Check if arrayA is out of bounds.
        if (x < arrayA.length) {
            a = arrayA[x];
        } else {
            mergedArray[i] = b;
            y++;
            continue;
        }
        
        // Check if arrayB is out of bounds.
        if (y < arrayB.length) {
            b = arrayB[y];
        } else {
            mergedArray[i] = a;
            x++;
            continue;
        }

        if (a == b) {
            mergedArray[i] = a;
            i++;
            x++;
            mergedArray[i] = b;
            y++;
        } else if (a < b) {
            mergedArray[i] = a;
            x++;
        } else if (b < a) {
            mergedArray[i] = b;
            y++;
        }
    }

    return mergedArray;
}

public static int[] mergeSort(int[] inputArr) {

    int n = inputArr.length;

    // Base Case: Array's length is less than 1 
    if (n <= 1) {
        return inputArr;
    }

    // Creating two arrays to store values
    int[] arrayA = new int[n / 2];
    int[] arrayB = new int[n - (n / 2)];

    // Creating resulting array
    int[] outputArr = new int[n];

    // Initializing arrayA
    for (int i = 0; i < arrayA.length; i++) {
        arrayA[i] = inputArr[i];
    }

    // Initializing arrayB
    for (int i = 0; i < arrayB.length; i++) {
        arrayB[i] = inputArr[i + (n / 2)];
    }

    // Divide array into 2 halves
    arrayA = mergeSort(arrayA);
    arrayB = mergeSort(arrayB);
    outputArr = merge(arrayA, arrayB);

    return outputArr;
}

public static void main(String[] args) {

    int[] intArr = {49, 23, 59, 23, 49, 32};

    int[] sortedArray = mergeSort(intArr);

    for (int e : sortedArray) {
        System.out.print(e + " ");
    }
}

}
public static int[] merge(int[] arrayA, int[] arrayB) {
    int n = arrayA.length + arrayB.length;

    int[] mergedArray = new int[n];

    int x = 0;
    int y = 0;
    int a = 0;
    int b = 0;

    for (int i = 0; i < mergedArray.length; i++) {
        
        // Check if arrayA is out of bounds.
        if (x < arrayA.length) {
            a = arrayA[x];
        } else {
            b = arrayB[y];
            mergedArray[i] = b;
            y++;
            continue;
        }
        
        // Check if arrayB is out of bounds.
        if (y < arrayB.length) {
            b = arrayB[y];
        } else {
            a = arrayA[x];
            mergedArray[i] = a;
            x++;
            continue;
        }

        if (a == b) {
            mergedArray[i] = a;
            i++;
            x++;
            mergedArray[i] = b;
            y++;
        } else if (a < b) {
            mergedArray[i] = a;
            x++;
        } else if (b < a) {
            mergedArray[i] = b;
            y++;
        }
    }

    return mergedArray;
}

public static int[] mergeSort(int[] inputArr) {

    int n = inputArr.length;

    // Base Case: Array's length is less than 1 
    if (n <= 1) {
        return inputArr;
    }

    // Creating two arrays to store values
    int[] arrayA = new int[n / 2];
    int[] arrayB = new int[n - (n / 2)];

    // Creating resulting array
    int[] outputArr = new int[n];

    // Initializing arrayA
    for (int i = 0; i < arrayA.length; i++) {
        arrayA[i] = inputArr[i];
    }

    // Initializing arrayB
    for (int i = 0; i < arrayB.length; i++) {
        arrayB[i] = inputArr[i + (n / 2)];
    }

    // Divide array into 2 halves
    arrayA = mergeSort(arrayA);
    arrayB = mergeSort(arrayB);
    outputArr = merge(arrayA, arrayB);

    return outputArr;
}

public static void main(String[] args) {

    int[] intArr = {49, 23, 59, 23, 49, 32};

    int[] sortedArray = mergeSort(intArr);

    for (int e : sortedArray) {
        System.out.print(e + " ");
    }
}

}
added 44 characters in body
Source Link

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what's wrong with the way I'm implementing mergeSortMergeSort? Or why this coding style is not acceptable?

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what's wrong with the way I'm implementing mergeSort?

It looks very different from others. This is because I am creating an extra array and returning another. Other than memory/space inefficiency can someone tell me what's wrong with the way I'm implementing MergeSort? Or why this coding style is not acceptable?

Post Closed as "Needs details or clarity" by Jamal
Source Link
Loading