0

I'm trying to answer this test from Geeks for Geeks to count the number of inversions some array need to be sorted. I know my code works but they need me to optimize my code in order to post it. The register keyword was something I implemented, but it still doesn't work... Could you guys help me, please?

// Your Code Here
register int i, j, cont = 0;

for(i = 0; i < N; i++) {
    j = i + 1;
    while(j < N) {
        if(arr[i] > arr[j] && i < j) {
            cont++;
        }
        j++;
    }
}

return cont;
  • To do this properly you'll want to split this out into a *comparator* and a *sorter* that uses some arbitrary comparator. – tadman Jan 22 '21 at 23:25
  • Use of `register` here is really unwarranted. Let the compiler figure out if that's necessary. Use `-O3` and don't fuss over coaching. – tadman Jan 22 '21 at 23:26
  • `i < j` is always true as `j = i + 1` and `j` only increments subsequently, so you can remove this check, also use preincrement. – 4xy Jan 22 '21 at 23:53

0 Answers0