just_sort

★实验任务

给定两个序列 a b,序列 a 原先是一个单调递增的正数序列,但是由于某些 原因,使得序列乱序了,并且一些数丢失了(用 0 表示)。经过数据恢复后,找 到了正数序列 b ,且序列 a 中 0 的个数等于序列 b 的个数,打算使用序列 b 恢 复序列 a 。 对于序列 a 来说,我们可以交换两个位置上的非零的数,并且可以交换任意 次。序列 b 同样也可以进行任意次交换。 现在要将序列 b 填充到序列 a 中的值丢失的位置上,序列 b 中的每个数只能 填充一次,问最后构成的序列是否是单调递增的,如果是,则输出填充后的序列, 否则输出-1。

★数据输入

输入给定 N M,表示序列 a 和序列 b 的长度。 第一行为序列 a ,第二行为 序列 b。 题目保证除了 0 以外的数,在序列 a 和 b 中只出现一次。 数据保证: 80%的数据,N, M <= 100 100%的数据,N, M <= 100000, 0 <= a[i] <= 100000, 0 < b[i] <= 100000

★数据输出

如果最后序列 a 是单调递增的,输出该序列,否则输出-1。

测试样例

输入:

4 2

0 11 0 15

1 12

输出:1 11 12 15

解题思路:直接对输入的a数组中的0的位置存储,然后b数组中先排序一次,计入位置之后进行比对就行了;我这题SO的超级委屈,因为我直接把几个10万级的数组放在了main函数里头,放在栽面就过了。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
int num[100005], Zero[100005];
int temp[100005] = { 0 };
using namespace std;
int main()
{
    int i = 0, j = 0, k = 0;
    
    int n, m, tmp, ok = 0;
    cin >> n >> m;
    for (i = 0; i < n; i++)
    {
        cin >> tmp;
        if (tmp == 0)Zero[j++] = i;
        else num[k++] = tmp;
    }
    for (i = 0; i < m; i++)
    {
        cin >> tmp; temp[tmp]++;
        num[k++] = tmp;
    }
    sort(num, num + n);
    for (i = 0; i < j; i++)
    {
        if (temp[num[Zero[i]]] == 1) { temp[num[Zero[i]]]--; ok = 1; }
        else { ok = 0; break; }
    }
    if (ok == 1)
    {
        for (i = 0; i < n; i++)printf("%d%c", num[i], i != n - 1 ? ' ' : '\n');
    }
    else cout << "-1\n";
    return 0;

}

转载于:https://www.cnblogs.com/heihuifei/p/7818844.html

<think> Hmm, the user is asking about implementing Shell sort in both ascending and descending orders. Let me look at the references they provided. Reference [1] mentions the Linux sort command options like -n and -r, which is interesting but not directly about Shell sort implementation. References [2] and [3] discuss bubble sort and quick sort respectively, while reference [4] actually contains Shell sort code - perfect! The user seems to be learning different sorting algorithms. Given they specifically asked about Shell sort's direction control, they're probably comparing sorting methods or working on a programming task. The request is quite precise - they want both ascending and descending implementations. Looking at reference [4]'s Shell sort code, the core logic uses an insertion-sort-like approach within decreasing gaps. For ascending order, it shifts elements when key < arr[j-inc]. To reverse it, I just need to flip that comparison to key > arr[j-inc]. I should explain both implementations clearly, keeping the gap sequence discussion simple but mentioning common choices like Knuth's formula. The user might also appreciate seeing how the direction control compares to other sorts like in reference [2]'s bubble sort with its reverse traversal approach. Better include concrete code examples since reference [4] shows C++ and they seem comfortable with code. Should use clear variable names and comments. The time complexity note from reference [4] is worth keeping too - O(n^{3/2}) and unstable nature. For the related questions section, performance analysis and gap sequence choices would naturally follow from this topic. The Linux sort command reference [1] suggests they might be interested in real-world applications too. The user appears technical but might need the key modification point (the comparison flip) highlighted clearly. No signs of frustration - seems like a straightforward learning query. Better keep the tone helpful but precise. </think> 基于引用内容,Shell Sort(希尔排序)的正序与倒序实现主要通过**比较逻辑的方向控制**实现,以下是具体方法和代码实现: --- ### **一、Shell Sort 正序(升序)实现** **核心逻辑**:元素后移条件为 `key < arr[j-gap]`(小值前移) **引用参考**:引用[4]中 `key < arr[j-inc]` 实现升序排序 ```c void shellSort_asc(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { // 动态缩小间隔 for (int i = gap; i < n; i++) { int key = arr[i]; int j; // 若当前元素小于前隔gap位的元素,则后移较大值 for (j = i; j >= gap && key < arr[j-gap]; j -= gap) { arr[j] = arr[j-gap]; // 较大值后移 } arr[j] = key; // 插入正确位置 } } } ``` --- ### **二、Shell Sort 倒序(降序)实现** **核心修改**:翻转比较条件为 `key > arr[j-gap]`(大值前移) ```c void shellSort_desc(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int key = arr[i]; int j; // 修改条件:当前元素大于前隔gap位的元素时后移较小值 for (j = i; j >= gap && key > arr[j-gap]; j -= gap) { arr[j] = arr[j-gap]; // 较小值后移 } arr[j] = key; // 插入正确位置 } } } ``` --- ### **关键区别对照表** | 操作 | 正序条件 | 倒序条件 | 目标 | |--------------|------------------------|------------------------|--------------------------| | **比较逻辑** | `key < arr[j-gap]` | `key > arr[j-gap]` | 控制前移元素的大小方向 | | **数据移动** | 后移**较大值** | 后移**较小值** | 实现升序/降序的排列 | > **稳定性说明**:Shell Sort 是不稳定排序(引用[4]),相同元素在排序后相对位置可能改变。 --- ### **三、扩展应用** 1. **动态间隔序列优化** 可替换 `gap = n/2` 为更高效的序列(如Hibbard序列): ```c int gap = 1; while (gap < n/3) gap = gap*3 + 1; // 生成Hibbard序列 ``` 2. **组合命令实现**(引用[1]) 在Linux中直接使用 `sort` 命令: ```bash sort -n file.txt # 正序(数值排序) sort -nr file.txt # 倒序(数值逆序) ``` --- **相关问题** 1. Shell Sort 的时间复杂度如何分析?不同间隔序列对性能有何影响? 2. 如何证明 Shell Sort 是不稳定排序算法? 3. Shell Sort 与插入排序相比有哪些改进?在什么场景下更具优势? [^3][^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值