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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:26:10+00:00 2026-06-17T17:26:10+00:00

I am trying to code some pseudocode that was given on an assignment –

  • 0

I am trying to code some pseudocode that was given on an assignment – however the coding portion I’m doing now is not part of the assignment and is just for fun.

One part of the pseudocode says “swap elements of the array”. In my code I have to do this in two places. However I was wondering if there’s a way to do this by creating only one method, i.e. creating another method that simply swaps them and saves the extra lines of code I used each time I wanted to run it.

The problem is, if I create a brand new method outside of this one, I’ll have to send in the array as a parameter and take it out as well, and I’m afraid this will be less efficient (obviously no big deal here but I’m trying to learn for future larger projects).

Here is my code containing the repeated “swap” method lines.

public int[] myAlgorithm(int[] arrayOfInts, int size){
    boolean done = true;
    int j= 0;

    while (j <= n-2){
        if (arrayOfInts[j] > arrayOfInts[j+1]){
            int tempHolder = arrayOfInts[j];
            arrayOfInts[j] = arrayOfInts[j+1];
            arrayOfInts[j+1] = tempHolder;
            done = false;
        }
        j = j + 1;
    }
    j = size - 1;
    while (j >= 1){
        if (arrayOfInts[j] <= arrayOfInts[j-1]){
            int tempHolder = arrayOfInts[j];
            arrayOfInts[j] = arrayOfInts[j+1];
            arrayOfInts[j+1] = tempHolder;
            done = false;
        }
        j--;
    }
    if (!done)
        myAlgorithm(arrayOfInts, size)
    else
        return arrayOfInts;
}
  • 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-06-17T17:26:11+00:00Added an answer on June 17, 2026 at 5:26 pm

    I’ll have to send in the array as a parameter and take it out as well

    You only need to “send it” and work on the array directly. In practice, passing a parameter to a method is a very cheap operation which, if called often enough, will likely be optimised by the compiler anyway so you should not worry about it too much (unless you prove that it is a performance penalty by profiling your application).

    In your case, you could have a method such as:

    public void swap(int[] arrayOfInts, int i, int j) {
        int tempHolder = arrayOfInts[i];
        arrayOfInts[i] = arrayOfInts[j];
        arrayOfInts[j] = tempHolder;
    }
    

    and you would call it:

    swap(arrayOfInts, j, j+1);
    

    Why does it work?

    Java passes arguments by value, but in the case of objects (i.e. non-primitive types, including arrays), the value that is passed is the reference to the object. In other words, the argument to the swap method is a reference to the same array as in the calling code, so you can work on that array directly, without having to send it back to the calling method.

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

Sidebar

Related Questions

I was just trying out some code, not a experienced coder. I implemented(at least
There is some code that I'm trying to convert from IList to IEnumerable :
i have some code that i am trying to recompile and understand, but i
I am currently trying to write some code that will accept some FTP details,
I am trying to port some code form java do F# that generates a
I am trying to write some code that will generate accurate .proto files from
I'm trying to create an application that triggers some code when the financial markets
I'm trying to code some stuff for a game but I keep getting error
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I am trying to generate some code using Acceleo. I am constructing my model

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.