我有一些用于时间排序算法的代码(用于学校),但每当数组大小大于 20k 时,它就会不断崩溃。
这是我的主要文件:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sorting.h"
#define ARG_COUNT 1
int main(int argc, char *argv[]) {
if (argc != ARG_COUNT + 1) {
printf("Too few or too many arguments passed.\n");
exit(1);
}
if (atoi(argv[1]) < 10000) {
printf("Array lenght should be at least 10 000.");
exit(2);
}
int arr_lenght = atoi(argv[1]);
srand(time(0));
int *arr1 = (int *)calloc(arr_lenght, arr_lenght * sizeof(int));
for (int i = 0; i < arr_lenght; i++) {
arr1[i] = rand() % 20; …Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList,其中的数字从 10 MM 到 20 MM
List<Long> l = new ArrayList<>();
for (long i = 10_000_000; i < 20_000_000; i++) {
l.add(i);
}
Run Code Online (Sandbox Code Playgroud)
为什么sum()函数在排序数组上比在未排序数组上更快?
public class Main {
public static void main(String[] args) {
List<Long> l = new ArrayList<>();
for (long i = 10_000_000; i < 20_000_000; i++) {
l.add(i);
}
System.out.println(sum(l));
Collections.shuffle(l);
System.out.println(sum(l));
Collections.sort(l);
System.out.println(sum(l));
}
static long sum(List<Long> l) {
long started = System.nanoTime();
long result = 0;
for (long v : l) { …Run Code Online (Sandbox Code Playgroud) 我有一个元素为 的数组[0, 1, 0, 3, 12]。我想将所有零移到它的末尾,同时保持非零元素的相对顺序。移动后的数组应该如下所示[1, 3, 12, 0, 0]。为此,我编写了以下代码:
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){
if (a == 0) {
return a > b;
} else {
return true;
}
}
int main(){
int arr[] = {0, 1, 0, 3, 12};
sort(arr, arr + 5, cmp);
for (int i: arr) {
cout << i;
}
}
Run Code Online (Sandbox Code Playgroud)
我编写这段代码时认为,只要false在非零元素之前放置零,i就可以交换元素,但此代码给出的输出是,即[12, 3, 1, 0, 0]它只是按降序对元素进行排序。谁能解释一下为什么我的比较器功能不起作用?我可以使用比较器功能来做到这一点吗?
我有一个名为的集合Dataframe,本质上是一个std::vector<char>. 它包含records特定的长度record_size,可以由1个或多个不同类型的字段组成。的定义Dataframe是不可协商的,因为它与代码库的其余部分紧密耦合。一般来说,该结构可以包含数百万数量级的大量记录和数百数量级的大量列。
根据我上面所说的,可能只有一个列或多列。单例列,就是我所说的简单情况,因为字符向量可以转换为实际类型,并且排序非常有效。
我遇到的问题是,对于多个列,我需要有一个指向记录的指针向量,应用一个跳转到内存中访问实际数据的比较器函数,然后交换指针,当排序过程结束时,应用到真实数据由指针标识的排列。这会比较慢,尤其是当Dataframe包含真实世界数据时。
下面的代码摘录可以更好地解释这种情况。有没有其他方法可以解决这个问题?
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <functional>
class Dataframe {
public:
enum class Base : char
{
SIGNED = 'S',
UNSIGNED = 'U',
CHAR = 'A',
// and other types like floats, date, timestamp, etc.
};
class Dtype
{
public:
Dtype(Base base_dtype, std::size_t size) : m_base_dtype(base_dtype), m_size(size) {}
auto base_dtype() const { return m_base_dtype; }
auto …Run Code Online (Sandbox Code Playgroud) 如何使用jq按 IPv4 地址值作为 32 位整数而不是按字母数字对对象数组进行排序?
为了视觉清晰度而使用一些额外的格式来说明我的意思:
$ echo '
{"name": "dns", "ip": "4.4.4.4"}
{"name": "host1", "ip": "10.9.9.9"}
{"name": "host2", "ip": "10.10.10.10"}
' |jq -sr '.|=sort_by(.ip) |.[] |[.name, .ip] |@tsv'
host2 10.10.10.10
host1 10.9.9.9
dns 4.4.4.4
Run Code Online (Sandbox Code Playgroud)
这|=sort_by(.ip)按字母数字顺序排序,但我希望数组按“IPnumerically”排序。如果我有这样的 TSV 格式,我可以使用外部工具(例如 的非 POSIX-V扩展sort)随后在外部执行此操作,但是有没有办法在 内部执行此操作jq?
$ echo '
{"name": "dns", "ip": "4.4.4.4"}
{"name": "host1", "ip": "10.9.9.9"}
{"name": "host2", "ip": "10.10.10.10"}
' |jq -sr '.|=sort_by(.ip) |.[] |[.name, .ip] |@tsv' |sort -t$'\t' -Vk2
dns 4.4.4.4
host1 …Run Code Online (Sandbox Code Playgroud) 我试图unordered_map<int, int>按其值对 an 进行反向排序,但我不明白为什么它不能正常工作。
这是我正在使用的代码:
static bool comp(const pair<int,int>& a, const pair<int,int>& b) { return a.second < b.second; }
unordered_map<int,int> sort_map(unordered_map<int,int>& m) {
vector<pair<int,int>> v;
unordered_map<int,int> sorted_m;
for (const auto& it : m) {
v.push_back(it);
}
sort(v.begin(),v.end(),comp);
std::cout << "sorted vector:" << std::endl;
for (const auto& it : v) {
std::cout << it.first <<":" << it.second <<std::endl;
sorted_m.emplace(it);
}
return sorted_m;
}
Run Code Online (Sandbox Code Playgroud)
这是地图的输入示例:[5,-3,9,1,7,7,9,10,2,2,10,10,3,-1,3,7,-9,-1,3,3]
这是输出:
sorted vector:
-9:1
1:1
-3:1
5:1
-1:2
2:2
9:2
10:3
7:3
3:4
sorted …Run Code Online (Sandbox Code Playgroud) 我的 C# 项目中必须具有相同索引的元素之间存在关系的数组:
简化示例如下:
Array_1 [0] =902;
Array_1 [1] =109;
Array_1 [2] =205;
Array_1 [3] =187;
Array_1 [4] =440;
Array_1 [5] =508;
Array_2 [0] ="Ford";
Array_2 [1] ="Opel";
Array_2 [2] ="Kia";
Array_2 [3] ="Renault";
Array_2 [4] ="BMW";
Array_2 [5] ="Toyota";
Run Code Online (Sandbox Code Playgroud)
我必须根据Array_1的值排名(从低到高)对Array-2进行排序。结果将写入Array_2_sorted。这意味着: Array_1 的最低值是 Array_1[1] (109) --> 所以 Array_2[1] (Opel) 应该是 Array_2_sorted[0]
下一个更高的值是 Array_1[3] (187) --> 所以 Array_2[3] (Renault) 应该是 Array_2_sorted[1]
下一个更高的值是 Array_1[2] (205) --> 所以 Array_2[2] (Kia) 应该是 Array_2_sorted[2]
下一个更高的值是 Array_1[4] (440) --> 所以 Array_2[4] (BMW) 应该是 Array_2_sorted[3]
...
如何在 …
我有两个多维数组,需要根据不同的键以与第二个数组相同的顺序对第一个数组进行排序(但它们的值相同)。在下面的示例中,我需要按照与 的值相同$allOptions的顺序进行排序。$regOptionsclID == optID
但是,并非所有$allOptions子数组 (clID) 都存在于$regOptions子数组 (optID) 中......因此,其中任何不匹配的元素都$allOptions将被扔到数组的底部/末尾。
我怎样才能做到这一点?
$allOptions = array(
array("clID"=> 171, ...other values),
array("clID"=> 191, ...other values),
array("clID"=> 131, ...other values),
array("clID"=> 101, ...other values),
array("clID"=> 201, ...other values),
array("clID"=> 181, ...other values),
...
array("clID"=> 99, ...other values), // not in regOptions
array("clID"=> 129, ...other values) // not in regOptions
array("clID"=> 139, ...other values)
) ;
$regOptions = array(
array("order"=>1,"optID"=> 131, ...other values),
array("order"=>2,"optID"=> 191, ...other …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一种更有效的方法来对 python 中的列表和字典进行排序,并遇到了Efficient data Structurekeepingingobjectsonmultiplekeys。建议的解决方案是使用heapq模块。
然而,在我的测试中,堆似乎比原生 Python 排序算法慢两倍。下面是我用来做简单测试的代码。结果例如:
堆: 0.005993366241455078
标准: 0.0020036697387695312
有没有办法真正使用堆并提高性能,正如上面链接的帖子声称的那样?该代码会是什么样子?
这是测试它的代码:
import random
import time
from heapq import *
standardlist = []
heaplist = []
for i in range(10000):
num = random.randint(0,10000)
standardlist.append(num)
heappush(heaplist, num)
# Standard sorting method:
start_time = time.time()
sorted_list = sorted(standardlist)
finish_time_1 = time.time() - start_time
# Heap sorting method:
start_time = time.time()
heap_sorted_list = [heappop(heaplist) for i in range(len(heaplist))]
finish_time_2 = time.time() - start_time
print("Standard Finish Time:", finish_time_1) …Run Code Online (Sandbox Code Playgroud) 我遇到了对数组中的字符串进行排序的问题。我能够对整数进行排序,但是我真的不知道如何将字符串从最短到最长排序。
我尝试使用Strings.length将Strings转换为整数,但是我不知道如何将其转换回其原始String。
字符串处理程序;
System.out.println("\nNow we will sort String arrays.");
System.out.println("\nHow many words would you like to be sorted.");
Input = in.nextInt();
int Inputa = Input;
String[] Strings = new String [Input];
for (int a = 1; a <= Input; Input --) //will run however many times the user inputed it will
{
counter ++; //counter counts up
System.out.println("Number " + counter + ": "); //display
userinputString = in.next();
Strings[countera] = userinputString; //adds input to array
countera ++;
}
System.out.println("\nThe words …Run Code Online (Sandbox Code Playgroud) sorting ×10
arrays ×4
c++ ×3
algorithm ×2
java ×2
performance ×2
benchmarking ×1
c ×1
c# ×1
clock ×1
comparator ×1
cpu ×1
custom-sort ×1
dictionary ×1
heap ×1
ipv4 ×1
jq ×1
json ×1
list ×1
php ×1
python ×1
ranking ×1
string ×1
time.h ×1
timing ×1