c++实现各种排序

本文详细介绍了使用C++实现的七大经典排序算法,包括归并排序、冒泡排序、Shell排序、选择排序、插入排序、堆排序和快速排序,涵盖了不同类型的排序思想和方法。

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

 一、归并排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<set>
#include <cstdlib>
#include <cstddef>
#include <list>
#include <functional>
#include <unordered_map>
#include "tes2.h"
#include <algorithm>
#include <iterator>
#include <ostream>



using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::list;
using std::unordered_map;



void Msort(int *mat, int *temp, int left, int right);
void Merge(int *mat, int *temp, int left, int middle, int right);
void mergeSort(int *mat,int n)
{
	if (n ==  1) 
		return;
	int *temp = new int[n];
	Msort(mat, temp, 0, n - 1);

	delete[]temp;
	

}

void Msort(int *mat, int *temp, int left, int right)
{
	if (left < right)
	{
		int middle = (left + right) / 2;
		Msort(mat, temp, left, middle);
		Msort(mat, temp, middle+1, right);
		Merge(mat, temp, left, middle, right);
	}
}

void Merge(int *mat, int *temp, int left, int middle, int right)
{
	int first = left;
	int result = left;
	int endfirst = middle;
	int endsecond = right;
	middle++;
	while (left <= endfirst && middle <= endsecond)
	{
		if (mat[left] <= mat[middle])
			temp[result++] = mat[left++];
		else
			temp[result++] = mat[middle++];
	}
	while(left<=endfirst)
		temp[result++] = mat[left++];
	while (middle <= endsecond)
		temp[result++] = mat[middle++];

	for (int i = first; i < result; i++)
		mat[i] = temp[i];
}

int main()
{
	int a[] = {8,2,5,3,6,9,1,4,7};
	
	mergeSort(a, 9);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	
	
	
	system("pause");
	return 0;
}

二、冒泡排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;

void Bubblesort(int *a, int len)
{
	bool F = false;
	int temp;
	for (int i = 0; i < len-1; i++)
	{
		F = false;
		for (int j = 0; j < len - i-1; j++)
		{
			if (a[j] > a[j + 1])
			{
				temp = a[j + 1];
				a[j + 1] = a[j];
				a[j] = temp;
				F = true;
			}
			
		}
		if (F == false)
			break;
	}
}

int main(int argc, char * argv[])
{
	

	int a[10] = { 49,38,65,97,76,13,27,49,55,04};
	Bubblesort(a, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}

三、shell排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;

void Shellsort(int *a, int len)
{
	int temp;
	for (int incr = len / 2; incr > 0; incr /= 2)
	{
		for (int i = incr; i < len; i++)
		{
			for (int j = i; j - incr >= 0; j -= incr)
			{
				if (a[j] < a[j - incr])
				{
					temp = a[j - incr];
					a[j - incr] = a[j];
					a[j] = temp;
				}
				else
					break;
			}
		}
	}
}

int main(int argc, char * argv[])
{
	

	int a[10] = { 49,38,65,97,76,13,27,49,55,04};
	Shellsort(a, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}

四、选择排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::swap;

void Selectsort(int *a, int len)
{
	bool F = false;
	int max;
	for (int i = 0; i < len-1; i++)
	{
		max=0;
		F = false;
		
		for (int j = 1; j < len - i; j++)
		{
			
			if (a[max] <= a[j])
			{
				max = j;

			}
			else
				F = true;
			
		}
	
		if (F == false)
			break;
		
		swap(a[max], a[len - i-1]);
		
		
	}
}

int main(int argc, char * argv[])
{
	

	int a[10] = { 49,38,65,97,76,13,27,49,55,04};
	Selectsort(a, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}

五、插入排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::swap;

void Insertsort(int *a, int len)
{
	
	for (int i = 1; i < len; i++)
	{
		
		for (int j = i; j >0; j--)
		{
			
			if (a[j] < a[j - 1])
			{
				swap(a[j], a[j - 1]);

			}
			else
				break;
			
			
		}
		
	}
}

int main(int argc, char * argv[])
{
	

	int a[10] = { 49,38,65,97,76,13,27,49,55,04};
	Insertsort(a, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}

六、堆排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::swap;

void Percdown(int *a, int len)
{
	int child;
	for (int i = (len - 1 - 1) / 2; i >= 0; i--)
	{
		for (int j = i; 2 * j + 1 < len; j = child)
		{
			child = 2 * j + 1;
			if (2 * j + 1<len - 1 && a[2 * j + 2] > a[2 * j + 1])
				child = 2 * j + 2;
			if (a[j] < a[child])
				swap(a[j], a[child]);
			else
				break;
		}

	}
}

void Heapsort(int *a, int len)
{
	Percdown(a, len);
	for (int i = 0; i <len - 1; i++)
	{
		swap(a[0], a[len - 1 - i]);
		Percdown(a, len - i - 1);


	}
}

int main(int argc, char * argv[])
{


	int a[10] = { 49,38,65,97,76,13,27,49,55,04 };
	Heapsort(a, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}


 

七、快速排序

#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<list>



using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::swap;

int median(int *a,int start,int end)
{
	int center = (start + end)/2;
	//冒泡排序
	if (a[start] > a[center])
		swap(a[start], a[center]);
	if (a[end - 1] < a[center])
		swap(a[end - 1], a[center]);
	if (a[start] > a[center])
		swap(a[start], a[center]);
	
	swap(a[end - 1], a[center]);
	return a[end - 1];
}

void Quicksort(int *a, int start,int end)
{
	if (end - start <= 1)
		return;
	
	else {
		int pivot = median(a, start, end);
		int i = start, j = end - 2;
		while (1)
		{
			while (a[i] < pivot)
				i++;
			while (a[j] >pivot)
				j--;
			if (i < j)
			{
				swap(a[i], a[j]);
			}	
			else
				break;
		}
		swap(a[i], a[end - 1]);
		Quicksort(a, start, i);
		Quicksort(a, i+1, end);

	}
	
	
}

int main(int argc, char * argv[])
{
	

	int a[10] = { 49,38,65,97,76,13,27,49,55,04};
	
	Quicksort(a,0, 10);
	for (auto v : a)
		cout << v << " ";
	cout << endl;
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值