标签: sorting

为什么使用std :: sort()会给我垃圾值?

我试图解决必须排序的问题,所以我使用了标准库std::sort函数,但是在第二个测试用例中却得到了错误的输出:

#include <bits/stdc++.h> 

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,i;
        cin>>n;
        int arr[n-1];

        for(i=1;i<=n-1;i++)
            cin>>arr[i];

        int size=sizeof(arr)/sizeof(arr[1]);
        sort(arr,arr+size); 

        for(i=1;i<=n-1;i++)
            cout<<arr[i]<<" ";

        cout<<endl;         
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入/输出:

2
5
1 2 5 4  
10
1 2 3 4 5 6 7 8 10
Run Code Online (Sandbox Code Playgroud)

预期O / P:

1 2 4 5
1 2 3 4 5 6 7 8 10
Run Code Online (Sandbox Code Playgroud)

实际O / P:

1 2 4 5
2 3 4 5 6 7 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting

1
推荐指数
1
解决办法
164
查看次数

Why does sorted "tan" != "ant"?

I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.

package main

import (
    "fmt"
    "sort"
)

func main() {
    for _, s := range []string{"nat", "tan", "ant"} {
        b := []byte(s)
        sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
        fmt.Println(s, string(b))
    }
}
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/bC9QWq7aF3G

I …

sorting string go

1
推荐指数
1
解决办法
90
查看次数

为什么“快速排序”算法的这两种变体在性能上有如此大的差异?

最初,我想出了一些排序算法以C ++进行编码以进行实践。人们告诉我这是非常低效的(实际上,对数百个数字进行排序大约需要10秒钟)。该算法是记住向量中的第一个元素(“枢轴”),然后解析每个其他元素,然后将每个元素移到枢轴的左侧(如果较小),否则将不执行任何其他操作。这会将列表分成较小的列表进行排序;其余的通过递归完成。

因此,现在我知道将列表分为两部分并进行这样的递归本质上是快速排序的工作(尽管如何进行分区有很多变体)。我不明白为什么我的原始代码效率低下,所以我写了一个新的代码。有人提到这是因为insert()和delete()函数,所以我确保不要使用那些,而要使用swap()。

旧(慢):

