#include <iostream>
using namespace std;
void print(int arr[], int n){
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout <<endl;
}
void quickSort(int arr[], int low, int high) {
int l = low, h = high;
int x = arr[l];
while (l < h) {
while (l < h && arr[h] >= x) {
h--;
}
if (l < h) {
arr[l++] = arr[h];
}
while (l < h && arr[l] <= x) {
l++;
}
if (l < h) {
arr[h--] = arr[l];
}
arr[l] = x;
quickSort(arr, low, l - 1);
quickSort(arr, l + 1, high);
}
}
void Down(int arr[], int cur, int n) {
int parent = cur;
int child = cur * 2 + 1;
while (child < n) {
if (child + 1 < n && arr[child] < arr[child + 1]) {
child++;
}
if (child < n && arr[child] > arr[parent]) {
swap(arr[child], arr[parent]);
parent = child;
}
child = child * 2 + 1;
}
}
void buildHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
Down(arr, i, n);
}
}
void heapSort(int arr[], int n) {
buildHeap(arr, n);
for (int i = n- 1; i > 0; i--) {
swap(arr[0], arr[i]);
Down(arr, 0, i);
}
}
void shellInsertSort(int arr[], int n, int dk){
for (int i = dk; i < n; i++) {
if (arr[i] < arr[i - dk]) {
int j = i - dk;
int x = arr[i];
while (x < arr[j]) {
arr[j + dk] = arr[j];
j -= dk;
}
arr[j + dk] = x;
}
}
}
void shellSort(int arr[], int n) {
int dk = n / 2;
while (dk >= 1) {
shellInsertSort(arr, n, dk);
dk /= 2;
}
}
void insertSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
int j = i - 1;
int x = arr[i];
while (arr[i] < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = x;
}
}
}
void selectSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[i]) {
k = j;
}
}
swap(arr[i], arr[k]);
}
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
swap(arr[i], arr[j]);
}
}
}
}
void merge(int arr[], int l, int r, int m) {
int i = l, j = m + 1, k = 0, s = r - l + 1;
int *ans = new int[s];
while (i <= m && j <= r) {
if (arr[i] < arr[j]) {
ans[k++] = arr[i++];
}
else {
ans[k++] = arr[j++];
}
}
while (i <= m) ans[k++] = arr[i++];
while (j <= r) ans[k++] = arr[j++];
}
void mergeSort(int arr[],int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, r, m);
}
}
void bucketSort(int arr[], int n) {
int bucket[205] = {0};
for (int i = 0; i < n; i++) {
bucket[arr[i]]++;
}
for (int i = 0, j = 0; i < 205; i++) {
while (bucket[i] > 0) {
arr[j] = i;
bucket[i]--;
j++;
}
}
}
int main(){
int n;
int arr[205] = {0};
cout << "print the num n:"<<endl;
cin >> n;
cout << "print the arr:" <<endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
print(arr, n);
cout << "quickSort:" << endl;
quickSort(arr, 0, n - 1);
print(arr, n);
cout << "heapSort:" << endl;
heapSort(arr, n);
print(arr,n);
cout <<"shellSort:" <<endl;
shellSort(arr, n);
print(arr, n);
cout << "insertSort:" << endl;
insertSort(arr, n);
print(arr, n);
cout << "selectSort:" << endl;
selectSort(arr, n);
print(arr, n);
cout << "bubbleSort:" << endl;
bubbleSort(arr, n);
print(arr, n);
cout << "mergeSort:" << endl;
mergeSort(arr, 0, n - 1);
print(arr, n);
cout << "bucketSort:" << endl;
bucketSort(arr, n);
print(arr, n);
return 0;
}
八大排序算法
最新推荐文章于 2023-10-13 14:39:20 发布