0

I'm doing mooc.fi, my code works but it won't submit.

errors I'm getting


the method swap does not work correctly with parameter 4, 7, 8, 6 index1=0 index2=3

the result was 4, 7, 8, 6 but it should have been 6, 7, 8, 4


and


the method sort does not work correctly with parameter 10, 20, 6, -1, 13, 11

the result was 10, 20, 6, -1, 13, 11 but it should have been -1, 6, 10, 11, 13, 20


I know the errors are connected but I'm not too sure what to do to fix this, any help is appreciated! thank you!

My Code:

import java.util.Arrays;

public class Main {

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

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

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

    public static void swap(int[] array, int index1, int index2) {
        int hold = 0;
        for (int i = 0; i < array.length; i++) {
            hold = array[index1];
            array[index1] = array[index2];
            array[index2] = hold;
        }
    }

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

    public static void main(String[] args) {
        int[] values = {8, 5, 3, 7, 9, 6, 1, 2, 4};
        sort(values);
    }
}
1

3 Answers 3

1

This algorithm is designed very badly. both smallest and indexOfSmallest use a loop, this will degrade geometrically.

The swap should not be scanning the entire array as you only want to swap two elements, not all of them!

You're effectively doing a whack version of a bubble-sort, but in a really convoluted way with too many loops. This will start to perform horribly with any decent sized list.

A much simpler version can be found here

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

2 Comments

I can see how this way can be inefficient but to pass the exercise I think I had to do it this way.
@syntax: Your swap() method has a useless (and potentially harmful) for loop around the code. Remove it, and at least your first error should go away. (Bonus exercise: Figure out in which cases your original code will work despite the needless loop, and in which cases it will fail.)
0

You do not need a loop for the swap method. As you only want to change the two element once and not n-times (which results in not swapping them at all if n is an even number).

Comments

0
public int[] swapArrayElement(int arr[], int a, int b) {
        int index1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == a || arr[i] == b) {
                index1 = i;
                if (arr[i] == a) {
                    for (int k = index1; k < arr.length; k++) {
                        if (arr[k] == b) {
                            arr[k] = arr[index1];
                            arr[index1] = b;
                        }
                    }
                    return arr;
                } else if (arr[i] == b) {
                    {
                        for (int k = index1; k < arr.length; k++) {
                            if (arr[k] == a) {
                                arr[k] = arr[index1];
                                arr[index1] = a;
                            }
                        }
                    }
                    return arr;
                }

            }

        }
        return arr;
    }

1 Comment

This does not answer the question. It is just a dump of code and does not help the OP to understand their mistakes or how to do it better. Instead of dumping code, you should always explain why and how stuff works.

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.