stable-sort

Does sort -n handle ties predictably when the --stable option is NOT provided? If it does, how?

烈酒焚心 提交于 2021-01-28 04:50:48
问题 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 read -s, --stable stabilize sort by disabling last-resort comparison which implies that without -s a last-resort comparison is done (between ties, because -s does not affect non-ties). So the question is: how is this last-resort comparison accomplished? A reference to the source code would be

stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++

假如想象 提交于 2020-05-09 05:57:46
问题 #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(make_pair(2,19)); v.push_back(make_pair(2,4)); int n = 4; stable_sort(v.begin(),v.end()); for (int i = 0; i < n; i++) cout << "[" << v[i].first << ", " << v[i].second << "] "; return 0; } Output : [1, 1] [1, 3] [2, 4] [2, 19] Expected Output : [1, 3] [1, 1] [2, 19] [2, 4] Why does the vector of pairs not maintain the relative ordering even

How is counting sort a stable sort?

一曲冷凌霜 提交于 2020-01-12 03:16:07
问题 Suppose my input is ( a , b and c to distinguish between equal keys) 1 6a 8 3 6b 0 6c 4 My counting sort will save as (discarding the a , b and c info!!) 0(1) 1(1) 3(1) 4(1) 6(3) 8(1) which will give me the result 0 1 3 4 6 6 6 8 So, how is this stable sort? I am not sure how it is "maintaining the relative order of records with equal keys." Please explain. 回答1: Simple, really: instead of a simple counter for each 'bucket', it's a linked list. That is, instead of 0(1) 1(1) 3(1) 4(1) 6(3) 8(1)

Sort numbers with decimals that are stored in string

痴心易碎 提交于 2020-01-06 08:15:37
问题 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}" and then I store them to list of amounts with values 200.00, 30.00, 588888.00, 56.36, I tried sorting it by orderby(x=>x.Anount) but this sorts on basis of string with first number as 200.00, 30.00, 56.36, 58888.00 I want the output to be sorted as 30.00, 56.36, 200.00, 588888.00 回答1: Pass a custom

How can I implement a stable quicksort algorithm using O(n) additional space?

巧了我就是萌 提交于 2020-01-06 04:27:06
问题 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 out how to make use of the additional array to make it stable. 回答1: The most trivial way that comes to mind is to store the initial indices in the array (1, 2, 3, etc) and swap them around as you swap the data. Then in the comparison, if the two elements are equal, compare their indices too, thus

Possible behaviors of `predsort/3`

你离开我真会死。 提交于 2019-12-21 10:40:34
问题 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 wanted predsort/3 to behave exactly as sort/2 : if I understand correctly, this would mean calling it as: ?- predsort(compare, List, Sorted). Now say that we wanted to use predsort/3 to sort as implemented by msort/2 (see also this question). One way to do it would be to define a comparison predicate

Stable separation for two classes of elements in an array

岁酱吖の 提交于 2019-12-17 13:03:40
问题 Consider the following problem. We are given an array of elements belonging to one two classes: either red or blue. We have to rearrange the elements of the array so that all blue elements come first (and all red elements follow). The rearrangement must be done is stable fashion, meaning that the relative order of blue elements must be preserved (same for red ones). Is there a clever algorithm that would perform the above rearrangement in-place? A non-in place solution is, of course,

How to stable_sort without copying?

孤街浪徒 提交于 2019-12-12 11:29:49
问题 Why does stable_sort need a copy constructor? ( swap should suffice, right?) Or rather, how do I stable_sort a range without copying any elements? #include <algorithm> class Person { Person(Person const &); // Disable copying public: Person() : age(0) { } int age; void swap(Person &other) { using std::swap; swap(this->age, other.age); } friend void swap(Person &a, Person &b) { a.swap(b); } bool operator <(Person const &other) const { return this->age < other.age; } }; int main() { static size

How Can I Replace StringList.Sort with a Stable Sort in Delphi?

时光怂恿深爱的人放手 提交于 2019-12-10 18:35:50
问题 I'm doing a simple StringList.sort, but Delphi uses a QuickSort that is not a stable sort, meaning it may change the relative order of records with equal keys. I need to use a stable sort. What would be the easiest way for me to implement this? Mike W's answer might be the simplest way to do it without too much code change necessary. Thanks, Mike. 回答1: If you're not already using the Objects property of the string list a quick and dirty solution would be to store the original position in the