Relative Content

Tag Archive for c++sorting

why did std::sort use this simple strict weak ordering cmpare still crash?

#include<vector> #include<algorithm> #include<iostream> using namespace std; void test() { vector<vector<int>> info = {{0,0,999}, {0,1,1000}, {0,2,1001}, {0,3,1002}, {0,4,1003}, {0,5,1004}, {0,6,1005}, {0,7,1006}, {1,0,1000}, {1,1,999}, {1,2,1000}, {1,3,1001}, {1,4,1002}, {1,5,1003}, {1,6,1004}, {1,7,1005}, {2,0,1001}, {2,1,1000}, {2,2,999}, {2,3,1000}, {2,4,1001}, {2,5,1002}, {2,6,1003}, {2,7,1004}, {3,0,1002}, {3,1,1001}, {3,2,1000}, {3,3,999}, {3,4,1000}, {3,5,1001}, {3,6,1002}, {3,7,1003}, {4,0,1003}, {4,1,1002}, {4,2,1001}, {4,3,1000}, {4,4,999}, {4,5,1000}, {4,6,1001}, {4,7,1002}, {5,0,1004}, {5,1,1003}, {5,2,1002}, […]

why did std::sort use this simple strict weak ordering cmpare still crash?

#include<vector> #include<algorithm> #include<iostream> using namespace std; void test() { vector<vector<int>> info = {{0,0,999}, {0,1,1000}, {0,2,1001}, {0,3,1002}, {0,4,1003}, {0,5,1004}, {0,6,1005}, {0,7,1006}, {1,0,1000}, {1,1,999}, {1,2,1000}, {1,3,1001}, {1,4,1002}, {1,5,1003}, {1,6,1004}, {1,7,1005}, {2,0,1001}, {2,1,1000}, {2,2,999}, {2,3,1000}, {2,4,1001}, {2,5,1002}, {2,6,1003}, {2,7,1004}, {3,0,1002}, {3,1,1001}, {3,2,1000}, {3,3,999}, {3,4,1000}, {3,5,1001}, {3,6,1002}, {3,7,1003}, {4,0,1003}, {4,1,1002}, {4,2,1001}, {4,3,1000}, {4,4,999}, {4,5,1000}, {4,6,1001}, {4,7,1002}, {5,0,1004}, {5,1,1003}, {5,2,1002}, […]

The sort() function in C++

I’ve just done a problem on Leetcode. But I were confused about the sort() function of C++. This’s more detail about it

Find top k nodes based on values in an array – C

I’m looking for the most efficient implementation choice to select the top k nodes with the highest values from an array of size N, which could potentially be very large due to containing PageRank values for each node. Each node is represented by its index in the array, and I need to return the top k highest values along with their corresponding nodes. Using qsort directly on the array to sort it might not be efficient, as it can have a worst-case complexity of O(n^2), although it should be O(NlogN), but I could have a milion nodes. And I always need to know the node associated with each value, so I would need to create a dedicated structure for the node-value pair. Another option I’ve considered is using a min heap, which would be more efficient compared to the O(n^2) complexity of qsort. Or could it make sense to implement sorting using threads, where each thread sorts a portion of the array, followed by a merge to combine the sorted parts?

custom comparitor for integers in string vector

I want to sort a vector of strings representing very large (10**6 digits) in ascending order in C++.
I am trying to use a custom comparitor, but the sort is failing when comparing numbers of the same number of digits, eg. failing to re-arrange 82 and 44.