Ideas:
First comparison: The program locates the record in the first position of the array, comparing the first data with each subsequent data,
Use a variable mix to record the index of data smaller than the first number, by comparing constantly updating the mix, and finally getting the index of the smallest element in the entire array, swapping the first number with the number of mix, the smallest digit is in the position of the array,
Second comparison: The program locates the record in the second position of the array, comparing the second data with each subsequent data,
Gets the smallest number in the array, starting with the second data, and swapping positions with the second number.
For a total of n-1 comparisons, each time a minimum number is selected in the remaining unordered array.
public void Selectsort (int[] array)
{
int arraylength = array.length;
for (int i = 0; i < arrayLength-1 i++)
{
//mix index value used to hold the smallest element in the array
int mix = i;//select the current array element as the minimum, traverse to the last, find the smallest array element The index of the element, and the current position exchange for
(int j = i+1 J < arraylength; J +)
{
if (ARRAY[J) < Array[mix])
{
mix = J;
}///////
Maximum swap
if (mix!= i)//if mix is not equal to current I, let the value of the minimum and current position exchange
{
swap (array,mix,i);
}
}
}
private void Swap (int[] array,int mix,int i)
{
int temp = Array[mix];
Array[mix] = Array[i];
Array[i] = temp;
}