2
 public class Array {
    public static void sort(int[] list) {
    int min;
    int temp;

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

    }

}
public static void main(String[] args) {
    int a[] = {2,1,3};
    sort(a);
    for(int i = 0; i < a.length; i++) {
        System.out.println(a[i]);

     }
  }
} 

I understand everything in this program until I get to:

        temp = list[min]; 
        list[min] = list[i];
        list[i] = temp;

Can someone explain this in in simple terms? In other words, what is the purpose of the above?

1
  • it swaps the elements Commented Dec 28, 2015 at 21:57

2 Answers 2

2

Let's break it down:

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

}

The first loop that uses the variable i takes the element at the first position. The inner loop, that uses the variable j takes the second element.

In the if condition if(list[j] < list[min]) You are comparing the first element and the second element. If the second element is less than the first element, in the code min = j you take the second element and assign it as the min value.

By the time you exit the second loop, your min is the the position of the element in the list that has the lowest value. You then in the code

temp = list[min]; 
list[min] = list[i]; 
list[i] = temp;

You replace the first element in the list with the lowest element you have. Now your first element in the list is the lowest value of the list. Once you have that, you go inside the first loop again, this time, instead of the first element, your i is the second element and your j is your third element.

This continues until you have looped through all the elements of the list.

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

1 Comment

You beautiful person
2
temp = list[min]; // saves the value of 'list[min]' to the 'temp' variable
list[min] = list[i]; // override the value of 'list[min]' with the value of 'list[i]'
list[i] = temp; // set the value of 'list[i]' to the value of 'list[min]', which is stored in the 'temp' variable

This is used to switch the contents of list[min] & list[i], using a 3rd temporary variable.

6 Comments

A cooler method (which only works for simple types like integers) is the bitwise XOR operator, which eschews the temporary variable entirely. a ^= b; b ^= a; a ^= b; which will confuse the HELL out of anyone not familiar with it. Including it here in case someone finds it in the future.
You can also use a = a + b; b = a - b; a = a - b;
Bitwise is (usually) faster, though. But yes, that will work.
I like how more advanced coders can learn from this question, too.
I learn all kinds of weird and arcane things on Stack Exchange, @StephenPeterWisniewski
|

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.