P1177 【模板】快速排序

本文深入探讨了快速排序算法的优化方案,对比了不同key值选取方式对算法效率的影响,并通过具体代码实现展示了优化前后的效果。文章提供了详细的C++代码示例,包括超时的原始方案和优化后的方案,适合对算法优化感兴趣的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.luogu.org/problem/P1177

 

#include <iostream>
#include <fstream>
using namespace std;

int n;
int inputList[100000];
bool test = false;

void swap(int i, int j)
{
    if(test)
    {
        cout << "i=" << i << ",j=" << j << endl;
    }
    if (i == j)
    {
        return;
    }

    int tmp = inputList[i];
    inputList[i] = inputList[j];
    inputList[j] = tmp;
    //cout << i << "," << j << "-";
    //for (int t = 0; t < n; t++)
    //{
    //    cout << inputList[t] << " ";
    //}
    //cout <<endl;
}

// 备注:此方案会超时,应该是key的取值不合适
// [start, end)
void quickSort(int start, int end)
{
    if (start+1 >= end)
    {
        return;
    }
    int key = inputList[start];
    int i = start;
    int j = end - 1;
    bool bFront = false;
    while ( i != j)
    {
        if (bFront)
        {
            if (inputList[i] >= key)
            {
                //cout << start << "," << end << " , ";
                swap(i, j);
                bFront = false;
                j--;
            }
            else
            {
                i++;
            }
        } 
        else
        {
            if (inputList[j] < key)
            {
                //cout << start << "," << end << " , ";
                swap(i, j);
                bFront = true;
                i++;
            }
            else
            {
                j--;
            }
        }
    }
    quickSort(start, i);
    quickSort(i+1, end);
}

// 优化后方案,key取中间值
// [start, end]
void quickSort2(int start, int end)
{
    //if (start+1 >= end)
    //{
    //    return;
    //}
    int key = inputList[(start+end)/2];

    if(test)
    {
        cout << "[" << start << "," << end << "] key=" << key << endl;
    }

    int i = start;
    int j = end;
    while ( i <= j)
    {
        while(i <= end && inputList[i] < key)
        {
            i++;
        }
        //while(j >= start && inputList[j] >= key)
        while(j >= start && inputList[j] > key)
        {
            j--;
        }
        if (i <= j)
        {
            swap(i, j);
            i++;
            j--;
        }
    }
    if(i < end && i > start)
    {
        quickSort2(i, end);
    }
    if (j > start && j < end)
    {
        quickSort2(start, j);
    }
}

int main()
{
    //test = true;
    if (test)
    {
        ifstream f("C:\\Users\\suyang\\Downloads\\testdata (5).in");
        if (f)
        {
            cout << "f is ok" << endl;
        }
        else
        {
            cout << "f is error" << endl;
        }
        f >> n;
        for(int i = 0; i < n; i++)
        {
            f>>inputList[i];
        }
        f.close();
        n = 50;
    } 
    else
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            cin>>inputList[i];
        }
    }


    quickSort2(0, n-1);
    //quickSort(0, n);

    if (test)
    {
        ofstream f("luogu_out.txt");
        if (f)
        {
            cout << "f is ok" << endl;
        }
        else
        {
            cout << "f is error" << endl;
        }
        //f << inputList[0];
        //for(int i = 1; i < n; i++)
        //{
        //    f << " " << inputList[i];
        //}
        //f << endl;
        for(int i = 0; i < n; i++)
        {
            f << inputList[i] << endl;
        }
        f.close();
    }
    else
    {
        cout << inputList[0];
        for(int i = 1; i < n; i++)
        {
            cout << " " << inputList[i];
        }
        cout << endl;
    }
    return 0;
}

 

p1177是《算法导论》(Introduction to Algorithms)这本书中的一个问题编号,它通常指的是书中关于排序算法的内容。在这本书中,排序算法是一个核心主题,其中包括许多经典算法如冒泡排序、插入排序、选择排序、希尔排序、归并排序、快速排序等。 具体到P1177,可能会涉及到某个特定排序算法的描述、分析其时间复杂度、实现细节或者是比较不同排序算法的优缺点。例如,快速排序在该页可能会有详细的递归实现讲解,而归并排序则可能涉及分治策略和合并过程。 如果你需要了解某种排序算法的具体步骤或者代码模板,比如“如何用C语言实现快速排序”,这里可以给你一个简单的快速排序的C语言模板: ```c #include <stdio.h> void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } 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); } } // 示例 int main() { int arr[] = {10, 7, 8, 9, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); printf("Sorted array: \n"); for (int i=0; i < n; i++) printf("%d ", arr[i]); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值