Transitioning and playing with numbers is one of the most fascinating aspects of compiling array data structures. On that note, did you know that arrays can be used for storing infinite natural numbers and digits that have been discovered till date?
One such number component that we are going to be discussing in this blog are integers. Integers are numbers that can be positive, negative and even zero.
In this array problem, we are going to be discussing how to move all negative elements to the end or the beginning of an array by using simple approaches and algorithms.
Keep in mind that the order of the elements is not relevant for this problem. We are only considering the negative elements and placing them at either the beginning or the end of the list.
What are the methods to move all negative elements to one side of an Array?
The arrays in programming are one of the most versatile data structures. This is due to the fact that, from numbers to integers, the arrays facilitate storing infinite positive, negative, and even zero value integers as well. Also, these elements within the array might be stored in random positions irrespective of their general sequence.
There are plenty of algorithms and methods that exist to help in sorting and moving these elements within the arrays. Hence, we can say that this is a form of sorting that is performed on the array as per the requirements of the program.
This problem is often asked in programming interviews. In the following section of the blog, we will discuss these approaches to move all negative elements to end of an array.
How to move all elements to one side in an Array using C?
In order to learn how to move all negative elements to the end of an array, we will consider this problem with the help of an example. Here we have an input array that consists of negative as well as positive integers.
Let us discuss the approaches and methods to move the negative elements towards one side of the array further in this section.
1 -2 3 -1 8 9
1 9 3 8 -2 -1
As we can see in the output of the array, the negative elements have been arranged at the end of the sequence.
Keep in mind that the arrangement of these elements do not have to be in a perfect order. They can be aligned in a random order as long as we move all negative elements to end within the program.
Now, let us discuss the different approaches that can be applied for shifting all the negative elements by the end of the array.
Method 1: Using the Naive Approach
We will start by attempting to solve this problem using the naive approach. This method is also commonly referred to as the recursive algorithm that is used for solving various other programming problems as well such as performing a zigzag traversal of Binary Tree.
- In the context of moving all the negative elements to the end of an array, we can start by sorting the given array in a descending order
- This way, all the negative elements will get piled up at the end of the array
- For achieving this we can implement the selection sort function i.e selection sort(int arr, int n)
- In this function, for each of the ith element within the array, we will try to figure out the minimum element within the subarray from i+1 and try to swap their values to sort the array at the end
Time Complexity for this approach:
Method 2: Using Partition Approach
Now we are moving towards more efficient and convenient approaches. For this method, we will be using the nested loops for performing only a single traversal through the entire array.
A simple method to use the partition approach is to implement the nested loop for the entire length of the array and this loop will help us in detecting the negative integers that need to be moved to the end.
Here’s how this approach can be implemented:
- You can start by declaring a variable such as j=0 at the starting of the array for arr[j]
- Next up, we will iterate through the entire data using the i pointer of the loop
- Now, at each step of the iteration, we will keep a tab of the fact whether arr[i] is negative or not
- If it is found to be true, we will simply swap the element with arr[j] and place it at the beginning of our input array
- Finally, you can increment the element j and its value and carry this process forward for the remaining elements of the array
Time Complexity for this approach:
Here we have considered “n” to be length of our input array
Method 3: Using the Variables or Pointers technique
As you might have already guessed, in order to move all negative elements to the end, as our third approach, we will initialise two variables or pointers “m” and “n” at the start and end positions of the input array.
By using this technique, we will keep a track of all the elements within the array and consecutively increment the m pointer and decrement the n pointer.
Here’s how the algorithm for this approach would work:
- If both the m and n pointers point are pointing to negative elements, then increment the element’s value to m
- If the value of pointer m has a positive number and pointer n has a negative number then swap both of their values
- In case, both of the pointers are indicating to a positive integer, then decrement the pointer n
- And finally, if m points towards a negative element while n points to positive, then increment m and decrement n
There are a wide range of array based problems that can be solved using Pointers and the Naive Approach such as performing zigzag traversal of Binary Tree.
These approaches offer their own set of classes and functions that can be effectively used for reducing the time complexity of the problem.