Java sorting algorithm_ Select sorting (example explanation)

Selective sorting is a very simple sorting algorithm. We can know from the literal meaning, Selection is to select the smallest (largest) element from the unordered sequence, and then exchange it with the element at position I-1 (the subscript in the array starts from 0) of the i-th sequence. The sequence before the i-th element is the sorted sequence. The whole sorting process only needs to traverse n-1 times to arrange, and the last element is automatically the maximum (minimum) value.

For example:

arr[] = {3,1,2,6,5,4}

Sorting of the first pass: index = 0, min = 1, after exchange -- > 1,3,4

Sorting of the second trip: index = 1, min = 2,4

Sorting of the third trip: index = 2,4

Sorting of the fourth trip: index = 3, min = 5,4,6

Sorting of the fifth trip: index = 4, min = 4,6

The core code is as follows:

Selective sorting algorithm is an unstable algorithm. Its time complexity is O (N2) and space complexity is O (1)

The above Java sorting algorithm_ Selection sorting (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>