void sort(vector<T>& vec){
  int size = vec.size();
  if (size <= 1){ //this is the most basic case
    return;
  }

  T pivot = vec[0];
  int index = 0; //to help split the list later
  for (int i = 1; i < size; ++i){ //moving (or not moving) the elements
    if (vec[i] < pivot){
      vec.insert(vec.begin(), vec[i]);
      vec.erase(vec.begin() + i + 1);
      ++index;
    }
  }

  if (index == 0){ //in case the 0th element is the smallest
    vec.erase(vec.begin());
    sort(vec); …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm

1
推荐指数
1
解决办法
118
查看次数

为什么默认情况下分组查询对A到Z进行排序?

我创建了一个查询,以从表的列中获取通过该查询不为空的值:

Select * from(
SELECT       OEID, Chest_Pain as Head, Chest_PainComment as Detail
FROM            tblComplaints
union

SELECT       OEID, SOB as Head, SOBComment as Detail
FROM            tblComplaints
union

SELECT        OEID, PND as Head, Cyanosis as Detail FROM tblComplaints
union

SELECT        OEID, Odema_Feet as Head, Vertigo + as Detail  From tblComplaints
union

SELECT       OEID, DM as Head, DMComment as Detail
FROM            tblComplaints
union

SELECT       OEID, RS as Head, RSComment as Detail
FROM            tblComplaints

) as t
where (Head is not null and ltrim(rtrim(Head)) …
Run Code Online (Sandbox Code Playgroud)

sql sql-server sorting union visual-studio

1
推荐指数
1
解决办法
36
查看次数

优化bash命令以计算HDFS txt文件中的所有行

摘要:

我需要计算HDFS实例中所有.txt文件中的所有唯一行。

.txt文件的总大小约为450GB。

我使用以下bash命令:

hdfs dfs -cat /<top-level-dir>/<sub-dir>/*/*/.txt | cut -d , -f 1 | sort --parallel=<some-number> | uniq | wc -l

问题在于该命令将占用所有可用内存,并且HDFS实例以代码137(内存不足)退出。

题:

我有什么办法可以限制整个命令的ram使用率,比如说hdfs中的可用空间的一半,或者以某种方式在命令仍在运行时清理内存?

更新:

我需要删除| 排序 因为它是一个合并排序实现,所以空间复杂度为O(n)。

我只能使用| uniq | 没有| 排序 。

sorting bash out-of-memory hdfs

1
推荐指数
1
解决办法
100
查看次数

如何制作包含数组的向量?

我试图学习C ++中的存储桶排序算法,发现以下代码:

// C++ program to sort an array using bucket sort 
#include <iostream> 
#include <algorithm> 
#include <vector> 
using namespace std;

// Function to sort arr[] of size n using bucket sort 
void bucketSort(float arr[], int n)
{
    // 1) Create n empty buckets 
    vector<float> b[n];

    // 2) Put array elements in different buckets 
    for (int i = 0; i < n; i++)
    {
        int bi = n * arr[i]; // Index in bucket 
        b[bi].push_back(arr[i]);
    }

    // 3) Sort individual …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting stdvector c++11

1
推荐指数
1
解决办法
102
查看次数

Clojure:进入{}不会保留排序顺序

我有一个嵌套的地图,其结构如下(Clojurescript):

{"6841"
 {"primaryTitle" "First name",
  "secondaryTitle" "last name"},
 "7944"
 {"primaryTitle" "Test 2 first name",
  "secondaryTitle" "Test 2 last name"}}
Run Code Online (Sandbox Code Playgroud)

然后,我使用嵌套地图内的键对地图进行排序,如下所示:

(defn compare-title [x y]
  (compare [(get (second x) "primaryTitle") (get (second x) "secondaryTitle")]
           [(get (second y) "primaryTitle") (get (second y) "secondaryTitle")]))

(sort compare-title @loaded-assets)
Run Code Online (Sandbox Code Playgroud)

到目前为止,排序工作正常,但由于排序功能返回的数据结构如下:

    ["6841"
     {"primaryTitle" "First name",
      "secondaryTitle" "last name"}],
    ["7944"
     {"primaryTitle" "Test 2 first name",
      "secondaryTitle" "Test 2 last name"}]}
Run Code Online (Sandbox Code Playgroud)

我必须使用into {}将地图转换回初始结构:

(into {} (sort compare-title my-map))
Run Code Online (Sandbox Code Playgroud)

但这完全颠倒了按排序进行的排序。我尝试替换 into {}为:

  1. flatten (将其转换为列表)
  2. apply hash-map …

sorting clojure clojurescript

1
推荐指数
1
解决办法
140
查看次数

如何在C#中按字母顺序打印字符串中出现的字符?

谁能帮助我找到最有效的方式来按字母顺序在给定的字符串中打印字符出现以及该字符?

我可以计算字符串中字符的出现,但不能按字母顺序对其进行排序。

string OutputString = string.Empty;
int count = 1;
char[] charArr = inputString.ToCharArray();
for (int i = 0; i < charArr.Length; i++) {
    for (int j = i + 1; j < charArr.Length; j++) {
        if (charArr[i] == charArr[j])
            count++;
    }
    if (!OutputString.Contains(charArr[i]))
        OutputString += charArr[i].ToString() + count.ToString();

    count = 1;
}
OutputString = string.Concat(OutputString.OrderBy(c => c));
Run Code Online (Sandbox Code Playgroud)

假设输入字符串在 xgdgyd

输出应为:

d2g2x1y1.
Run Code Online (Sandbox Code Playgroud)

c# sorting string

1
推荐指数
1
解决办法
93
查看次数

Weird qsort behavior, what's wrong?

Why doesn't the code below sort the month names? Base address is correct, number of elements is correct, size of each element is correct, cmp function is correct. gcc -W -Wall -ansi -pedantic doesn't complain a peep.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int by_name(const void *a, const void *b) {
    fprintf(stderr, "%p %p\n", a, b);
    return strcmp((const char *)a, (const char *)b);
}
int main(void) {
    char *months[] = {
        "January",
        "Februar",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September", …
Run Code Online (Sandbox Code Playgroud)

c sorting c-strings qsort string-literals

1
推荐指数
1
解决办法
68
查看次数

为什么我的程序无法对整数字符串进行排序?

我正在尝试解决一个非常简单的编码问题:

考虑一个数字字符串数组,其中每个字符串都是一个正数,范围从1到10 ^ 6位数。对数组元素按其整数值的降序或升序排序,然后在新行上打印排序后的数组的每个元素。

第一行包含一个整数n,该整数n表示字符串的数目。n行中的每一行都包含一个INTEGER STRING。

我的代码是:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i; string s;
    cin >> i;
    int j = i;
    string arr[i];
    int cntr = 0;
    while (i--){
        cin >> s;
        arr[cntr] = s;
        cntr++;
    }
    sort(arr, arr+j);

    for (auto c: arr)
        cout << c << endl;

}
Run Code Online (Sandbox Code Playgroud)

输入是

6
31415926535897932384626433832795
1
3
10
3
5
Run Code Online (Sandbox Code Playgroud)

我的输出结果是:

1
10
3
3
31415926535897932384626433832795
5
Run Code Online (Sandbox Code Playgroud)

如果我创建一个数组并手动向其添加整数字符串,则上述代码可以正常工作。那么,为什么从网站输入信息却会产生错误的结果呢?

PS:这是问题的链接:https : //www.hackerrank.com/challenges/big-sorting/problem

c++ sorting

1
推荐指数
1
解决办法
125
查看次数