stable-sort

“stable_sort()ing” a STL <list> in C++

走远了吗. 提交于 2019-12-10 15:57:04
问题 I think the question title is clear enough: is is possible to stable_sort() a std::list in C++? Or do I have to convert it to a std::vector? I'm asking because I tried a simple example and it seems to require RandomAccessIterators, which a linked list doesn't have. So, how do I stable sort a std::list() ? EDIT: sample code that gives me an error: #include <list> #include <algorithm> // ... list<int> the_list; stable_sort(the_list.begin(), the_list.end()); g++ gives me around 30 lines of

How is counting sort a stable sort?

痞子三分冷 提交于 2019-12-03 02:55:22
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. 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) You get 0(.) 1(.) 3(.) 4(.) 6(a,b,c) 8(.) (here I use . to denote some item in the bucket). Then just dump

Is there any builtin stable sort routine and swap function in .NET?

对着背影说爱祢 提交于 2019-12-01 22:09:41
问题 Is there any in-built stable sort routine in .NET? I know that C++ has an in-built sort routine under "algorithms" std::sort() . Likewise, do we have something to use along with C#? Also, is there any in-built swap function in .NET? 回答1: Using "C# stable sort" in Google revealed this SO post as top result: Is the sorting algorithm used by .NET's `Array.Sort()` method a stable algorithm? So the answer is: Enumerable.OrderBy is a stable sort function, not built into C#, but part of the .NET

Is there any builtin stable sort routine and swap function in .NET?

孤街醉人 提交于 2019-12-01 20:36:57
Is there any in-built stable sort routine in .NET? I know that C++ has an in-built sort routine under "algorithms" std::sort() . Likewise, do we have something to use along with C#? Also, is there any in-built swap function in .NET? Doc Brown Using "C# stable sort" in Google revealed this SO post as top result: Is the sorting algorithm used by .NET's `Array.Sort()` method a stable algorithm? So the answer is: Enumerable.OrderBy is a stable sort function, not built into C#, but part of the .NET framework libraries. Concerning "Swap": I don't know of any prebuilt generic swap function in the

Easy way to add stable sorting to TList and TStringList

好久不见. 提交于 2019-11-30 23:22:19
问题 I use TList/TObjectList and TStringList (with associated objects) for a multitude of tasks, either as-is, or as basis for more complex structures. While the sort functionality is usually good enough, I sometimes need to do a stable sort, and both lists use quicksort. What's the easiest way to implement stable sorting for TList and/or TStringList? Do I have to write my own sorting routine, or can it be done by using some clever trick with TStringListSortCompare/TListSortCompare? 回答1: You'll

Why isn't heapsort stable?

核能气质少年 提交于 2019-11-30 06:15:36
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 more than one key, which can be very beneficial (i.e., do multiple sortings, each based on a different key. Since every sort will preserve the relative order of elements, previous sortings can add up to give a final list of elements sorted by multiple criteria). However, why wouldn't heapsort preserve this as well? Thanks for your help! The final sequence of the results from heapsort comes from

How do I do stable sort?

落花浮王杯 提交于 2019-11-30 05:00:30
问题 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. Example: a = [[:a, 0], [:b, 1], [:c, 0], [:d, 0]] a.sort_by { |x, y| y } # => [[:a, 0], [:d, 0], [:c, 0], [:b, 1]] Looking for [[:a, 0], [:c, 0], [:d, 0], [:b, 1]] 回答1: Put the key that you originally wanted to sort by and the index into an array, and sort by that. a.sort_by.with_index { |(x, y), i|

django: __in query lookup doesn't maintain the order in querset

岁酱吖の 提交于 2019-11-29 23:02:57
I have ID's in a specific order >>> album_ids = [24, 15, 25, 19, 11, 26, 27, 28] >>> albums = Album.objects.filter( id__in=album_ids, published= True ) >>> [album.id for album in albums] [25, 24, 27, 28, 26, 11, 15, 19] I need albums in queryset in that order as id's in album_ids. Anyone please tell me how can i maintain the order? or obtain the albums as in album_ids? Assuming the list of IDs isn't too large, you could convert the QS to a list and sort it in Python: album_list = list(albums) album_list.sort(key=lambda album: album_ids.index(album.id)) Since Djnago 1.8 you can do in this way

Why isn't heapsort stable?

社会主义新天地 提交于 2019-11-29 05:06:59
问题 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 more than one key, which can be very beneficial (i.e., do multiple sortings, each based on a different key. Since every sort will preserve the relative order of elements, previous sortings can add up to give a final list of elements sorted by multiple criteria). However, why wouldn't heapsort

django: __in query lookup doesn't maintain the order in querset

筅森魡賤 提交于 2019-11-28 19:52:30
问题 I have ID's in a specific order >>> album_ids = [24, 15, 25, 19, 11, 26, 27, 28] >>> albums = Album.objects.filter( id__in=album_ids, published= True ) >>> [album.id for album in albums] [25, 24, 27, 28, 26, 11, 15, 19] I need albums in queryset in that order as id's in album_ids. Anyone please tell me how can i maintain the order? or obtain the albums as in album_ids? 回答1: Assuming the list of IDs isn't too large, you could convert the QS to a list and sort it in Python: album_list = list