归并,快排,冒泡排序,c++合集

#include "iostream"
using namespace std;
class A
{
private:
	void merge(int arr[], int left, int middle, int right)
	{
		int i, j, k;
		int n1 = middle - left + 1;
		int n2 = right - middle;
		int* L = new int[n1];
		int* R = new int[n2];

		for (i = 0; i < n1; i++)
			L[i] = arr[left+i];
		for (j = 0; j < n2; j++)
			R[j] = arr[middle+1+j];
		i = 0;
		j = 0;
		k = left;
		while (i < n1 && j < n2)
		{
			if (L[i] < R[j])
				arr[k++] = L[i++];
			else
				arr[k++] = R[j++];
		}
		while (i < n1)arr[k++] = L[i++];
		while (j < n2)arr[k++] = R[j++];
	}
public:
	void quick_sort(int arr[], int left, int right)
	{
		if (left >= right)return;
		int i = left, j = right, temp = arr[left];
		while (i < j)
		{
			while (arr[j] >= temp && i < j)j--;
			while (arr[i] <= temp && i < j)i++;
			if (i < j)
			{
				int k = arr[i];
				arr[i] = arr[j];
				arr[j] = k;
			}
		}
		arr[left] = arr[i];
		arr[i] = temp;
		quick_sort(arr,left, i - 1);
		quick_sort(arr,i + 1, right);
	}
	void merge_sort(int arr[], int left, int right)
	{
		if (left >= right)return;
		int middle = (right + left) / 2;
		merge_sort(arr, left, middle);
		merge_sort(arr, middle + 1, right);
		merge(arr, left, middle, right);
	}
	void pop_sort(int arr[], int len)
	{
		for (int i = 0; i < len; i++)
		{
			for (int j = 0; j < len - i - 1; j++)
			{
				if (arr[j] > arr[j + 1])
				{
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
	}
};
int main(void)
{
	int arr[10] = { 8,12,24,4,9,6,4,2,1,3 };
	A var;
	var.pop_sort(arr,10);
	for (int i = 0; i < 10; i++)
		cout << arr[i] << " ";
	cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值