  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Do equal elements preserve their order in Insertion Sort algorithm?  » java » Do equal elements preserve their order in Insertion Sort algorithm?

By : Samuel.T
Date : November 21 2020, 03:00 PM
Hope that helps You are absolutely right. This here is a snippet of the Insertion Sort Algorithm from the popular book "Introduction to Algorithms" by Thomas H. Cormen. code :
``````INSERTION-SORT(A)
1. for j=2 to A.length
2. key = A[j]
3. // Insert A[j] into the sorted sequence A[1..j-1].
4. i = j-1
5. while i > 0 and A[i] > key
6. A[i+1] = A[i]
7. i = i-1
8. A[i+1] = key
`````` ## Will std::multimap preserve the insert order if the key of 2 elements equal to each other?

By : Ben Swanson
Date : March 29 2020, 07:55 AM
wish of those help The elements in a std::map must have unique keys, so... no.
The std::multimap container allows multiple values mapped to one key. When iterating over a std::multimap the elements are ordered by key, but the order of elements having the same key is not specified. ## Maintain insertion order for equal elements in std::multiset

By : ghoswheel
Date : March 29 2020, 07:55 AM
seems to work fine Here's a wild idea: As @Jerry suggests, maintain a counter. Since the pair of object and counter is unique, we can now use a set (ordered lexicographically). Then we use lower_bound to find the insertion point and compute the next counter value:
code :
``````unsigned int const uimax = std::numeric_limits<unsigned int>::max();

typedef std::set<std::pair<T, unsigned int>> pair_set;

void counted_insert(pair_set & s, T const & t)
{
pair_set::iterator it = s.lower_bound(std::make_pair(t, uimax));

if (it == s.begin() || !(*--it == t))
{
s.insert(it, std::make_pair(t, 0));
}
else
{
s.insert(it, std::make_pair(t, it->first + 1));
}
}
`````` ## Preserve insertion order of elements in a data member for Neo4j

By : Vaishali Aghera
Date : March 29 2020, 07:55 AM
I hope this helps . There is a special implementation of Set, the class TreeSet keeps the elements in the set sorted, either by their natural ordering or by asking a Comparator how they should be ordered. TreeSets reorders the set whenever you add/remove elements.
There is also the LinkedHashSet implementation which keeps the items according to the insertion order. ## Preserve original order if numeric value is equal in coreutils sort?

Date : March 29 2020, 07:55 AM
seems to work fine Just add the -s (stable sort) flag, this disables last-resort comparison
code :
``````echo '7 a
3 c
3 b
2 first
2 second
2 third
2 fourth
2 fifth
9 d
2 sixth
' | sort  -k 1,1n -s

2 first
2 second
2 third
2 fourth
2 fifth
2 sixth
3 c
3 b
7 a
9 d
`````` ## Stable sort a C++ hash map - preserve the insertion order for equal elements

By : MariusVT
Date : March 29 2020, 07:55 AM
may help you . Here is a possible solution I came up with using an std::unordered_map and tracking the order of inserting using a std::vector.
Create a hash map with the string as key and count as value.
code :
``````    unordered_map<std::string, int> map;
std::vector<std::unordered_map<std::string,int>::iterator> order;

// Lets assume this is my string stream
std::vector<std::string> words = {"a","b","a" ... };

// Insert elements to map and the corresponding iterator to order
for (auto& word : words){
auto it = map.emplace(word,1);
if (!it.second){
it.first->second++;
}
else {
order.push_back(it.first);
}
max_count = std::max(max_count,it.first->second);
}

//  Bucket Sorting
/*  We are iterating over the vector and not the map
this ensures we are iterating by the order they got inserted */
std::vector<std::vector<std::string>> buckets(max_count);
for (auto o : order){
int count = o->second;
buckets[count-1].push_back(o->first);
}

std::vector<std::string> res;
for (auto it = buckets.rbegin(); it != buckets.rend(); ++it)
for (auto& str : *it)
res.push_back(str);
`````` 