* * Select sort
Principle: If the elements on the 0-point mark are compared to the elements in the following sequence,
At the end of the first inner loop, the minimum value appears in the 0-point mark position.
You just think about how we hit the stars.
****
***
**
*
public static void Selectsort (int[] arr)
{
i<arr.length-1 N Number 22 ratio, n-1 times can get the maximum value.
for (int i=0; i<arr.length-1; i++)
{
0 The point mark element is not necessary and itself, so that J starting from 1.
I found that J is always 1 bigger than I in order not to repeat the comparison, so J starts with I+1.
for (int j=i+1; j<arr.length; j + +)
{
if (Arr[j]<arr[i])
{
int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}
}
* * Bubble sort
Principle: Two adjacent elements are compared, the maximum value appears at the maximum angle mark after the first comparison.
public static void Bubblesort (int[] arr)
{
i<arr.length-1 N Number 22 ratio, n-1 times can get the maximum value.
for (int i=0; i<arr.length-1; i++)
{
In order to prevent the corner mark from crossing, J<arr.length-1
To reduce the number of comparisons, j<arr.length-1-i
for (int j=0; j<arr.length-1-i; j + +)
{
if (arr[j]>arr[j+1])
{
int temp = Arr[j];
ARR[J] = arr[j+1];
ARR[J+1] = temp;
}
}
}
}
Java Sort: