简体   繁体   中英

Java Bubblesort is only swapping 1st array item

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.

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.

You are using two times the i counter:

for(int i = 0; i < allArraySize; i++) {
    targetArray[i] = sourceArray[i];
        for (i = 0; i < allArraySize; i++) {
        ...

Possibly the inner loop is making overwriting the i counter that are used in your outer loop.

(Please note that this is my first post to Stackoverflow. I've programmed in many languages, none of which are truly OO. I've been attacking Java for a month or so in spare time.)

As in the original code in msg 1, I felt the need to do the swapping via a separate procedure, since doing so works just great (and is encouraged) in Pascal, VBasic, etc. as part of the "divide-and-conquer" programming practice. So it was quite puzzling to me, too, why my identical swap routine didn't work in Java... until I re -read about parameter passing. I finally got it through my head that the array itself needed to be passed in order to change its contents.

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

The above worked for me, and as much as I've struggled to absorb Java, I'm actually proud that I finally figured it out. But geez.... pretty routine thing to be baffled by, huh?

On the other hand, I guess I learned what by reference means as well as how to cope with it.

And I guess I unwittingly encapsulated the swap method's workings, thereby following an OO tenet. So... double good?

But this makes me ask: Is it just a goofy idea to "divide and conquer" sorting to this extent? After all, moving the 3-line swap routine inline with the rest of the sort routine is common sense, but is my swap method just silly or, if not "best", then at least "good" Java practice?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM