十大排序|十大排序

news2024/9/19 20:39:48

 稳定排序:冒泡排序、插入排序、归并排序、基数排序、桶排序

不稳定排序:选择排序、快速排序、希尔排序、堆排序

二、插入排序:

代码:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;



int main()
{
	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;
	int i, j;
	for (i = 1; i < n; i++)
	{
		if (A[i] < A[i - 1])//i为要检查的元素,要和前面的有序序列(从小到大)
		{
			int temp = A[i];//哨兵位
			for (j = i - 1; j >=0; j--)//和有序队列的每一位元素进行比较,插入合适的位置
			{
				if (A[j] > temp)
				{
					A[j + 1] = A[j];//j移到j+1的位置,都往后挪一位
					break;
				}
			}
			A[j+1] = temp;//最后一个元素是j+1,为什么,因为可能到-1

		}
		
	}

	for (int j = 0; j < n; j++)
	{
		cout <<A[j] << endl;
	}

	return 0;
}

三、希尔排序

在插入排序的基础上

 

 

 

 这里王道讲的代码,有n个数的话实际上留了n+1位,第一位空着做哨兵位。我对数组修改

 

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;

int main()
{

	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;//留第一个位置做哨兵位
	int i, j;
	for (int d = n / 2; d >= 1; d = d / 2)//每一趟d都进行缩小
	{
		for (i = d; i <n;i++)//注意是从d开始
		{
			if (A[i] < A[i - d])//说明可以插入的有序队列,这个if到下面的逻辑基本是插入排序,找到一个合适的位置插入,其余往后移动
			{
				int temp = A[i];
				for (j = i - d; j >=0 && temp< A[j]; j = j - d)
				{
					A[j+d] = A[j];

				}
				A[j + d] = temp;

			}
			
		
			
		}
	}

	for (int j = 0; j < n; j++)
	{
		cout << A[j] << endl;
	}
	return 0;
}

 三、冒泡排序(相邻位置元素进行比较和swap,直到把最大的元素“冒”到最后)


#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;
void swap(int *a, int *b)
{
	int temp = *a;
	 *a= *b;
	 *b = temp;

}

void Swap1(int& x, int& y)
{
	int temp = x;
	x = y;
	y = temp;
}

int main()
{
	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;
	for (int i = 0; i < n-1; i++)
	{
		bool flag = false;
		for (int j =0; j < n -1-i; j++)
		{
			if (A[j] > A[j + 1])
			{
				Swap1(A[j], A[j+1]);
				flag = true;

			}
		}
		if (flag == false)//代表一次都没有发生交换,说明数组有序,可以停止比较了
		{
			break;
		}
	}

	for (int j = 0; j < n; j++)
	{
		cout << A[j] << endl;
	}

	return 0;
}

 

快速选择排序------------------基于交换排序,前序遍历构建树,枢轴,

 

 

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;

//类似前序遍历构建树


//1、找到枢轴的位置
int Partition(int A[], int low, int high)
{
	int base = A[low];
	while (low < high)
	{
		while (low <high && A[high] >= base)
		{
			high--;
		}
		A[low] = A[high];
		while (low < high && A[low] <= base)
		{
			low++;
		}
		A[high] = A[low];

	}
	A[low] = base;
	return low;
}


//前序遍历构建树
void QuickSort(int A[], int low, int high)
{
	if (low < high)
	{
		int root_index = Partition(A, low, high);
		QuickSort(A, low, root_index - 1);
		QuickSort(A, root_index + 1, high);

	}
	
}




int main()
{
	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;
		

	for (int i=0;i<n;i++)
	{
		cout << A[i] << " ";
	}
	cout << endl;
	QuickSort(A, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << A[i] << " ";
	}
	cout << endl;
	return 0;
}

 

 

 简单选择排序

 

 

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;

void Swap3(int& a, int& b)
{
	int temp = a;
	 a = b;
	 b = temp;
}

int main()
{
	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;
	for (int i = 0; i < n - 1; i++)
	{
		int minn = i;
		for (int j = i + 1; j < n; j++)
		{
			if (A[j]<A[minn])
			{
				minn = j;
			}
		}
		if (minn != i)
		{
			Swap3(A[i], A[minn]);

		}
		
	}
	for (int j = 0; j < n; j++)
	{
		cout << A[j] << endl;
	}

	return 0;
}

