Strand sort is a sorting algorithm. It works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with a result array. Each iteration through the unsorted list pulls out a series of elements which were already sorted, and merges those series together.
The name of the algorithm comes from the "strands" of sorted data within the unsorted list which are removed one at a time. It is a comparison sort due to its use of comparisons when removing strands and when merging them into the sorted array.
The strand sort algorithm is O(n2) in the average case. In the best case (a list which is already sorted) the algorithm is linear, or O(n). In the worst case (a list which is sorted in reverse order) the algorithm is O(n2).
Strand sort is most useful for data which is stored in a linked list, due to the frequent insertions and removals of data. Using another data structure, such as an array, would greatly increase the running time and complexity of the algorithm due to lengthy insertions and deletions. Strand sort is also useful for data which already has large amounts of sorted data, because such data can be removed in a single strand.
Unsorted list | Sublist | Sorted list |
---|---|---|
3 1 5 4 2 | ||
1 4 2 | 3 5 | |
1 4 2 | 3 5 | |
2 | 1 4 | 3 5 |
2 | 1 3 4 5 | |
2 | 1 3 4 5 | |
1 2 3 4 5 |
#include <iostream> using namespace std; void merge(int res[],int resLen,int sublist[],int last) { int *temp = (int *)malloc(sizeof(int)*(resLen+last)); int beginRes=0; int beginSublist=0; int k; for(k=0;beginRes<resLen && beginSublist<last;k++) { if(res[beginRes]<sublist[beginSublist]) temp[k]=res[beginRes++]; else temp[k]=sublist[beginSublist++]; //cout<<"k:"<<k<<" temp[k]:"<<temp[k]<<endl; } if(beginRes<resLen) memcpy(temp+k,res+beginRes,(resLen-beginRes)*sizeof(int)); else if(beginSublist<last) memcpy(temp+k,sublist+beginSublist,(last-beginSublist)*sizeof(int)); memcpy(res,temp,(resLen+last)*sizeof(int)); free(temp); } void strandSort(int array[],int length) { int *sublist=(int *)malloc(sizeof(int)*length); int *res=(int *)malloc(sizeof(int)*length); //sizeof(array)=4 int i; int resLen=0; res[0]=array[0]; array[0]=0; for(i=1;i<length;i++) { if(array[i]>res[resLen]) { resLen++; res[resLen]=array[i]; array[i]=0; } } resLen++; int last; int times=1; bool finished; while (true) { finished = true; last = -1; for(i=times;i<length;i++) { //cout<<"This time array[i]: "<<array[i]<<endl; if(array[i]!=0) { //cout<<"This time array[i]: "<<array[i]<<endl; if (last==-1) { sublist[0]=array[i]; array[i]=0; last=0; finished = false; } else if(array[i]>sublist[last]) { last++; sublist[last]=array[i]; array[i]=0; } } } if(finished) break; last++; merge(res,resLen,sublist,last); resLen=resLen+last; times++; } memcpy(array,res,length*sizeof(int)); } int main() { //int array[]={15,9,8,1,4,11,7,2,13,16,5,3,6,2,10,14}; int array[]={13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10,35,54,90,58}; int i; int length=sizeof(array)/sizeof(int); //在这里 sizeof(array)=80 strandSort(array,length); //int *arr = array; //cout<<arr[2]<<endl; for(i=0;i<length;i++) { cout<<array[i]<<" "; } cout<<endl; return 0; }