Look at the animation algorithm: sort – select sort

Time：2021-1-21

brief introduction

Selection sorting is to select the largest or smallest element from the array, and then interact with the first or last element of the team.

Because the first thing to do is a selection process, it is called selection sorting.

Examples of selection sorting

If we have an array: 29,10,14,37,20,25,44,15, how can we sort it?

First look at an animation:

The principle of sorting is as follows:

Eight numbers, we need seven rounds of sorting.

Take the first round as an example, we compare all the data, find the smallest 10, and then put 10 in the first of the array.

In the second round, because the first element 10 of the array is already in order, we just need to start from the second position. Similarly, in the second round, we find the smallest 14 of the following elements and put it in the second position of the array.

And so on, 7 rounds of sorting will get the final result.

Java code implementation of selective sorting

We implement the above logic in Java code as follows:

``````public class SelectionSort {

public void doSelectionSort(int[] array){
log.info (the array before sorting is: {} ", array);
//Outer loop, traversing all rounds
for(int i=0; i< array.length-1; i++){
//Inner loop, find the smallest number
int minIndex=i;
for(int j=i+1;j<array.length;j++)
{
if(array[j] < array[minIndex])
{
minIndex = j;
}
}
//After each selection, the element of minindex is exchanged with the i-th element
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
log.info (the array sorted in the {} round is: {} ", I + 1, array);
}
}

public static void main(String[] args) {
int[] array= {29,10,14,37,20,25,44,15};
SelectionSort selectionSort=new SelectionSort();
selectionSort.doSelectionSort(array);
}
}``````

Results of operation:

The second Java implementation of selective sorting

In the above code, we can find the smallest element every time. Similarly, we can also find the largest element.

``````public class SelectionSort1 {

public void doSelectionSort(int[] array){
log.info (the array before sorting is: {} ", array);
//Outer loop, traversing all rounds
for(int i=0; i< array.length-1; i++){
//Inner loop, find the largest number
int maxIndex=0;
for(int j=0;j<array.length-i;j++)
{
if(array[j] > array[maxIndex])
{
maxIndex = j;
}
}
//After each selection, exchange the element of maxindex with the element of length-i-1
int temp = array[array.length-i-1];
array[array.length-i-1] = array[maxIndex];
array[maxIndex] = temp;
log.info (the array sorted in the {} round is: {} ", I + 1, array);
}
}

public static void main(String[] args) {
int[] array= {29,10,14,37,20,25,44,15};
SelectionSort1 selectionSort=new SelectionSort1();
selectionSort.doSelectionSort(array);
}
}``````

Results of operation:

We should pay attention to the different comparison conditions of internal circulation in the two kinds of sorting.

Time complexity of selection sorting

Like bubble sorting, selection sorting needs n * N cycles, so its time complexity is O (N 2).

learn-algorithm