0

So what I'm trying to do is to sort an array by going through the whole array and each time swap the value with the minimum value in the array. I've created a method to find minValue, but I'm not sure how to swap that value with my current value. This is probably a horrible explanation, but here's the code:

public static int findMin(int[] numList, int start, int end){

    int minIndex = numList[0];
    for (int i = start; i < end; i++) {
        if(numList[i] < minIndex){
            minIndex = numList[i];
        }
    }

    return minIndex;
}

And my loop which is supposed to sort the array:

for (int i = 0; i < numList.length; i++) {
        int minIndex = findMin(numList,i,10);
        numList[i] = minIndex;
    }

So as you can see, this only replaces numList[i] with the minValue. So how can I swap the value already in numList[i] with wherever in the array minValue was found?

Thanks!

1
  • why do you req minIndex for each index of numList ? I dont understand the reason .. you can loop whole the array and retrieve the minimum, then go replace it .. Commented Oct 17, 2013 at 10:29

3 Answers 3

2

You need to copy the value to a temporary integer to swap:

int temp = numList[i];
numList[i] = numList[minIndex];
numList[minIndex] = temp;
Sign up to request clarification or add additional context in comments.

2 Comments

Re your footnote, how does compareAndSwap allow you to swap 2 variables without using a temporary variable ... assuming that is what you are alluding to?
@StephenC I guess it's not possible, I was assuming swapping addressed two memory addresses instead of an object.
1

The Corrected method is

public static int findMin(int[] numList, int start, int end){

        int minVal= numList[start];
        int minIndex = start;
        for (int i=start; i <end; i++) {
            if(numList[i] <minVal){
               minIndex=i;
               minVal=numList[i];
            }
        }

        return minIndex;
    }

And the corrected loop is

for (int i = 0; i < numList.length; i++) {
            minIndex = findMin(numList,i,numList.length);
            temp=numList[i];
            numList[i]=numList[minIndex];
            numList[minIndex]=temp;
        }

Comments

1

You have to use a temporary integer

int temp = numList[i];
numList[i] = numList[minIndex];
numList[minIndex] = temp;

In your findMin() I think

int minIndex = numList[0];

should be

int minIndex = numList[start];

because after the first swap, numlist[0] will be the smallest value. The minimum value should be searched for in the rest of the array, so that we get successively larger values in each iteration.

2 Comments

numList[i] = numList[minIndex] gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 99907 Wouldnt numList[minIndex] in this case simply mean element number 99907 in my array? My array only consists of 10 values.
@Boxiom This is because you are returning the array element itself as minIndex where it should be the index of the minimum element. In your findMin() function, change minIndex = numList[i]; to minIndex = i;

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.