两种快速排序的c++实现

1.双边循环法

子循环内是先从右往左找还是先从左往右找,取决于基准元素的位置是首还是尾。

#include <iostream>
#include <time.h>
using namespace std;
const int N = 16;
const int C = 64;
// 对数组的[low,high]区间进行一次划分,返回基准元素的位置
int partition(int a[], int low, int high) {
    int pivot = low;
    while (low < high) {
        while (low < high && a[high] >= a[pivot]) {
            high--;
        }
        while (low < high && a[low] <= a[pivot]) {
            low++;
        }
        if (low < high) {
            int t = a[high];
            a[high] = a[low];
            a[low] = t;
        }
    }
    int t = a[pivot];
    a[pivot] = a[low];
    a[low] = t;
    return low; // 基准元素的位置
}
void qSort(int a[], int low, int high) {
    if (low < high) {
        // 分成两个子序列
        int pivotIndex = partition(a, low, high);
        qSort(a, low, pivotIndex - 1);
        qSort(a, pivotIndex + 1, high);
    }
}
void init(int a[], int n) {
    srand(time(NULL));
    printf("排序前的随机数组:\n");
    for (int i = 0; i < n; i++) {
        a[i] = rand() % C;
        cout << a[i] << " ";
    }
}
void check(int a[], int n) {
    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            printf("排序出错");
            return;
        }
    }
    printf("\n完成快速排序后的数组:\n");
    for (int i = 0; i < N; i++) {
        cout << a[i] << " ";
    }
}
int main() {
    int a[N] = {0};
    init(a, N);
    qSort(a, 0, N - 1);
    check(a, N);
    return 0;
}

2.单边循环法

只有partition的实现与双边循环法不同

#include <iostream>
#include <time.h>
using namespace std;
const int N = 16;
const int C = 64;
// 对数组的[low,high]区间进行一次划分,返回基准元素的位置
int partition(int a[], int low, int high) {
    int pivot = a[low];
    int pivotIndex = low;
    for (int i = low + 1; i <= high; i++) {
        if (a[i] < pivot) {
            pivotIndex++;
            int t = a[pivotIndex];
            a[pivotIndex] = a[i];
            a[i] = t;
        }
    }
    a[low] = a[pivotIndex];
    a[pivotIndex] = pivot;
    return pivotIndex; // 基准元素的位置
}
void qSort(int a[], int low, int high) {
    if (low < high) {
        // 分成两个子序列
        int pivotIndex = partition(a, low, high);
        qSort(a, low, pivotIndex - 1);
        qSort(a, pivotIndex + 1, high);
    }
}
void init(int a[], int n) {
    srand(time(NULL));
    printf("排序前的随机数组:\n");
    for (int i = 0; i < n; i++) {
        a[i] = rand() % C;
        cout << a[i] << " ";
    }
}
void check(int a[], int n) {
    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            printf("排序出错");
            return;
        }
    }
    printf("\n完成快速排序后的数组:\n");
    for (int i = 0; i < N; i++) {
        cout << a[i] << " ";
    }
}
int main() {
    int a[N] = {0};
    init(a, N);
    qSort(a, 0, N - 1);
    check(a, N);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值