归并排序:

 

 

 

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;


//借助辅助数组
vector<int>B;
void Merge(int A[],int low,int mid,int high)
{
	int k = low,i=low,j=mid+1;
	while(i <= mid && j <= high)
	{
		if (A[i] < A[j])
		{
			B[k++] = A[i++];
		}
		else
		{
			B[k++] = A[j++];

		}
	}
	while (i <= mid)
	{
		B[k++] = A[i++];
	}
	while (k <= high)
	{
		B[k++] = A[j++];
	}

	for (k = low; k <= high; k++)
	{
		A[k] = B[k];
	}
}

void MergeSort(int A[], int low, int high)
{
	if (low < high)
	{
		int mid = low + (high - low) / 2;
		MergeSort(A, low, mid);
		MergeSort(A, mid+1,high);
		Merge(A, low, mid, high);
	}

}

int main()
{
	int A[6] = { 4,5,6,3,2,1 };
	int n = 6;
	
	B.resize(n);
	MergeSort(A, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << A[i] << "  ";
	}
	cout << endl;


	return 0;
}

 

基数排序:

桶排序:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/822015.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

真机搭建中小网络

这是b站上的一个视频&#xff0c;演示了如何搭建一个典型的中小网络&#xff0c;供企业使用 一、上行端口&#xff1a;上行端口就是连接汇聚或者核心层的口&#xff0c;或者是出广域网互联网的口。也可理解成上传数据的端口。 二、下行端口&#xff1a;连接数据线进行下载的端…

Vue源码学习 - 虚拟Dom 和 diff算法

目录 前言一、认识虚拟DOM用 JS 对象模拟 DOM 结构用JS对象模拟DOM节点的好处为什么要使用虚拟 DOM 呢&#xff1f;虚拟Dom 和 diff算法的关系 二、认识diff算法diff算法的优化key的作用diff算法 在什么时候执行&#xff1f; 三、深入diff算法源码patch 函数sameVnode 函数patc…

简要介绍 | 生成模型的演进:从自编码器(AE)到变分自编码器(VAE)和生成对抗网络(GAN),再到扩散模型

注1:本文系“简要介绍”系列之一,仅从概念上对生成模型(包括AE, VAE, GAN,以及扩散模型)进行非常简要的介绍,不适合用于深入和详细的了解。 生成模型的演进:从自编码器(AE)到变分自编码器(VAE)和生成对抗网络(GAN),再到扩散模型 一、背景介绍 生成模型在机器学习领域…

【Linux后端开发】poll/epoll多路转接IO服务器

目录 一、poll原理 二、poll实现多路转接IO服务器 三、epoll函数接口 四、epoll的工作原理 五、epoll实现多路转接IO服务器 一、poll原理 poll函数接口 #include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout);// pollfd结构 struct pollfd …

搜索二叉树_SearchBinaryTree

目录 搜索二叉树的原理 搜索二叉树的搜索时间复杂度 二叉搜索树实现_key 模型 节点 构造函数 查找 中序遍历 插入 循环 递归 删除 循环 1.删除叶子节点 2.删除有一个孩子的节点 3.左右孩子都不为空 递归 析构函数 拷贝构造 operator key_value 模型 节点 …

JDBC-笔记

JDBC 1. JDBC介绍 JDBC&#xff08;Java Database Connectivity&#xff09;是一种用于连接和操作数据库的 Java API。 通过Java操作数据库的流程 第一步&#xff1a;编写Java代码 第二步&#xff1a;Java代码将SQL发送到MySQL服务端 第三步&#xff1a;MySQL服务端接收到SQ…

ems

【python爬虫】邮政包裹物流查询 目标网站 ems 邮政快递包裹查询: https://www.ems.com.cn/ 截图 接口预览 getPic请求滑动验证码的背景图片和滑块图片&#xff0c;返回的是base64编码的图片 getLogisticsTestFlag发送验证码的验证信息 xpos为滑动的距离&#xff0c;本站没…

CUDA编译器环境配置篇

