Sort a given array which is already sorted based on the absolute value of the element


In this article, we will sort the given array. The given array is already sorted on the basis of absolute value of the elements, we just need to sort the array based on the true values of the elements.

In the first approach, we will use a sorting algorithm, like merge sort, bubble sort, insertion sort, quicksort and so on. In this example, we will use the inbuilt sort function to sort our array.

In the second approach, we will use a double ended queue. We will push the positive elements in front of the double ended queue, and we will push the negative elements in the back of the double ended queue. This method will allow us to get a sorted array without performing and sorting algorithms.

Problem Statement

Given an array with N integers which are sorted on the basis of absolute value of the elements, we need to sort the array based on the true value of the elements in the array.

Sample Examples

Input

arr = {1, 2, -3, -8, 16}

Output

-8, -3, 1, 2, 16

Explanation

After sorting the input array in ascending order, the negative values come at the beginning of the array, and we get the final output.

Input

arr = {8, -8, -8, 8}

Output

-8, -8, 8, 8

Explanation

Since –8 is smaller than 8, it should come before it after sorting.

Input

arr = {-2, -33, -71, 81, 93}

Output

-71, -33, -2, 81, 93

Explanation

The negative values will appear in reverse order in the output.

Approach 1

In this approach, we will use a sorting technique to sort the input array. There are multiple sorting techniques that we can use, like merge sort, quick sort, bubble sort, selection sort, insertion sort and so on. The time and space complexity of the solution will depend on the type of sorting technique used. In the example below, we will use the inbuilt sort function to sort the array.

Algorithm

  • Step 1 − Get the input array as an argument.

  • Step 2 − Make a new array, res, and copy the input array elements to res preserving the same order.

  • Step 3 − Sort the new array, res, by using the inbuilt sort function.

  • Step 4 − Return the sorted array.

Example

#include <bits/stdc++.h>
using namespace std;
vector<int> sort_abs_sorted(vector<int> &arr){
   vector<int> res = arr;
   sort(res.begin(),res.end());
   return res;
}
int main(){
   vector<int> arr = {-2, -33, -71, 81, 93};
   vector<int> sorted = sort_abs_sorted(arr);
   for(int i=0;i<sorted.size();i++)cout<<sorted[i]<<" ";
   return 0;
}

Output

-71 -33 -2 81 93

Time complexity − O(N*log(N)), where N is the number of elements in the array.

Space complexity − O(N), to store the elements in the new array

Approach 2

In this approach, we will use a double ended queue to sort the array based on the true values of the elements. First, we will traverse our original array from left to right. If the current element is positive, we push the element in the back of the queue. If the current element is negative, we will push the element in the front of the queue. After we traverse through all the elements, we make another array to store the sorted elements. We get the elements from the double ended queue one by one and add it to our final array.

Algorithm

  • Step 1 − Make a double ended queue.

  • Step 2 − Traverse the input array from left to right.

  • Step 2.1 − If the current element is positive, push it to the back of the double ended queue.

  • Step 2.2 − If the current element is negative, push it to the front of the queue.

  • Step 3 − Make a final array to store the sorted elements.

  • Step 3.1 − Pop the elements from deque and add them to the final array.

  • Step 3.3 − Return the sorted array.

Example

#include <bits/stdc++.h>
using namespace std;
vector<int> sort_abs_sorted(vector<int> &arr){
   vector<int> res;
   deque<int> deq;
   for(int i=0;i<arr.size();i++){
      if(arr[i]>=0)deq.push_back(arr[i]);
      else deq.push_front(arr[i]);
   }
   for(auto it:deq)res.push_back(it);
   return res;
}
int main(){
   vector<int> arr = {-2, -33, -71, 81, 93};
   vector<int> sorted = sort_abs_sorted(arr);
   for(int i=0;i<sorted.size();i++)cout<<sorted[i]<<" ";
   return 0;
}

Output

-71 -33 -2 81 93

Time complexity − O(N), where N is the number of elements in the array.

Space complexity − O(N), to store the elements in the queue.

Updated on: 01-Nov-2023

41 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements