# Algorithm learning-stability of sorting selection (C ++ implementation)

Source: Internet
Author: User

Algorithm learning-stability of sorting selection (C ++ implementation)
Select sort

The idea of selecting sorting is simple.

Select the current minimum number each time. Move one digit backward and select the second small number. ... Move to the second-to-last position, and end after the operation.

If you do not know how to attach Baidu encyclopedia, select sorting.

Stability

So is it stable?

Unstable Interpretation

Those who have read the link from Baidu encyclopedia may feel that it is not stable.

For example:

[5 8 5 2 9 4]

In this case, when the minimum value is selected for the first time, the positions 5 and 2 are changed to the following:

[2 8 5 5 9 4]
==========
[5 8 5 2 9 4] // This is the original array. Observe the position of number 5.

The first operation is placed after another 5, which has damaged the stability.

Is that true?Unstable?

Not absolute.

Stable explanation

We have observed that the selected sorting is unstable because location switching occurs.

Place the smallest value in the first position and the second smaller value in the second position.

So how can we achieve it?

New array O (N) Space linked list

In this way, location switching will not happen !!

It is stable.

Conclusion
When the array is implemented, the O (1) space complexity is unstable. When the array is implemented, the O (n) space complexity can be stabilized. You can use a linked list to achieve stability.

Because location switching occurs between 2 and 3, but no location switching occurs. Therefore, the same number is not exchanged.

Code Implementation

Below I have implemented`1 and 3`Method.
2. It is a bit difficult to write. I don't want to write it for the moment.

The Code is as follows:

``````/// Main. cpp // SelectSort /// Created by Alps on 15/4/2. // Copyright (c) 2015 chen. All rights reserved. // # include

``````

The test case is just a string of numbers.

``[ 5 8 5 9 2 4 ]``

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.