# My sort method meets error

• Dear all, I use quick sort to do pre-process, however, it meets error as "runtime error". I use sort method in <algorithm>, then it is accepted.
I have two question:

1. Does it a linear method? I don't think so.
2. what's wrong in my quick sort?
code is below:
``````     #include <algorithm>
// my quick sort
int Compare(const void *elem1, const void *elem2)
{
return *((int *)(elem1)) - *((int *)(elem2)) > 0 ? 1 : -1;
}

class Solution {
public:

int singleNumber(int A[], int n)
{

if (n == 1 || n&0x01 == 0)
{
return A[0];
}

//qsort(A, n, sizeof(int), Compare); // my qsort
sort(A, A+n); // accept method

for (int i=1; i<n; i+=2)
{
if (A[i]^A[i-1])
return A[i-1];
}
return A[n-1];
}
};``````

• I believe using quick sort could never be a liner solution, since it is O(N log N).

• yes, of cause. do you have liner solution ?

• I met the same question. After changing qsort to sort, the problem was solved. I was confused about the qsort, it must be quicker than sort as some experiments show, then why qsort can't pass ?

• But qsort is indeed quicker than sort. why RE?

• Sure, I think qsort is quicker than sort. Maybe it's a overflow question. However, it's my guess. I will check qsort and sort to find their difference. ;)

• @ideno, as far as I know, qsort and sort are same.

• @kaleguan, look around in single number category, you might find some posts helpful. For example, this one

• change

``````   return *((int *)(elem1)) - *((int *)(elem2)) > 0 ? 1 : -1;
``````

to

`````` return (*((int *)(elem1)) > *((int *)(elem2))) ? 1 : -1;
``````

will make qsort work.

I think the reason is that the former one could cause integer overflow

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.