0

I am trying to sort an array according to example 104.5. It asks you to sort an array from smallest to largest. I've thoroughly scoured through my program several times, but I can't spot my error. Here's the code from my sorting class:

import java.util.Arrays;
public class Sorting {
    public static int smallest(int[] array) {
        int min = array[0];
        for(int i = 0; i < array.length; i++){
            if(array[i] < array[0]) {
                min = array[i];
            }
        }
        return min;
    }

    public static int indexOfTheSmallest(int[] array) {
        int ind = array[0];
        for(int i = 0; i < array.length; i++){
            if(array[i] < array[0]) {
                ind = i;
            }
        }
        return ind;
    }

    public static int indexOfTheSmallestStartingFrom(int[] array, int index) {
        int ind = index;
        for(int i = index; i < array.length; i++){
            if(array[i] < array[index]) {
                ind = i;
            }
        }
        return ind;
    }

    public static void swap(int[] array, int index1, int index2) {
        int stor = array[index1];
        array[index1] = array[index2];
        array[index2] = stor;
    }

    public static void sort(int[] array) {
        int indSmall;
        System.out.println(Arrays.toString(array));
        for(int i = 0; i < array.length; i++) {
            indSmall = indexOfTheSmallestStartingFrom(array, i);
            swap(array, indSmall, i);
            System.out.println(Arrays.toString(array));
        }
    }
}

And here's the code I am running:

int[] values = {8, 3, 7, 9, 1, 2, 4};
        Sorting.sort(values);

It should output:

[8, 3, 7, 9, 1, 2, 4]
[1, 3, 7, 9, 8, 2, 4]
[1, 2, 7, 9, 8, 3, 4]
[1, 2, 3, 9, 8, 7, 4]
[1, 2, 3, 4, 8, 7, 9]
[1, 2, 3, 4, 7, 8, 9]
[1, 2, 3, 4, 7, 8, 9]

But instead it is outputting:

[8, 3, 7, 9, 1, 2, 4]
[4, 3, 7, 9, 1, 2, 8]
[4, 2, 7, 9, 1, 3, 8]
[4, 2, 3, 9, 1, 7, 8]
[4, 2, 3, 8, 1, 7, 9]
[4, 2, 3, 8, 1, 7, 9]
[4, 2, 3, 8, 1, 7, 9]
[4, 2, 3, 8, 1, 7, 9]

1 Answer 1

1

This

if(array[i] < array[0]) {

should be

if(array[i] < min) {

or you could use Math.min(int, int) like

min = Math.min(array[i], min);

without an if at all.

Also, in indexOfTheSmallestStartingFrom

if(array[i] < array[index]) {

should be

if(array[i] < array[ind]) {
Sign up to request clarification or add additional context in comments.

5 Comments

Only indexOfTheSmallestStartingFrom and swap are called. The methods that use the if(array[i] < array[0]) comparison are never called, so that shouldn't matter right?
@Rob Then why are they in your question?
Sorry, I wasn't sure if I should have posted the class in its entirety or posted just the relevant methods. I'll do the latter next time.
@Rob Edited to point out basically the same bug in indexOfTheSmallestStartingFrom.
Oh, I see it now. Thank you.

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.