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

  • Home
  • SEARCH
  • 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 8902229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:33:33+00:00 2026-06-15T01:33:33+00:00

my bubble sort code is only swapping the 1st array item. All other items

  • 0

my bubble sort code is only swapping the 1st array item. All other items are left as 0. I think my nested loops are wrong or something I haven’t been able to diagnose it correctly yet. so here is my code.

    public void swap(int i, int j) {

    int temp;

    temp = i;
    i = j;
    j = temp;
}


public void sortArray(int [] sourceArray, int [] targetArray, int allArraySize){

    for(int i = 0; i < allArraySize; i++) {
        targetArray[i] = sourceArray[i];
        for (i = 0; i < allArraySize; i++) {
            for(int j = i+1; j < allArraySize;j++) {
                if(targetArray[i] > targetArray[j]) {
                    swap(i, j);
                }
            }

        }

    }
}

thanks so much for the feedback. I am (obviously) new to programming. I have changed my code to this.

public void sortArray(int [] sourceArray, int [] targetArray, int allArraySize){
    int temp;

    for(int i = 0; i < allArraySize; i++) {
        targetArray[i] = sourceArray[i];
        for (i = 0; i < allArraySize; i++) {
            for(int j = i+1; j < allArraySize;j++) {
                if(targetArray[i] > targetArray[j]) {
                    temp = targetArray[i];
                    targetArray[i] = targetArray[j];
                    targetArray[j] = temp;
                }
            }

        }

    }
}

and the result is still only swapping 1 item but now it is the last 1. if anyone is still able to help I would greatly appreciate it.

  • 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-15T01:33:36+00:00Added an answer on June 15, 2026 at 1:33 am

    The reason is explained in detail in this post. In summary, java passes arguments by value and your swap method only swaps local variables but has no effect on the variables in your sortArray method.

    An easy fix would be to include the code of swap directly in your if. Note: I have not checked the rest of your code. For example, you probably meant to swap the elements in the array rather than the indexes.

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

Sidebar

Related Questions

I'm trying to bubble sort a character array in alphabetic order. My code is
I'm trying to use the bubble sort method to sort an array of only
I need to implement the bidirectional bubble sort in my code. In other words
I'm attempting to do a simple bubble sort code to get familiar with list/string
I want to sort array using Bubble Sort. I an missing a small point
My code for bubble sort is in gui is this:- def bubble(self): def bubble_sort
I want to implement a delegate solution for Bubble sort. I have this code:
I am trying to sort an array of integers in MIPS using bubble sort
I wrote a bubble sort in Prolog (code below). It works but it smells.
i write a c++ code for Bubble sort algorithm and i dont know how

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.