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 832755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:24:40+00:00 2026-05-15T04:24:40+00:00

Hi I have a question about Batcher’s odd-even-merge sort. I have the following code:

  • 0

Hi I have a question about Batcher’s odd-even-merge sort. I have the following code:

public class Batcher {
  public static void batchsort(int a[], int l, int r) {
    int n = r-l+1;

    for (int p=1; p<n; p+=p)
      for (int k=p; k>0; k/=2)
        for (int j=k%p; j+k<n; j+=(k+k))
          for (int i=0; i<n-j-k; i++)
            if ((j+i)/(p+p) == (j+i+k)/(p+p))
              exch(a, l+j+i, l+j+i+k);
  }

  public static void main(String[] args) {
    int a[] = new int[] {2, 4, 3, 4, 6, 5, 3};

    batchsort(a, 0, a.length - 1);

    for (int i=0; i<a.length; i++) {
      System.out.println(a[i]);
    }
  }

  public static void exch(int a[], int i, int j) {
    int t = a[i];
    a[i] = a[j];
    a[j] = t;
  }
}

I will provide some comments about the code’s function:
It’s divided into phases indexed by variable p the last phase is when p==n is batchers odd-even-merge the next-to-phase is when p==n/2 is odd-even-merge with the first stage and all comparators that cross n/2 eliminated the third-to-last phase is when p==n/4 the odd-even-merge with the first two stages and all comparator that cross any multiple of n/4 eliminated and so forth.

Results are:

3
3
4
4
5
2
6

What have I missed?
What is wrong?

  • 1 1 Answer
  • 1 View
  • 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-15T04:24:40+00:00Added an answer on May 15, 2026 at 4:24 am

    I don’t know the algorithm but you need to compare values in a in batchsort before switching them, e.g.

    if ((j + i) / (p + p) == (j + i + k) / (p + p))
    {
        if (a[l + j + i] > a[l + j + i + k])
        {
            exch(a, l + j + i, l + j + i + k);
        }
    }
    

    or that might be more readable if you use temp variables for the combined indices, e.g.

    if ((j + i) / (p + p) == (j + i + k) / (p + p))
    {
        int i1 = l + j + i;
        int i2 = l + j + i + k;
        if (a[i1] > a[i2])
        {
            exch(a, i1, i2);
        }
    }
    

    but the commenters above are right – this really isn’t written very readably at all. You should try to make sure that equal braces have the same indent, that nested for()s have more indent than their containing for, etc., and you should probably pick more descriptive variable names too.

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

Sidebar

Related Questions

I have a question about how GC works in Java. Consider the following code:
I have question about throw. How will the throw work in the following code?
I have question about removing element from QList. myclass.h: class node2D : public QObject
I have a question about arithmetic behaviour in R. Regard the following piece of
I have question about my code which does the Hoare Partition Method. Here is
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I have question about integrate Open office with PHP. I want make mail merge
I have question about try, catch and finally in Java. Consider the following scenario:
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
I have a question about his article . I converted the source code to

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.