Strand sort 属于归并排序

本文详细介绍了StrandSort算法的工作原理、复杂度、适用场景,并提供了算法的C++实现示例。通过解析未排序列表,算法反复抽取并合并有序子列表,最终将整个列表排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example

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
  1. Parse the unsorted list once, taking out any ascending (sorted) numbers.
  2. The (sorted) sublist is, for the first iteration, pushed onto the empty sorted list.
  3. Parse the unsorted list again, again taking out relatively sorted numbers.
  4. Since the sorted list is now populated, merge the sublist into the sorted list.
  5. Repeat steps 3–4 until both the unsorted list and sublist are empty.

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值