Quicksort

快速排序算法实现
本文介绍了一个使用C++实现的快速排序算法。该算法通过递归地将数组分为较小和较大的两个子集,并对这两个子集进行排序来实现整个数组的排序。输入为一组整数,输出为升序排列的整数序列。

Description
#include <iostream>
using namespace std;
#define maxlen 100000
int partition(int a[], int p, int r)
{
    int x = a[r];
    int i = p - 1, j;
    for (j = p; j < r; j++)
    {
        if (a[j] <= x)
        {
            i = i + 1;
            int temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }
    int temp2 = a[i + 1];
    a[i + 1] = a[r];
    a[r] = temp2;
    return i + 1;
}
 
void quicksort(int a[], int first, int last)
{
    if (first < last)
    {
        int q = partition(a, first, last);
        quicksort(a, first, q - 1);
        quicksort(a, q, last);
    }
}
 
int main()
{
    int n;
    while (cin >> n)
    {
        int a[maxlen];
        int i;
        for (i = 0; i < n; i++)
            cin >> a[i];
        quicksort(a, 0, n - 1);
        for (i = 0; i < n-1; i++)
            cout << a[i]<<" ";
        cout << a[i];
        cout << endl;
    }
    return 0;
}
/**************************************************************
    Problem: ****
    User: Avivadepp
    Language: C++
    Result: Accepted
    Time:4 ms
    Memory:1596 kb
****************************************************************/

快速排序

Input

For each of the input, the first line of the input will be the size of the array (size < 1,000,000), and the second line is the integers in the array, separated by commas.

Output

For each of the input, your program should use quicksort to sort the integers in increasing order and output them.

Sample Input

31 3 2103 4 6 1 2 6 0 -1 2 5

Sample Output

1 2 3-1 0 1 2 2 3 4 5 6 6

HINT

### C++ 中 QuickSort 排序算法的实现与用法 #### 1. 快速排序算法简介 快速排序是一种分治策略的高效排序算法,它通过选取一个基准值(pivot),将数据划分为两部分:一部分小于等于基准值,另一部分大于基准值。随后递归地对这两部分分别进行排序[^2]。 #### 2. 快速排序的核心函数 `partition` `partition` 函数用于重新排列数组元素并返回基准值的位置。以下是其实现逻辑: - 基准值通常选为子数组的最后一个元素。 - 使用两个指针变量 `i` 和 `j` 遍历子数组: - 当前元素小于基准值时,交换该元素到左侧区域。 - 否则继续遍历直到完成整个子数组。 - 最终将基准值放置在其最终位置上,并返回此索引。 ```cpp int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j <= high - 1; ++j) { if (arr[j] < pivot) { ++i; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } ``` 上述代码展示了如何划分数组并找到基准值的正确位置[^3]。 #### 3. 主要递归函数 `quickSort` `quickSort` 是核心递归函数,负责调用 `partition` 来分割数组并对左右两侧分别执行相同操作。 ```cpp void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } ``` 这段代码定义了一个递归过程,其中每次调用都会缩小待处理范围直至完全有序。 #### 4. 完整示例程序 下面提供完整的 C++ 程序演示如何使用自定义的 `quickSort` 方法对数组进行升序排序。 ```cpp #include <iostream> using namespace std; // Partition function as described earlier. int partition(int arr[], int low, int high); // Recursive quick sort implementation. void quickSort(int arr[], int low, int high); // Utility to print array elements. void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } int main() { int data[] = {8, 7, 6, 1, 0, 9, 2}; int n = sizeof(data)/sizeof(data[0]); cout << "Unsorted Array:\n"; printArray(data, n); quickSort(data, 0, n - 1); cout << "\nSorted Array in Ascending Order:\n"; printArray(data, n); return 0; } // Implementation of the functions defined above. int partition(int arr[], int low, int high){ int pivot = arr[high]; int i = low - 1; for (int j = low; j <= high - 1; ++j){ if (arr[j] < pivot){ ++i; swap(arr[i], arr[j]); } } swap(arr[i+1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high){ if(low < high){ int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } ``` 以上是一个典型的例子,说明了如何利用手动编写的 `quickSort` 对一组随机数列实施排序。 #### 5. STL 提供的标准模板库版本 除了自己编写外,在实际开发过程中更推荐直接采用 `<algorithm>` 头文件里的 `std::sort()` 或者稳定版 `std::stable_sort()` 进行排序工作,它们内部采用了优化后的混合排序方法(通常是 IntroSort)。这些内置功能不仅性能优越而且经过严格测试更加可靠[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值