Select sort
The idea of choosing a sort is simple.
- Selects the current minimum number at a time.
- Move back one bit and select the second small number.
- ...
- Move to the second-to-last position, closing after operation.
Still do not understand the attached Baidu Encyclopedia selection sort.
Stability
So is it stable or not?
Unstable interpretation
Read the above Baidu Encyclopedia link people will feel must not be stable ah.
Because for example as follows:
[5 8 5 2 9 4]
This swap the position of 5 and 2 when the first selection is minimal, and becomes as follows:
[2 8 5 5 9 4]
=========
[5 8 5 2 9 4]//This is the original array, observing the position of the number 5.
The first operation was placed behind another 5, which has destroyed the stability.
So is it not stable ?
No, absolutely.
Stability explanation
We have observed that the choice of sort instability lies in the location exchange.
So does the position exchange happen?
Put the smallest one directly in the first position, and put the second small one in the second position.
So how can it be achieved?
- New opening of an array O (N) space
- Linked list
So we don't have a position exchange!!
Then it's a stable one.
Conclusion
- When the array is implemented, the O (1) space complexity is unstable.
- When the array is implemented, the space complexity of O (n) can be stabilized.
- The chain list can be used to achieve stability.
The position exchange does not occur, since 2 and 3 occur although the position transformation occurs. So the same numbers don't swap positions.
Code implementation
I have implemented the methods in the above conclusions separately 1 和 3
.
2 Write a little trouble, temporarily don't want to write.
The code is as follows:
////Main.cpp//Selectsort////Created by Alps on 15/4/2.//Copyright (c) 2015 Chen. All rights reserved.//#include <iostream>using namespace STD;//Traditional selection sortvoidSelectsort (int*arr,intLength) {inttemp =0; for(inti =0; i < length; i++) { for(intj = 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 for the implementation of the linked list/////////////typedef structnode{intVal Node *next; NodeintV): Val (v), next (NULL) {}}*List;Listselectsort_t (Listheader) {if(Header = = NULL && Header->next = = null) {returnNULL; }ListSwapListCurtemp = header;Listtemp = curtemp->next; while(curtemp && curtemp->next && curtemp->next->next) { while(Temp && Temp->next) {if(Temp->next->val < Curtemp->next->val) {//if (Curtemp->next = = temp) {//Curtemp->next = temp->next;//Temp->next = temp->next->next;//Curtemp->next->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; }returnHeader->next;}intMainintargcConst Char* argv[]) {inta[]= {5,8,5,2,9,4}; Selectsort (A,sizeof(a)/sizeof(int)); for(inti =0; I <sizeof(a)/sizeof(int); i++) {printf("%d", A[i]); }printf("\ n");ListHeader =NewNode0);ListTemp for(intj =0; J <6; J + +) {temp = (List)malloc(sizeof(structnode));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 bunch of numbers.
585924 ]
Algorithmic learning-Choosing a stability discussion for sorting (c + + implementation)