Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:42:10+00:00 2026-05-29T17:42:10+00:00

void sort(int a[], int b[], int m, int n) { int e = m

  • 0
void sort(int a[], int b[], int m, int n)
    {
        int e = m + n;
        while(m >=0 && n >=0)
        {
            if(a[m] > b[n])
            {
                a[e] = a[m];
                m--;
                e--;
            }
            else if(a[e] < b[n])
            {
                a[e] = b[n];
                n--;
                e--;
            }
            else
            {
                a[e] = b[n];
                e--;
                n--;
                m--;
            }
        }
    }

public static void main(String[] args) {
        SortSorted obj = new SortSorted();
        int a[] = new int [6];
        int b[] = new int [3];
        a[0] = 1;
        a[1] = 2;
        a[2] = 3;
        b[0] = 2;
        b[1] = 7;
        b[2] = 8;
        obj.sort(a,b, 2, 2);
    }

I get the output as 1 2 3 7 8 0 instead of 1 2 2 3 7 8 with and without adding the 3rd else condition.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:42:15+00:00Added an answer on May 29, 2026 at 5:42 pm

    You start e too low, it should be m + n + 1.

    Think about it, for two three-element arrays, m and n are both two. That means that you’ll start at four with m + n whereas, with a six-element result, you should be starting at five.

    This is a relatively simple off-by-one error.

    You can also fix the other problem of losing values when they’re equal by simply ignoring the equality. Choose a if it’s greater than or equal, otherwise choose b.

    And your loop continuation logic is wrong, which you would see if you exhausted a first (use a = {2, 2, 3} and b = {1, 7, 8} to see what I mean). You only continue if both a and b have elements left. You should continue while either of them have elements left.

    You can fix this by leaving that loop as-is but adding two other loops (only one of which will actually do anything) to exhaust the other list.

    As support, I provide the following C code (since I’m faster with that than Java, but the sort routine itself should be pretty much identical):

    #include <stdio.h>
    
    void sort (int a[], int b[], int m, int n) {
        // Start at correct offset for target array.
    
        int e = m + n + 1;
    
        // Until one list empty, choose the correct value.
    
        while (m >= 0 && n >= 0)
            if (a[m] >= b[n])
                a[e--] = a[m--];
            else
                a[e--] = b[n--];
    
        // If b was empty, just transfer a.
    
        while (m >= 0)
            a[e--] = a[m--];
    
        // If a was empty, just transfer b.
    
        while (n >= 0)
            a[e--] = b[n--];
    }
    

    And some test code:

    int main(void) {
        int a[6] = {2,2,3};
        int b[] = {1,7,8};
        sort (a, b, 2, 2);
        printf ("%d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
        return 0;
    }
    

    The output of this is:

    1 2 2 3 7 8
    

    as expected.


    And the equivalent Java code:

    class Test {
        static void sort (int a[], int b[], int m, int n) {
            // Start at correct offset for target array.
    
            int e = m + n + 1;
    
            // Until one list empty, choose the correct value.
    
            while (m >= 0 && n >= 0)
                if (a[m] >= b[n])
                    a[e--] = a[m--];
                else
                    a[e--] = b[n--];
    
            // If b was empty, just transfer a.
    
            while (m >= 0)
                a[e--] = a[m--];
    
            // If a was empty, just transfer b.
    
            while (n >= 0)
                a[e--] = b[n--];
        }
    

    along with its test suite:

        static public void main(String[] args) {
            int a[] = new int[6];
            a[0] = 2; a[1] = 2; a[2] = 3;
            int b[] = new int[3];
            b[0] = 1; b[1] = 7; b[2] = 8;
            sort (a, b, 2, 2);
            for (int i = 0; i < 6; i++)
                System.out.println (a[i]);
        }
    }  
    

    In fact, since you’re writing to a anyway, you can actually leave out that middle loop (the while (m >= 0) one). That’s because, in that situation, you’re simply transferring elements to themselves.

    I’ll leave it in since it becomes important if the array you’re writing to is not in-place but you can remove it if you wish for your particular situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Main { public static void main (String args[]) { int nums[]= {2,
I noticed the specificaition for Collections.sort: public static <T> void sort(List<T> list, Comparator<? super
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
The following is my implementation of merge sort. private static void mergeSort(int[] a, int
class Sort { int[] arr; int counter=0; //Constructor public Sort() { arr = new
Can someone clearly explain me how these functions of heap sort are working?? void
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
void some_func(int param = get_default_param_value());
void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.