Skip to main content
Filter by
Sorted by
Tagged with
0 votes
3 answers
191 views

Is there a short, stable sorting algoritm for 4 elements with 5 comparisons and 5 swaps? I'm looking for an algorithm like https://stackoverflow.com/a/50303723/97248 in terms of length and complexity, ...
pts's user avatar
  • 88.7k
3 votes
1 answer
588 views

I have a vector of structs, each struct has a numerical ID that I am using to sort the vector items by. I want the IDs to be sorted, but to also appear in the order they did in the original vector ...
Coffee'd Up Hacker's user avatar
1 vote
0 answers
51 views

Does std::priorirty_queue guarantee a specific ordering (regardless of the standard library implementation) when extracting elements in a largest-first order? Suppose that the comparison function that ...
Szabolcs's user avatar
  • 25.8k
-1 votes
1 answer
83 views

In c++ there is a function stable_sort() function (It preserves the order ) .Is there any function in python like this ?
Vivek Maddeshiya's user avatar
0 votes
1 answer
69 views

static void stableSelectionSort(int[] a, int n) { for (int i = 0; i < n - 1; i++) { int min = i; for (int j = i + 1; j < n; j++) if (a[min] > a[j]) ...
Dhanush Kannan's user avatar
0 votes
2 answers
175 views

I wrote simple algorithm for sorting rows in Eigen matrix. This should do the same as Matlab's sortrows function: template <typename D> void _sort( const D &M, Eigen::VectorX<...
Kerim's user avatar
  • 221
4 votes
2 answers
3k views

I have a list like this: list_results=[('Horror', 2), ('Romance', 2), ('Comedy', 2), ('History', 2), ('Adventure', 1), ('Action', 3)] I wish to sort the number in descending order and if numbers were ...
mher's user avatar
  • 41
1 vote
0 answers
47 views

I am currently learning algorithms. While learning selection sort I came to know that selection sort is unstable as it swaps two elements. It can be made stable by inserting elements instead of ...
Jonayed Mohiuddin's user avatar
0 votes
1 answer
517 views

I'm trying to sort element in vector by module with the condition that the initial order of equal (by module) elements does not change. Logic tells me that the comparator should take less than or ...
Evgeny's user avatar
  • 637
2 votes
2 answers
1k views

On the Wikipedia page about Counting Sort, it states: It is possible to modify the algorithm so that it places the items into sorted order within the same array that was given to it as the input, ...
Ricola's user avatar
  • 2,981
1 vote
3 answers
2k views

I am trying to write a custom comparator in C++ to sort a vector. For the sake of simplicity, I will say my sorting criteria is that all even values should come before all odd values and I am trying ...
Jay Modi's user avatar
0 votes
2 answers
774 views

I am trying to do stable sort in C++ and my array is like this {{first = "art zero", second = "let3 "}, {first = "own kit dig", second = "let2 "}, {first = &...
Error's user avatar
  • 3
2 votes
2 answers
179 views

Here it looks like the space after the 3 in both rows breaks the numerical sorting and lets the alphabetic sorting kick in, so that 11<2: $ echo -e '3 2\n3 11' | sort -n 3 11 3 2 In man sort, I ...
Enlico's user avatar
  • 30.3k
3 votes
4 answers
2k views

NumPy's np.argsort is able to do stable sorting through passing kind = 'stable' argument. Also np.argsort doesn't support reverse (descending) order. If non-stable behavior is needed then descending ...
Arty's user avatar
  • 17k
0 votes
1 answer
258 views

https://leetcode.com/problems/move-zeroes/ was solving this question. But this doesnot compile. void moveZeroes(vector<int>& nums) { stable_sort(nums.begin(), nums.end(),[](int&...
Kshitij Dubey's user avatar
1 vote
0 answers
770 views

Is there is any sorting algorithm except(bubble/insertion) which is both stable and in-place? I know heap sort and quick sort are in-place but they are not stable, and merge sort is stable but not in-...
Aman Gupta's user avatar
1 vote
1 answer
467 views

https://leetcode.com/problems/largest-number/ When I was solving the above problem, I came across the case where std::sort() was giving me a runtime error, but replacing it with std::stable_sort() ...
VK Singh's user avatar
2 votes
2 answers
2k views

#include <bits/stdc++.h> using namespace std; int main() { vector<pair<int,int>>v; v.push_back(make_pair(1,3)); v.push_back(make_pair(1,1)); v.push_back(...
user avatar
1 vote
2 answers
651 views

Input = [("M", 19), ("H", 19), ("A", 25)] Output =[("A", 25), ("M" ,19), ("H", 19)] It should sort alphabetically but when the second value is equal then it should remain in place without changing ...
Harsh Vardhan's user avatar
4 votes
1 answer
2k views

I am using C++ stable_sort to sort a vector of my class objects in ascending order using a comparator function, but the sort is not stable. A work around that worked was to reverse iterate and ...
Aman's user avatar
  • 61
0 votes
1 answer
1k views

I am fairly new to Java programming although I have been programming in other languages for a few years now. I have recently begun tackling different programs on Kattis to assign to my students. I ...
Randy's user avatar
  • 1
2 votes
1 answer
684 views

Imagine i have a data set that contains: Date Id -------------- ---- 11/1/2017 null 11/4/2017 3 11/5/2017 null 11/12/2017 10 null 1 null 2 ...
Ian Boyd's user avatar
  • 259k
0 votes
3 answers
64 views

I am in trouble with this sort. I have an array containing values like this: array = [ {SortTime : 123456, Operation : Assigning}, {SortTime : 4567 , Operation: Assigning}, {SortTime : 123456 , ...
Vins's user avatar
  • 9
1 vote
0 answers
102 views

I have lists of tasks that I want to sort pseudo-topologically. Say, I have something like this: enum Task { case a(Int, Int) case b(String) case c(Bool) } I require all a tasks to be ...
Raphael's user avatar
  • 10.8k
2 votes
1 answer
575 views

I am trying to create a stable sort element to the default JavaScript .sort() function. I have it working in all browsers except for IE11 and below. Here is the code: Array.prototype.stableSort =...
TheLettuceMaster's user avatar
4 votes
2 answers
1k views

So, actually what I need is to keep the index of old array after sorted. So for example if I input [2,4,1,5,7,9,6] then the output is [2,0,1,3,6,4,5]. I already have use qsort and it works very well ...
fahadh4ilyas's user avatar
20 votes
2 answers
2k views

I wish to make use of std::stable_sort. The complexity of the algorithm is stated as O(N·log^2(N)), where N = std::distance(first, last) applications of cmp. If additional memory is available, then ...
izaak_pyzaak's user avatar
0 votes
3 answers
59 views

Below is an output of my array $array1 = Array ( [d] => 5 [e] => 1 [a] => 3 [b] => 3 [c] => 3 [f] => 3 ) I want to sort it like... Array ( [d] => 5 ...
Sri Ni's user avatar
  • 19
0 votes
0 answers
26 views

I am asking first question because: As we know, we pass first and last index in quicksort which thereafter chooses a pivot, and then does partitioning. What if in mergesort also in Mergesort() method, ...
Vivek Vardhan's user avatar
1 vote
1 answer
1k views

Stable sort talks about equal keys NOT getting past each other, after sorting Consider duplicate key 4 at array index 8 & 9, in the below sequence, a = [5 20 19 18 17 8 4 5 4 4] where pivot = 0, ...
overexchange's user avatar
  • 17.3k
3 votes
1 answer
185 views

I want to know the scenario where Stable sorting will make a huge impact. Previous versions of JAVA had Merge sort for collections.sor API which is a stable sort while for Array.sort, quicksort was ...
Crypto's user avatar
  • 148
0 votes
1 answer
540 views

I'm running this code to sort big array of IPs using thrust stable_sort and user defined operator to compare the IPs. this code is working for arrays less than 50000 IPs, but I got a memory error for ...
Ziad Bkh's user avatar
0 votes
5 answers
3k views

I have numbers that are getting converted to string. for example I have an amount 20000 and I have to show it as 200.00 so I am performing string Amount = $"{Convert.ToDouble(x.Amount) / 100:0.00}" ...
Ramki's user avatar
  • 31
4 votes
3 answers
951 views

From various sources cited I know that in-built C function, stable_sort is stable but qsort is unstable. If that is the case why do we use qsort at all? Isn't it redundant? Why not use stable_sort ...
markroxor's user avatar
  • 6,602
2 votes
2 answers
4k views

I am allowed to use an extra array to perform a stable quicksort unlike the general quicksort algorithm. I know how to select the pivot at random and partition accordingly but I'm not able to figure ...
humblenoob's user avatar
0 votes
1 answer
550 views

Im trying to use stable_sort in order to sort a vector of pointers to a certain class. I've a code like this : #include <iostream> #include <vector> #include <algorithm> using ...
ironman89's user avatar
2 votes
4 answers
2k views

Straight from the qsort manual it says: If two members compare as equal, their order in the sorted array is undefined. However, I want qsort to leave the ordering unchanged on equality. That is to ...
Andy T's user avatar
  • 357
0 votes
2 answers
150 views

I have an array like: array (size=5) 0 => "" 1 => "" 2 => "foo" 3 => "" 4 => "bar" I want to move all of the ...
ramsey_lewis's user avatar
0 votes
1 answer
93 views

I am using the below code to sort rectangles. In the stable_sort function, how can I specify the iterator values other than boundRect.begin and boundRect.end. I want to sort the elements between ...
aries's user avatar
  • 929
5 votes
1 answer
540 views

MATLAB's built-in function accumarray accepts a function fun as a fourth argument. A = accumarray(subs,val,sz,fun); This applies fun to each subset of elements in val that have identical subscripts ...
knedlsepp's user avatar
  • 6,104
7 votes
1 answer
513 views

This is a followup to an answer to a question about sorting on a particular argument of a term, without creating a new list for a keysort (if I understood the original question correctly). Say we ...
user avatar
4 votes
3 answers
316 views

Consider n cards that are marked either red or blue i=1; j=n; while(i<n) { if(a[i]==RED) i++; ...
hitish's user avatar
  • 355
0 votes
1 answer
119 views

I have a problem with sorting. I sort the objects containing the dynamic table. It seems that the stable_sort (or the vector) doesn't use a public copy constructor. I looks like they use a non-...
Mat's user avatar
  • 3
7 votes
1 answer
423 views

How do I stably sort an array? The value I want to sort by can have a lot of duplicates, and I'm not sure which sort algorithm ruby uses. I'm thinking insertion sort would have worked best for me. ...
Karan Verma's user avatar
  • 1,759
2 votes
3 answers
106 views

Let D be a data.frame, with D$x containing real numbers and D$y containing booleans, among other fields. The problem is to sort the rows of D so that D$x is non-decreasing, while breaking ties in a ...
Museful's user avatar
  • 7,019
43 votes
6 answers
58k views

I'm trying to understand why heapsort isn't stable. I've googled this, but haven't found a good, intuitive explanation. I understand the importance of stable sorting - it allows us to sort based on ...
JMS's user avatar
  • 1,121
5 votes
1 answer
2k views

Given an array of n elements, is there a sorting algorithm that sorts in at most O(n log n) time (and optionally, O(n) time in the best case) is stable takes O(1) auxilliary space All sorting ...
fuz's user avatar
  • 94.7k
1 vote
4 answers
1k views

And I have a comparison function "compr" already in the code to compare two values. I want something like this: Sorting.stableSort(arr[i,j] , compr) where arr[i,j] is a range of element in array.
user avatar
0 votes
3 answers
237 views

Is it ever good to call stable_sort instead of sort on scalar types (i.e. int, long, etc.) with the default comparator? If so, when should you do this? If not, then why don't standard libraries just ...
user541686's user avatar
  • 213k
4 votes
6 answers
3k views

what is difference between the following two code snippets. vector<int> a; // initialization code sort( a.rbegin(), a.rend() ); and vector<int> a; // same initialization as above sort(a....
seeker's user avatar
  • 704