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 say, leave the ordering of two elements as they are in the original array if they have the same value used for ordering.   I'm not sure how to do this using qsort. I would rather not use an alternative sorting library/ roll my own.   Thanks  --  edit: now I know the distinction between stable and unstable sorts I realise this is a duplicate.
122778
I'm assuming that the good old qsort function in stdlib is not stable, because the man page doesn't say anything about it.  This is the function I'm talking about:     #include &lt;stdlib.h&gt;    void qsort(void *base, size_t nmemb, size_t size,               int(*compar)(const void *, const void *));     I assume that if I change my comparison function to also include the address of that which I'm comparing, it will be stable.  Is that correct?    Eg:  int compareFoos( const void* pA, const void *pB ) {     Foo *pFooA = (Foo*) pA;     Foo *pFooB = (Foo*) pB;      if( pFooA-&gt;id &lt; pFooB-&gt;id ) {         return -1;     } else if( pFooA-&gt;id &gt; pFooB-&gt;id ) {         return 1;     } else if( pA &lt; pB ) {         return -1;                 } else if( pB &gt; pA ) {        return 1;     } else {        return 0;     } }
99528
I'm writing a paper about an algorithm that I have developed. Just for illustration, I will say that the method name is "quicksort". My question is about the usage of the in the following context:     This paper proposes quicksort, a novel and fast algorithm. The advantage of   quicksort is that...   My question is whether I should use "The advantage of the quicksort..." or "The advantage of quicksort".  I am also looking for resources explaining the usage of the in this context.     [Meta] Usually, to check if a certain sentence is correct, I search Google using wildcards. However, in this case, the correct answer is depends on the context. I have also tried to find a answer in the following book but without success: Science Research Writing: A Guide for Non-Native Speakers of English.
34342
I saw this theorem referenced in a paper on performing shuffles in an array. There is a proof on pages 20-21 of the , but the proof is very terse and omits a lot of intuition.  Is there an intuitive explanation for why this theorem is true, or at least a proof that omits fewer details?
123805
After reading through a few other questions I was just asking myself: How was the Order of Operations defined, and why is it this specific order and not a different one?  Most of us know things like multiplication/division before addition/subtraction, parentheses first, etc - but what's the actual reason behind it? I'm probably biased by following those rules since childhood, so I can't really think of any other way.  2 + 2 x 2 = 6 and not 8  But if the order would be changed, let's say to "addition/subtraction before multiplication/division" would that order still work if we assume that mathematics would build up on it? Or is there some strange mathematical problem if we would be using a different order?  Of course, parentheses have a grouping function so they should always come first - I'm mainly talking about the exponents, multiplication/division, addition/subtraction (and maybe other operations left out).
81019
It seems that sort behaves strangely on lines such as &gt;&gt;b    $ cat test a &gt;&gt;b b c &gt; &gt;&gt;  $ sort test &gt; &gt;&gt; a b &gt;&gt;b c   I would expect the &gt;&gt;b line to be the third line output by sort but it is the fifth. Why does it do this and is there a way to make sort give my expected output?    I'm using GNU/Linux Ubuntu 16.04.
18904
I believe there's a way to find the kth largest element in an unsorted array of length n in O(n).  Or perhaps it's "expected" O(n) or something.  How can we do this?
27143
I use LINQ to Objects instructions on an ordered array.  Which operations shouldn't I do to be sure the order of the array is not changed?
62506
Using alphabetical ordering for authors' names is a long established tradition in mathematics. In the situations of extremely unequal contributions, is it reasonable to break from this tradition?  Has there been any examples?  If it is common, what is the threshold for breaking from alphabetical ordering?
32561
Is it possible to find the greatest of two integers without any comparison? I found some  solutions:   if(!(a/b)) // if a is less than b then division result will be zero. {     cout &lt;&lt; " b is greater than a"; } else if (!(a-b)) // we know a is greater than or equal to b now.  check whether they are equal. {     cout &lt;&lt; "a and b are equal"; } else     cout &lt;&lt; "a is greater than b";   But if(c) or if(!c) is a comparison to zero. In addition it doesn't work for negative numbers. In fact I need a solution that avoids any if statement. Instead I should use switch statements and arithmetic operators. ThanX.
11800
A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java?  Stack Overflow question  is similar to this question, but requires a more academic answer. The scenario above demonstrates why it would be necessary to have one, and I'm wondering if there is any equivalent solution.
93369
I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
16259
If $a$ is a group element, prove that $a$ and $a^{-1}$ have the same order.   I tried doing this by contradiction.    Assume $|a|\neq|a^{-1}|$  Let $a^n=e$ for some $n\in \mathbb{Z}$ and $(a^{-1})^m=e$ for some $m\in \mathbb{Z}$, and we can assume that $m &lt; n$.  Then $e= e*e = (a^n)((a^{-1})^m) = a^{n-m}$. However, $a^{n-m}=e$ implies that $n$ is not the order of $a$, which is a contradiction and $n=m$.  But I realized this doesn't satisfy the condition if $a$ has infinite order. How do I prove that piece?
15071
Suppose $m, n$ are infinite ordinal numbers. $$a) m=n → |m|=|n|$$ $$b)|m|=|n| →m=n$$ $$c)m&lt;n→ |m|&lt;|n|$$ $$d)|\max{(m,n)}|&lt; |m|+|n|$$ $$e)|m|&lt;|n| →|m|^{|n|}&lt;|n|^m$$  Which of the above statements are true? (a) looks true but I do not know the way to work it out. Please help
123930
$m \times n$ persons stand as a rectangle of $m$ rows and $n$ columns. Each one is taller than the one next to his left.  Now, if commander orders each column to sort by height, prove that after such sorting, each one is still taller than the one next to his left.   I'm reading a Combinatorics book on my own, but some exercises I can't solve... many thanks!
28594
Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:  from sets import Set [...] myHash = Set(myList)   but I don't know how to retrieve the list members from the hash in alphabetical order.  I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I'd prefer a solution that is expressed in code clearly to a fast but more opaque one.
30140
I want to get a list of files in a directory, but I want to sort it such that the oldest files are first.  My solution was to call File.listFiles and just resort the list based on File.lastModified, but I was wondering if there was a better way.  Edit: My current solution, as suggested, is to use an anonymous Comparator:  File[] files = directory.listFiles();  Arrays.sort(files, new Comparator&lt;File&gt;(){     public int compare(File f1, File f2)     {         return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());     } });
626
Anyone have a good resource or provide a sample of a natural order sort in C# for an FileInfo array?  I am implementing the IComparer interface in my sorts.