代码系列1:c++实现的7种排序算法

#include <iostream>
using namespace std;

void bubbleSort(int list[], int length) {
	for (int i = length; i > 0; i--) {
		for (int j = 1; j < i; j++) {
			if (list[j] < list[j - 1]) {
				int tmp = list[j];
				list[j] = list[j - 1];
				list[j - 1] = tmp;
			}
		}
	}
}

void insertSort(int list[], int length) {
	for (int i = 0; i < length; i++) {
		int current = list[i];
		int position = i;
		for (int j = i - 1; j >= 0; j--) {
			if (current < list[j]) {
				list[position] = list[j];
				position = j;
			}
		}
		list[position] = current;
	}
}

void selectSort(int list[], int length) {
	for (int i = 0; i < length; i++) {
		int min = list[i];
		int position = i;
		for (int j = i; j < length; j++) {
			if (min > list[j]) {
				min = list[j];
				position = j;
			}
		}
		list[position] = list[i];
		list[i] = min;
	}
}

void shellSort(int list[], int lenght) {
	for (int d = lenght / 2; d > 0; d = d / 2) {
		for (int i = 0; i < lenght; i++) {
			int current = list[i];
			int position = i;
			for (int j = i; j >= 0; j -= d) {
				if (current < list[j]) {
					list[position] = list[j];
					position = j;
				}
			}
			list[position] = current;
		}
	}
}

int quickSortHelper(int list[], int front, int end) {
	int current = list[front];
	bool isFromEnd = true;
	while (front < end) {
		if (isFromEnd) {
			if (current < list[end]) {
				end--;
			} else {
				list[front] = list[end];
				list[end] = current;
				front++;
				isFromEnd = false;
			}
		} else {
			if (current > list[front]) {
				front++;
			} else {
				list[end] = list[front];
				list[front] = current;
				end--;
				isFromEnd = true;
			}
		}
	}
	return front;
}

void quickSort(int list[], int front, int end) {
	if (front < end) {
		int mid = quickSortHelper(list, front, end);
		quickSort(list, front, mid - 1);
		quickSort(list, mid + 1, end);
	}
}

void mergeSortHelper(int list[], int front, int end) {
	int mid = (front + end) / 2;
	int leftLenght = mid - front + 1;
	int rightLenght = end - mid;
	int leftList[leftLenght];
	int rightList[rightLenght];
	int leftPosition = 0;
	int rightPosition = 0;
	for (int i = 0; i < leftLenght; i++) {
		leftList[i] = list[front + i];
	}
	for (int j = 0; j < rightLenght; j++) {
		rightList[j] = list[mid + 1 + j];
	}
	for (int k = front; k <= end; k++) {
		if (leftPosition < leftLenght && rightPosition < rightLenght) {
			if (leftList[leftPosition] < rightList[rightPosition]) {
				list[k] = leftList[leftPosition];
				leftPosition++;
			} else {
				list[k] = rightList[rightPosition];
				rightPosition++;
			}
		} else if (leftPosition < leftLenght) {
			list[k] = leftList[leftPosition];
			leftPosition++;
		} else if (rightPosition < rightLenght) {
			list[k] = rightList[rightPosition];
			rightPosition++;
		}
	}
}

void mergeSort(int list[], int front, int end) {
	if (front < end) {
		int mid = (front + end) / 2;
		mergeSort(list, front, mid);
		mergeSort(list, mid + 1, end);
		mergeSortHelper(list, front, end);
	}
}

void heapAdjust(int list[], int node, int end) {
	int leftchild = 2 * node + 1;
	int rightchild = 2 * node + 2;
	int maxPosition = node;
	if(node <= (end - 1) / 2) {
		if(leftchild <= end) {
			maxPosition = list[leftchild] > list[maxPosition] ? leftchild : maxPosition;
		}
		if(rightchild <= end) {
			maxPosition = list[rightchild] > list[maxPosition] ? rightchild : maxPosition;
		}
		if(maxPosition != node) {
			int tmp = list[node];
			list[node] = list[maxPosition];
			list[maxPosition] = tmp;
			heapAdjust(list, maxPosition, end);
		}
	}
}

void heapBuild(int list[], int end) {
	for(int i = (end - 1) / 2; i >= 0; i--) {
		heapAdjust(list, i, end);
	}
}

void heapSort(int list[], int end) {
	heapBuild(list, end);
	for(int i = end; i >= 0; i--) {
		int tmp = list[i];
		list[i] = list[0];
		list[0] = tmp;
		heapAdjust(list, 0, i - 1);
	}
}

int main(int argc, char **argv) {
	int array[] = { 9, 5, 6, 3, 1, 4, 8, 2, 0, 7 };
	int len = sizeof array / sizeof array[0];
//	bubbleSort(array, len);
//	insertSort(array, len);
//	selectSort(array, len);
//	shellSort(array, len);
//	quickSort(array, 0, len - 1);
//	mergeSort(array, 0, len - 1);
	heapSort(array, len - 1);
	for (int i = 0; i < 10; i++) {
		cout << array[i] << " ";
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值