Learn Sorting Techniques
The video lecture on this page describes the process behind the selection sort algorithm. It is my hope that you will seek to understand the algorithm so that you can then generate your own version of the code for other programming languages, or even better yet, one day design a sort algorithm of your own that surpasses the techniques currently employed by computer scientists. This can only happen if you as the student do more than just copy and paste code, but truly apply your analytic skills to understand and improve upon todays technologies.
~ Mr. Hudak
Source Code – Selection Sort Method
public static void selectionSort(int[] arr)
{
int min = 0; //variable to hold current min value
int index = 0; //location of current minimum
int temp = 0; //hold value during array sort
for(int i = 0 ; i < arr.length ; i++)
{
index = i; //variable to hold index of current min
min = arr[index]; //current min
for(int j = i + 1 ; j < arr.length ; j++)
{
if(arr[j] < min)
{
min = arr[j]; //set new min
index = j; //location of new min
}
}
temp = arr[i]; //hold current value
arr[i] = min; //replace current value with min
arr[index] = temp; //complete the swap sort.
}
}