手搓sort得20
查看原帖
手搓sort得20
923362
AAAuc03楼主2024/9/13 21:04
#include <bits/stdc++.h>
#define int long long
using namespace std;
int cmp(int a[],int i,int j)//《真·排序规则》 
{
	int point=a[i];
	while(i<j)
	{
		while(i<j&&a[j]>=point)
		{
			--j;
		}
		if(i<j)a[i]=a[j];
		while(i<j&&a[i]<=point)
		{
			++i;
		}
		if(i<j)a[j]=a[i];
	}
	a[i]=point;
	return i;
}
void unstable_sort(int a[],int left,int right)//《真·不稳定快排》 
{
	if(left>=right)return;
	int pos=cmp(a,left,right);
	unstable_sort(a,left,pos-1);
	unstable_sort(a,pos+1,right);
}
signed main()
{
	int n,a[11000];
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	unstable_sort(a,1,n);
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<" ";
	}
	return 0;
}

请忽视注释

2024/9/13 21:04
加载中...