75 questions
0
votes
3
answers
191
views
stable sorting algorithm for 4 elements, with 5 comparisons and 5 swaps
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, ...
3
votes
1
answer
588
views
How can I keep the order of duplicates in a vector after sorting it?
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 ...
1
vote
0
answers
51
views
Does priority_queue provide a predictable order? [duplicate]
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 ...
-1
votes
1
answer
83
views
Implementation of stable_sort in python
In c++ there is a function stable_sort() function (It preserves the order ) .Is there any function in python like this ?
0
votes
1
answer
69
views
What is the time complexity of Stable Selection Sort algorithm?
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])
...
0
votes
2
answers
175
views
`std::stable_sort` gives wrong results when `std::execution::par`
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<...
4
votes
2
answers
3k
views
How can i sort a list by the second item descending and first one ascending?
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 ...
1
vote
0
answers
47
views
Why make selection sort stable? [duplicate]
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 ...
0
votes
1
answer
517
views
C++. How to sort by module with saving the original order of elements
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 ...
2
votes
2
answers
1k
views
Counting sort implementation that modifies the input array
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, ...
1
vote
3
answers
2k
views
How to perform stable sort in C++ when using a custom comparator?
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 ...
0
votes
2
answers
774
views
What does comparator return really mean in C++
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 = &...
2
votes
2
answers
179
views
Does sort -n handle ties predictably when the --stable option is NOT provided? If it does, how?
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 ...
3
votes
4
answers
2k
views
NumPy - descending stable arg-sort of arrays of any dtype
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 ...
0
votes
1
answer
258
views
Stable sort custom comparator using pass by reference with lamdas gives compile error
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&...
1
vote
0
answers
770
views
Stable and in-place sorting algorithm?
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-...
1
vote
1
answer
467
views
std::stable_sort vs std::sort [duplicate]
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() ...
2
votes
2
answers
2k
views
stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++
#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(...
1
vote
2
answers
651
views
Sorting tuples in python and keeping the relative order
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 ...
4
votes
1
answer
2k
views
C++ stable_sort not stable?
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 ...
0
votes
1
answer
1k
views
SortofSorting on Open.Kattis.com fails on the second test case despite running on every test case I have generated.
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 ...
2
votes
1
answer
684
views
How to perform stable sort over multiple columns?
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
...
0
votes
3
answers
64
views
How to correctly sort these type of array?
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 , ...
1
vote
0
answers
102
views
Is there a stable sort that works with partial orders in the standard library? [duplicate]
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 ...
2
votes
1
answer
575
views
Creating stable sort for JavaScript issues in IE11
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 =...
4
votes
2
answers
1k
views
How to Sort Array But Keep The Position of Duplicate Element in C?
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 ...
20
votes
2
answers
2k
views
std::stable_sort: How to choose memory-optimised algorithm over time-optimised algorithm?
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 ...
0
votes
3
answers
59
views
arsort() is not preserving the original element order for duplicated values (below PHP8) [duplicate]
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
...
0
votes
0
answers
26
views
Why can't we have a mergesort which is in-place? Why can't we have a quicksort which is stable?
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, ...
1
vote
1
answer
1k
views
Quick sort - Not a stable sort
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, ...
3
votes
1
answer
185
views
A solid example (or some business use case) where Stable sort makes a significant difference
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 ...
0
votes
1
answer
540
views
memory location error: thrust::stable_sort when using big array and user-defined comparison operator
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 ...
0
votes
5
answers
3k
views
Sort numbers with decimals that are stored in string
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}" ...
4
votes
3
answers
951
views
What is the difference between inbuilt qsort function and stable sort function?
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 ...
2
votes
2
answers
4k
views
How can I implement a stable quicksort algorithm using O(n) additional space?
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 ...
0
votes
1
answer
550
views
stable_sort in C++
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 ...
2
votes
4
answers
2k
views
How to leave ordering unchanged on equality using qsort [duplicate]
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 ...
0
votes
2
answers
150
views
Move null items to the end of an array [duplicate]
I have an array like:
array (size=5)
0 => ""
1 => ""
2 => "foo"
3 => ""
4 => "bar"
I want to move all of the ...
0
votes
1
answer
93
views
Specifying iterator value in stable_sort function
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 ...
5
votes
1
answer
540
views
Stable accumarray in MATLAB
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 ...
7
votes
1
answer
513
views
Possible behaviors of `predsort/3`
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 ...
4
votes
3
answers
316
views
How to convert the following non-stable sorting algorithm to stable?
Consider n cards that are marked either red or blue
i=1;
j=n;
while(i<n)
{
if(a[i]==RED)
i++;
...
0
votes
1
answer
119
views
stable_sort on objects containing dynamic tables
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-...
7
votes
1
answer
423
views
How do I do stable sort?
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.
...
2
votes
3
answers
106
views
Sorting with tie-breaking that minimizes disconinuity of a boolean field
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 ...
43
votes
6
answers
58k
views
Why isn't heapsort stable?
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 ...
5
votes
1
answer
2k
views
Is it possible to stably sort an array in O(n log n) with constant auxilliary space?
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 ...
1
vote
4
answers
1k
views
Scala: How to sort an array within a specified range of indices?
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.
0
votes
3
answers
237
views
When do you call stable_sort() on scalars?
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 ...
4
votes
6
answers
3k
views
Difference between Iterator and reverse iterator
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....