1

I was trying to use a sort algorithm according to our programming lecture. Maybe I am just missing something.

I would appreciate it if someone may help me out or could give me a hint about any mistake I made.

Here my current code:

package Sortieralgorithmus;

public class sort {

public static int[] straightSelection(int[] numbers) {

    for (int i = 0; i < numbers.length; i++) {
        int smallestIndex = i;

        for (int j = i + 1; j < numbers.length; j++) {
            if (numbers[i] < numbers[smallestIndex]) {
                smallestIndex = j;
            }
        }
        swap(j, i, numbers);


    }

    return numbers;
}
}
10
  • 3
    Welcome to Stack Overflow! Does your code run? What error do you get? What does the code do when it runs? What do you want it to do? You have to make sure to address all of these questions for us to be able to help you...otherwise, we don't really know what you're even asking. Commented Jan 3, 2015 at 18:50
  • What is not working? Commented Jan 3, 2015 at 18:50
  • 1
    1) What's the problem? 2) Use Java code conventions to make your code easier to read; packages are lowercase, classes start with a capital letter. Commented Jan 3, 2015 at 18:52
  • Please read stackoverflow.com/help/how-to-ask Commented Jan 3, 2015 at 18:52
  • Do you have swap defined anywhere? You have mixed up i, j, and smallestIndex in two places. Commented Jan 3, 2015 at 18:58

2 Answers 2

2

You are doing an in-place selection sort. Change

if (numbers[i] < numbers[smallestIndex]) 

to

if (numbers[j] < numbers[smallestIndex]) 

Also change

(int i = 0; i < numbers.length; i++)

to

(int i = 0; i < numbers.length()-1; i++)

Additionally, because i and j are declared within your for condition, they are only accessible within the scope of the for loop. Instead, declare them outside of your loops.

Lastly, it's a good idea to check if(smallestIndex != i) before swapping them.

So here's your working code, assuming your swap function works correctly.

package Sortieralgorithmus;

public class sort {

public static int[] straightSelection(int[] numbers) {
int i, j;  // declare them here 
int smallestIndex; //declare it here as well

for (i = 0; i < numbers.length-1; i++) {
    smallestIndex = i;

    for (j = i + 1; j < numbers.length; j++) {
        if (numbers[j] < numbers[smallestIndex]) {
            smallestIndex = j;
        }
    }
    if(smallestIndex != i){
    swap(smallestIndex, i, numbers);
    }

}

return numbers;
}
}

Please refer to the following: http://en.wikipedia.org/wiki/Selection_sort

Sign up to request clarification or add additional context in comments.

7 Comments

so it seems like it's working but as I run it I get an empty console and why do I need an Object to retrieve the method straightSelection? Because if I try to retrieve it without one it says : "The method straightSelection(int[]) is undefined for the type Workflow"
oh sorry it says this: "The static method straightSelection(int[]) from the type Sort should be accessed in a static way"
@DevSEDominik removing the static modifier should fix that problem, then.
I did all I get was this : "[I@15db9742"
collabedit.com/bb899 Okay copy and paste your code there and I will take a look. @DevSEDominik
|
0

Here is my implementation of the swap command (sorry for wrong declarations I have recognized them and I will change them immediately!):

package Sortieralgorithmus;

public class Swap {

public static void swap(int a, int b, int []numbers) {

    int temp = numbers[a];
    numbers[a] = numbers[b];
    numbers[b] = temp;

}

}

1 Comment

Never, ever, choose public visibility for something Java without a [doc comment] (docs.oracle.com/javase/8/docs/technotes/tools/windows/…). (Writing the documentation for swap(), you should note a shortcoming with the parameter names, esp. with a and b.)

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.