2

I got this algorithm that sort int[] a from low to high.

public static void sortering(int[] a){
    int temp;               

    for(int i = 0; i < a.length; i++){
    for(int j = i + 1; j < a.length; j++){
        if(a[i] > a[j]){
            temp = a[j];
            a[j] = a[i];
            a[i] = temp;
        }
    }
}
}

What i want to do is to reverse it, make it sort from high to low. I thought this would be a walk in the park doing something like this:

public static void sorteringU(int[] a){
    int temp;

    for(int i = a.length; i < a.length; i--){
        for(int j = i - 1; j < a.length; j--){
            if(a[i] > a[j]){
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
}

I was wrong, this apparently does nothing. Anyone willing to help?

Edit: Thx Jesper and Satya, it worked.

3
  • 3
    The only thing you need to do is change in the original code is if(a[i] > a[j]) to if(a[i] < a[j]). Commented Oct 28, 2013 at 9:04
  • 2
    change this if(a[i] > a[j]){ to if(a[i] < a[j]){ and se if it is working Commented Oct 28, 2013 at 9:04
  • if(a[i] > a[j]) this is where logic is hidden not in loop taversing Commented Oct 28, 2013 at 9:07

5 Answers 5

3

This is enough:

public static void sorteringU(int[] a){
    int temp;               

    for(int i = 0; i < a.length; i++){
    for(int j = i + 1; j < a.length; j++){
        if(a[i] < a[j]){ // Change ">" to "<"
            temp = a[j];
            a[j] = a[i];
            a[i] = temp;
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1
public static void sortering(int[] a){
    int temp;               

    for(int i = 0; i < a.length; i++){
for(int j = i + 1; j < a.length; j++){
    if(a[i] < a[j]){
        temp = a[j];
        a[j] = a[i];
        a[i] = temp;
    }
    }
}
}

try this, instead of if(a[i] > a[j]), make it if(a[i] < a[j])

Comments

0

It should be:

                 note -1   note condition change
                    V       V
for(int i = a.length-1; i >= 0; i--){
  for(int j = i - 1;    j >= 0; j--){

It needs to be -1, as arrays go from 0 to length-1, you previously started off at 0, thus you need to start off at the other side, i.e. length-1.

You need to check >= 0. If you check < length, it will go on forever, since it only gets smaller, so it will never be larger than length. If you compare against 0, it will stop as soon as it gets to the beginning of the array.

But just changing if(a[i] > a[j]) to if(a[i] < a[j]) would be a simpler fix.

Comments

0

Believe me you do not need to change the travering loop :). Sorting logic is hidden into if(a[i] > a[j]). Simply change change if(a[i] > a[j]) to if(a[i] < a[j]). It will work.

for(int i = 0; i < a.length; i++){
    for(int j = i + 1; j < a.length; j++){
        if(a[i] < a[j]){
            temp = a[j];
            a[j] = a[i];
            a[i] = temp;
        }
    }
}

Comments

0

this is a bubble sort algorithm , just change the comparator method will be ok

    change it from if(a[i] > a[j]) to if(a[i] < a[j])

let other code untouched.

2 Comments

IMO it is not bubble sort.
It's still not bubble sort. Bubble sort only swaps adjacent elements. It's more like some variant of selection sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.