-1

This is the question I am looking at:

Given an unsorted array Arr of N positive and negative numbers. Your task is to create an array of positive and negative numbers without changing the relative order of positive and negative numbers.

So, here's what my naive mind thought! I use a variation of insertion sort.

Here is the code:

void reorder(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        int hole = i;
        while ((arr[hole] < 0) && (hole > 0)) {
            int temp = arr[hole];
            arr[hole] = arr[hole - 1];
            arr[hole - 1] = temp;
            hole--;
        } 
    }
}

This code changes the relative position of negative numbers, which is not desired. How can I avoid doing that in my code?

Example

Input:

arr = { -12, 11, -13, -5, 6, -7, 5, -3, -6 };

Expected result:

-12 -13 -5 -7 -3 -6 11 6 5 

But my result is:

-6 -3 -7 -5 -13 -12 11 6 5
trincot
  • 317,000
  • 35
  • 244
  • 286
Roushan
  • 45
  • 4
  • 2
    ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Mar 19 '21 at 08:20
  • 1
    Sounds like a job for [`std::partition`](https://en.cppreference.com/w/cpp/algorithm/partition). – tadman Mar 19 '21 at 08:22
  • 4
    Maybe better [std::stable_partition](https://en.cppreference.com/w/cpp/algorithm/stable_partition), in order to keep the relative positions. – Damien Mar 19 '21 at 08:23
  • 2
    And if you want to implement `stable_partition` yourself for some reason, take a look at this [answer](https://stackoverflow.com/a/59225308). Note that in-place stable partition cannot be implemented in `O(n)` time. – Evg Mar 19 '21 at 08:37
  • @Damien Even better, yes. Thanks for pointing that out. – tadman Mar 19 '21 at 08:39
  • The problem with such homework questions is that there are often unstated and arbitrary constraints on what can be used. E.g. the absence of `std::vector` and `std::swap` suggests that the Standard Library can't be used. This would mean that the teacher is not competent to teach C++ . – MSalters Mar 19 '21 at 09:08
  • Your while loop is moving the "current" negative number to the beginning of the array, which is too far. (Use smaller test cases, so you can trace through it either by hand or by printing intermediate results.) – molbdnilo Mar 19 '21 at 09:12

1 Answers1

0

Note that the assignment says "Your task is to create an array"... so if I understand correctly, you are not asked to modify the input array, but to create one.

So then the solution is quite trivial:

  • Create an array of the same size as the input array
  • Iterate the input array and copy negative values into the new array
  • Iterate the input array again and now copy the positive values into the new array
  • Return the new array.

This has linear time complexity.

trincot
  • 317,000
  • 35
  • 244
  • 286