首页 > 代码库 > 常见排序算法(冒泡、选择、插入、快速、归并C++实现)

常见排序算法(冒泡、选择、插入、快速、归并C++实现)

常见排序算法(冒泡、选择、插入、快速、归并C++实现)

#include <iostream>
using namespace std;
// 冒泡排序
void bubbleSort (int data[], size_t size) {
	for (size_t i = 0; i < size - 1; ++i) {
		bool ordered = true;
		for (size_t j = 0; j < size - 1 - i; ++j)
			if (data[j+1] < data[j]) {
				int temp = data[j+1];
				data[j+1] = data[j];
				data[j] = temp;
				ordered = false;
			}
		if (ordered)
			break;
	}
}
// 插入排序
void insertSort (int data[], size_t size) {
	for (size_t i = 1; i < size; ++i) {
		int temp = data[i];
		size_t j;
		for (j = i; j > 0 && temp < data[j-1]; --j)
			data[j] = data[j-1];
		if (j != i)
			data[j] = temp;
	}
}
// 选择排序
void selectSort (int data[], size_t size) {
	for (size_t i = 0; i < size - 1; ++i) {
		size_t min = i;
		for (size_t j = i + 1; j < size; ++j)
			if (data[j] < data[min])
				min = j;
		if (min != i) {
			int temp = data[i];
			data[i] = data[min];
			data[min] = temp;
		}
	}
}
// 快速排序
void quickSort (int data[], size_t left,
	size_t right) {
	size_t p = (left + right) / 2;
	int pivot = data[p];
	for (size_t i = left, j = right; i < j;) {
		while (! (i>= p || pivot < data[i]))
			++i;
		if (i < p) {
			data[p] = data[i];
			p = i;
		}
		while (! (j <= p || data[j] < pivot))
			--j;
		if (j > p) {
			data[p] = data[j];
			p = j;
		}
	}
	data[p] = pivot;
	if (p - left > 1)
		quickSort (data, left, p - 1);
	if (right - p > 1)
		quickSort (data, p + 1, right);
}
// 异地合并
void merge (int data1[], size_t size1, int data2[],
	size_t size2, int data3[]) {
	size_t i = 0, j = 0, k = 0;
	for (;;)
		if (i < size1 && j < size2)
			if (data1[i] <= data2[j])
				data3[k++] = data1[i++];
			else
				data3[k++] = data2[j++];
		else if (i < size1)
			data3[k++] = data1[i++];
		else if (j < size2)
			data3[k++] = data2[j++];
		else
			break;
}
// 本地合并
void merge (int data[], size_t l, size_t m,
	size_t r) {
	int* res = new int[r-l+1];
	merge (data+l, m-l+1, data+m+1, r-m, res);
	for (size_t i = 0; i < r-l+1; ++i)
		data[l+i] = res[i];
	delete[] res;
}
// 归并排序
void mergeSort (int data[], size_t left,
	size_t right) {
	if (left < right) {
		int mid = (left + right) / 2;
		mergeSort (data, left, mid);
		mergeSort (data, mid+1, right);
		merge (data, left, mid, right);
	}
}
int main (void) {
	int data[] = {13,23,20,12,15,31,19,26,24,37};
	size_t size = sizeof (data) / sizeof (data[0]);
//	bubbleSort (data, size);
//	insertSort (data, size);
//	selectSort (data, size);
//	quickSort (data, 0, size - 1);
	mergeSort (data, 0, size - 1);
	for (size_t i = 0; i < size; ++i)
		cout << data[i] << ' ';
	cout << endl;
	/*
	int data1[] = {10, 20, 30, 45, 66};
	int data2[] = {15, 18, 27, 33};
	int data3[9];
	merge (data1, 5, data2, 4, data3);
	for (size_t i = 0; i < 9; ++i)
		cout << data3[i] << ' ';
	cout << endl;
	// 10 15 18 20 27 30 33 45 66
	/*
	int data[] = {100,10,20,30,45,66,15,18,27,33,0};
	merge (data, 1, 5, 9);
	for (size_t i = 0; i < 11; ++i)
		cout << data[i] << ' ';
	cout << endl;
	// 100 10 15 18 20 27 30 33 45 66 0
	*/
	return 0;
}


常见排序算法(冒泡、选择、插入、快速、归并C++实现)