常见排序算法实现
#include "pch.h"
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;
//选择排序
void selectSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[i]) {
swap(arr[j], arr[i]);
}
}
}
}
//插入排序
void insertSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
for (int j = i; j >=0; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr[j], arr[j - 1]);
}
else
break;
}
}
}
//改进的插入排序
void new_insertSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int j;
int tmp = arr[i];
for (j = i; j >= 0; j--) {
if (tmp < arr[j - 1]) {
arr[j] = arr[j - 1];
}
else
break;
}
arr[j] = tmp;
}
}
//归并排序自顶向下
void __merge(int arr[], int left, int mid, int right) {
int *tmp = new int[right - left + 1];
for (int i = left; i <= right; i++) {
tmp[i - left] = arr[i];
}
int i = left, j = mid + 1;
for (int k = left; k <= right; k++) {
if (i > mid) {
arr[k] = tmp[j - left];
j++;
}
else if (j > right) {
arr[k] = tmp[i - left];
i++;
}
else if (tmp[i - left] < tmp[j - left]) {
arr[k] = tmp[i - left];
i++;
}
else {
arr[k] = tmp[j - left];
j++;
}
}
delete []tmp;
}
void __mergeSort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
__mergeSort(arr, left, mid);
__mergeSort(arr, mid + 1, right);
__merge(arr, left, mid, right);
}
void mergeSort(int arr[], int n) {
__mergeSort(arr, 0, n - 1);
}
//归并排序自低向上
void mergeSortBU(int arr[], int n) {
for (int size = 1; size <= n; size += size) {
for (int i = 0; i + size < n; i += size + size) {
__merge(arr, i, i + size - 1, min(i + size + size - 1,n - 1));
}
}
}
//快速排序
int __partition(int arr[], int left, int right) {
int k = arr[left];
int j = left;
for (int i = left + 1; i <= right; i++) {
if (arr[i] < k) {
swap(arr[j + 1], arr[i]);
j++;
}
}
swap(arr[left], arr[j]);
return j;
}
void __quickSort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int k = __partition(arr, left, right);
__quickSort(arr, left, k - 1);
__quickSort(arr, k + 1, right);
}
void quickSort(int arr[], int n) {
__quickSort(arr, 0, n - 1);
}
//三路快排
void __quickSort3Ways(int arr[], int left, int right) {
if (left >= right) {
return;
}
int k = arr[left];
int j = left;
int lt = left;
int rt = right + 1;
int i = left + 1;
while (i < rt) {
if (arr[i] > k) {
swap(arr[i], arr[rt - 1]);
rt--;
}
else if (arr[i] < k) {
swap(arr[i], arr[lt + 1]);
lt++;
i++;
}
else
{
i++;
}
}
swap(arr[left], arr[lt]);
__quickSort3Ways(arr, left, lt);
__quickSort3Ways(arr, rt, right);
}
void quickSort3Ways(int arr[], int n) {
__quickSort3Ways(arr, 0, n - 1);
}
//堆排序
//建堆
class MaxHeap {
private:
int *data;
int count;
void shiftUp(int n) {
while (n > 1&&data[n / 2] < data[n]) {
swap(data[n / 2], data[n]);
n = n / 2;
}
}
void shiftDown(int k) {
while (2 * k < count) {
int j = 2 * k;
if (j + 1 < count && data[j + 1] > data[j])
{
j = j + 1;
}
if (data[k] > data[j]) {
break;
}
swap(data[k], data[j]);
k = j;
}
}
public:
MaxHeap(int capacity) {
data = new int[capacity + 1];
}
~MaxHeap(){
delete[]data;
}
int size() {
return count;
}
bool empty() {
return count == 0;
}
void insert(int n) {
data[count + 1] = n;
count++;
shiftUp(count);
}
int extractMax() {
if (count > 0) {
int res = data[1];
data[1] = data[count];
count--;
shiftDown(1);
return res;
}
}
};
void heapSort(int arr[], int n) {
MaxHeap maxheap = MaxHeap(n);
for (int i = 0; i < n; i++) {
maxheap.insert(arr[i]);
}
for (int i = n - 1; i >= 0; i--) {
arr[i] = maxheap.extractMax();
}
}
int main()
{
int a[10] = { 1,4,3,7,5,8,9,6,11,10 };
//selectSort(a, 10);
//insertSort(a, 10);
//new_insertSort(a, 10);
//mergeSort(a, 10);
//mergeSortBU(a, 10);
//quickSort(a, 10);
//quickSort3Ways(a, 10);
heapSort(a, 10);
for (int i = 0; i < 10; i++) {
cout << a[i] << endl;
}
}