Strand sort 属于归并排序

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

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

在Python中,我们可以使用归并排序Merge Sort)算法对文件进行排序归并排序是一种分治算法,它将一个大问题分解为小问题,并通过递归地解决小问题来解决整个问题。 首先,我们需要将文件中的数据读取到内存中,并将其进行排序。然后,我们可以使用归并排序的思想来合并已排序的子数组,最终得到完全排序的数据。下面是一个实现文件归并排序的示例代码: ```python def merge_sort_file(input_file, output_file): # 读取输入文件中的数据到内存 with open(input_file, 'r') as file: data = [int(line.strip()) for line in file] # 使用归并排序算法对数据进行排序 sorted_data = merge_sort(data) # 将排序后的数据写入输出文件 with open(output_file, 'w') as file: for num in sorted_data: file.write(str(num) + '\n') def merge_sort(arr): if len(arr) <= 1: return arr # 将数组分为两个子数组 mid = len(arr) // 2 left_arr = arr[:mid] right_arr = arr[mid:] # 递归地对子数组进行排序 left_arr = merge_sort(left_arr) right_arr = merge_sort(right_arr) # 合并已排序的子数组 return merge(left_arr, right_arr) def merge(left, right): merged = [] left_index = 0 right_index = 0 # 比较两个子数组的元素,并按序将较小的元素添加到合并后的数组中 while left_index < len(left) and right_index < len(right): if left[left_index] <= right[right_index]: merged.append(left[left_index]) left_index += 1 else: merged.append(right[right_index]) right_index += 1 # 将剩余的元素添加到合并后的数组中 merged.extend(left[left_index:]) merged.extend(right[right_index:]) return merged ``` 你可以调用`merge_sort_file`函数来实现文件的归并排序。传入输入文件路径和输出文件路径作为参数,函数将会读取输入文件中的数据,对其进行排序,然后将排序后的结果写入输出文件。 注意:这个实现只适用于每行一个数字的文本文件,如果文件结构不同,你需要根据实际情况进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值