1101. Quick Sort (25)

本文提供了一种解决 PAT A 1101 问题的有效方法,通过使用两个数组 min_so_far 和 max_so_far 来分别记录每个位置右侧的最小值和左侧的最大值,从而确定哪些元素可以作为 pivot。此外,还特别注意了输出格式的要求。

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

题目链接:https://www.patest.cn/contests/pat-a-practise/1101

思路:设置两个数组int min_so_far[N],max_so_far[N]
假设在位置pos,namemin_so_far[pos] 用来记录从pos到n-1之间的最小值,即位置pos右边的最小值; max_so_far[N] 用来记录从位置0到pos的最大值,即位置左边的最大值。记录这两个值以便判断位置pos上的元素是否可以作为pivot。理解了这两个数组的作用,剩下的就很简单了。
还有要注意输出的格式,[衰],花了好长时间才注意到!

#include <iostream>
#include <vector>
#include <math.h>
#include<algorithm>
using namespace std;
#define N 100000
int main()
{
    int n,num[N],min_so_far[N],max_so_far[N];
    vector<int> seq;//存储结果
    cin>>n;//n为正数,即n>0
    for(int i=0;i<n;i++)
    {
        cin>>num[i];//数据输入
    }
    //进行初始化处理
    min_so_far[n-1]=num[n-1];
    max_so_far[0]=num[0];
    for(int i=1,j=n-2;i<n;i++,j--)//大于等于2个数据的情况
    {
        //min_so_far[j]存储从位置n-1到j之间最小的值
        min_so_far[j]=min(min_so_far[j+1],num[j]);
        //max_so_far[i]存储从0到i之间最大的值
        max_so_far[i]=max(max_so_far[i-1],num[i]);
    }
    /* for( int i=0;i<n;i++ )
    {
        //cout<<num[i]<<endl;
        cout<<min_so_far[i]<<"****"<<max_so_far[i]<<endl;
    } */
    if( min_so_far[0]>=num[0] )//等于号为min_so_far[0]为num[0]本身的情况
        seq.push_back(num[0]);
    for( int i=1;i<n-1;i++ )//处理中间的数据
    {
        if( num[i]<=min_so_far[i] && num[i]>=max_so_far[i] )
            seq.push_back(num[i]);
    }
    if( n-1>0 && num[n-1]>=max_so_far[n-1] ) //处理最后一个数据
        seq.push_back(num[n-1]);

    int length=seq.size();//获取长度
    cout<<length<<endl;
    sort(seq.begin(),seq.end());//排序
    //以下控制输出格式。尤其是else语句没有进行控制,浪费好长时间
    if( length>0 )
    {
        cout<<seq[0];
        for(int i=1;i<length;i++)
            cout<<" "<<seq[i];
        cout<<endl;
    }
    else
        cout<<endl;
}
Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值