C++各种排序方法讲解(进阶)

本文介绍了C++实现的两种排序算法:堆排序的详细步骤与修复过程,以及希尔排序的两种版本,包括插入和Shell Sort。通过实例演示了如何在实际项目中使用这些算法对整数数组进行排序。

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

堆排序~1

#include<iostream>
using namespace std;
int heap[32768];
int heap_size=0;
int tot;
void put(int num){
	heap[++heap_size]=num;
	int now=heap_size,next;
	while(now>1){
		next=now>>1;
		if(heap[next]<=heap[now]){
			return;
		}
		else{
			int temp=heap[next];
			heap[next]=heap[now];
			heap[now]=temp;
			now=next;
		}
	}
}
void repair(){
	heap[1]=heap[heap_size];
	int now=1;
	int next=now*2;
	while(next<=heap_size){
		if(next<heap_size && heap[next+1]<heap[next])
		    next++;
		if(heap[next]<heap[now]){
			int temp=heap[next];
			heap[next]=heap[now];
			heap[now]=temp;
			now=next;
		}
		else 
		    break;
		next=now*2;
	}
	heap_size--;
}
int main(){
	int x;
	cin>>tot;
	for(int i=1;i<=tot;i++){
		cin>>x;
		put(x);
	}
	for(int i=1;i<=tot;i++){
		cout<<heap[1]<<endl;
		repair();
	}
	return 0;
} 

堆排序~2

#include<iostream>
using namespace std;
int heap[32768];
int tot;
void set(int parent,int len){
	int now=parent;
	int next=parent*2;
	while(next<=len){
		if(next<len && heap[next]>heap[next+1])
		    next++;
		if(heap[now]<=heap[next])
	        return;
	    else{
	    	int temp=heap[now];
	    	heap[now]=heap[next];
	    	heap[next]=temp;
	    	now=next;
		}
		next=now*2;
	}
}
int main(){
    cin>>tot;
    for(int i=1;i<=tot;i++){
    	cin>>heap[i];
	}
	for(int i=tot/2;i>0;i--){
		set(i,tot);
	}
	for(int i=tot;i>1;i--){
		int temp=heap[1];
		heap[1]=heap[i];
		heap[i]=temp;
		set(1,i-1);
	}
	for(int i=1;i<=tot;i++){
		cout<<heap[i]<<endl;
	}
	return 0;
}

希尔排序~1

#include<iostream>
using namespace std;
int tot;
int num[32768];
void shellsort(){
	for(int net=tot/2;net>0;net/=2){
		for(int i=net+1;i<=tot;i++){
			for(int j=i-net;j>0;j-=net){
				if(num[i]>num[j]){
				    int temp=num[i];
			        for(int w=i;w>j;w-=net){
			    	    num[w]=num[w-net];
			        }
			        num[j]=temp;
				}
			}
		}
	}
}
int main(){
	cin>>tot;
	for(int i=1;i<=tot;i++)cin>>num[i];
	shellsort();
	for(int i=1;i<=tot;i++)cout<<num[i]<<endl;
	return 0;
} 

希尔排序~2

#include<iostream>
using namespace std;
void ShellSort(int arr[],int N)
{
	int i,j,gap;
	for(gap = N/2;gap>0;gap/=2)
	{
		for(i=0;i<N;i++)
		{
			for(j=i-gap;j>=0;j-=gap)
			{
				if(arr[i]>arr[j])
					break;
			}
			int temp = arr[i];
			for(int k=i;k>j;k-=gap)
				 arr[k] = arr[k-gap];
			arr[j+gap] = temp;
		}
	}
	for(int h=N-1;h>=0;h--)
	cout<<arr[h]<<" ";
}

int main()
{
	int tot;
	cin>>tot;
	int a[1100];
	for(int i=0;i<tot;i++)cin>>a[i];
	ShellSort(a,tot);
	return 0;
}

C++下标可以为负数吗?https://blog.csdn.net/way_back/article/details/122782426?spm=1001.2014.3001.5501

C++各种排序(基础)icon-default.png?t=M0H8https://blog.csdn.net/way_back/article/details/122769310?spm=1001.2014.3001.5501 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

感谢有你陪伴

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值