cuda教程目录 第一章 指针篇 第二章 CUDA原理篇 第三章 CUDA编译器环境配置篇 第四章 kernel函数基础篇 第五章 kernel索引(index)篇 第六章 kenel矩阵计算实战篇 第七章 kenel实战强化篇 第八章 CUDA内存应用与性能优化篇 第九章 CUDA原子(atomic)实战篇 第十章 CUDA流(strea…

CHI中的System Debug, Trace, and Monitoring

Data Source indication □ Read request的completer&#xff0c;可以在CompData, DataSepResp, SnpRespData, and SnpRespDataPtl response中的datasource域段中指定data的来源&#xff1b;即使响应中带有错误&#xff0c;该datasource也是有效的&#xff1b; □ 该域段也可复…

Flutter 之Bloc入门指南实现倒计时功能

Flutter Timer By Bloc 前言Stream.periodic实现倒计时定义Bloc状态定义Bloc事件定义Bloc组件定义View层参考资料前言 使用Bloc开发Flutter的项目,其项目结构都很简单明确,需要创建状态,创建事件,创建bloc,创建对应的View。flutter_timer项目来分析下Bloc的使用方法。 通…

逻辑回归变量系数可为负数吗?应该如何解释?

之前很多学员来问逻辑回归变量系数是否都应该为正数&#xff0c;如果出现负的变量系数该怎么办&#xff1f;是否需要重新建模&#xff1f;这些学员都是在网上搜索时&#xff0c;被错误信息误导。网上信息可以随意转载&#xff0c;且无人审核对错。我见过最多情况时很多文章正确…

软工导论知识框架(三)结构化的设计

一.传统软件工程方法学采用结构化设计技术&#xff08;SD&#xff09; 从工程管理角度结构化设计分两步&#xff1a; 概要设计&#xff1a; 将软件需求转化为数据结构和软件系统结构。详细设计&#xff1a;过程设计&#xff0c;通过对结构细化&#xff0c;得到软件详细数据结构…

dubbo-helloworld示例

1、工程架构 2、创建模块 &#xff08;1&#xff09;创建父工程,引入公共依赖 pom.xml依赖 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></depende…

MultipartFile 获取文件名、文件前缀、文件后缀、文件类型

测试 debug 方法 RequestMapping(value "/test",method RequestMethod.POST)public void fileUpload(MultipartFile file){// 文件名String originalFilename file.getOriginalFilename();// 文件名前缀String fileName file.getOriginalFilename().substring(0,…

第5讲:VBA中OFFSET函数的利用

【分享成果&#xff0c;随喜正能量】幸福从来不是跟别人比来的&#xff0c;追求不同&#xff0c;各有活法&#xff0c;开心了就笑&#xff0c;累了就休息&#xff0c;日子安稳踏实就是最大的幸福。做人就怕尊严扫地&#xff0c;保留一点做人的尊严&#xff0c;是人生最大的本钱…

C语言每日一题

今天分享的是一道牛客网上面的题目&#xff0c;链接在下面 有序序列合并 这道题做法有很多&#xff0c;最简单的是合并一起&#xff0c;然后用排序就行了&#xff0c;今天将一个最高效的办法&#xff0c;思路是两个数组第一项进行比较&#xff0c;小的先输出&#xff0c;输出的…

Mac上命令

1. block端口&#xff1a; sudo cp /etc/pf.conf /etc/pf443.conf 编辑pf443.conf&#xff0c;vim /etc/pf443.conf&#xff0c;如 block on en0 proto udp from any to any port 9000 # block UDP port 9000 block on en0 proto tcp from any to any port 5004 # bloc…

InnoDB引擎底层逻辑讲解——后台线程

1.后台线程 后台线程的作用就是将innodb存储引擎缓冲池中的数据&#xff0c;在合适的时机刷新到磁盘文件当中。innodb存储引擎后台的线程主要分为四类&#xff1a;

Golang之路---02 基础语法——函数

函数 函数定义 func function_name( [parameter list] ) [return_types] {函数体 }参数解释&#xff1a; func&#xff1a;函数由 func 开始声明function_name&#xff1a;函数名称&#xff0c;函数名和参数列表一起构成了函数签名。[parameter list]&#xff1a;参数列表&a…

Istio 安全 mTLS认证 PeerAuthentication

这里定义了访问www.ck8s.com可以使用http也可以使用https访问&#xff0c;两种方式都可以访问。 那么是否可以强制使用mtls方式去访问&#xff1f; mTLS认证 PeerAuthentication PeerAuthentication的主要作用是别人在和网格里的pod进行通信的时候&#xff0c;是否要求mTLS mTL…