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.

So what about location switching?
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 implemented1 and 3Method.
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
  
   
Using namespace std; // traditionally selected sorting void SelectSort (int * arr, int length) {int temp = 0; for (int I = 0; I <length; I ++) {for (int j = I + 1; j <length; j ++) {if (arr [I]> arr [j]) {temp = arr [I]; arr [I] = arr [j]; arr [j] = temp ;}}}} /////////////// The following is a stable algorithm implemented by the linked list //////// // typedef struct node {int val; node * next; node (int v): val (v), next (NULL) {}} * list; list SelectSort_t (list header) {if (header = NULL & header-> next = NULL) {return NULL;} list swap; list curtemp = header; list temp = curtemp-> next; while (curtemp & curtemp-> next) {while (temp & temp-> next) {if (temp-> next-> val <curtemp-> next-> val) {// if (curtemp-> next = temp) {// curtemp-> next = temp-> next; // temp-> next = temp-> next; // curtemp-> next = temp; // continue; //} swap = temp-> next; temp-> next = swap-> next; swap-> next = curtemp-> next; curtemp-> next = swap; continue;} temp = temp-> next;} curtemp = curtemp-> next; temp = curtemp-> next;} return header-> next ;} int main (int argc, const char * argv []) {int a [] = {5, 8, 5, 2, 9, 4}; SelectSort (, sizeof (a)/sizeof (int); for (int I = 0; I <sizeof (a)/sizeof (int); I ++) {printf ("% d", a [I]);} printf ("\ n"); list header = new node (0); list temp; for (int j = 0; j <6; j ++) {temp = (list) malloc (sizeof (struct node); scanf ("% d ", & temp-> val); temp-> next = header-> next; header-> next = temp;} header = SelectSort_t (header); while (header) {printf ("% d", header-> val); header = header-> next;} return 0 ;}
  

The test case is just a string of numbers.

[ 5 8 5 9 2 4 ]

Contact Us

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.