Rearrange positive and negative numbers using inbuilt sort function in C++

C++Server Side ProgrammingProgramming

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements of an array are sorted using the inbuilt sort function of C++ STL as well as using recursive technique of coding and printing the result.

Let us see various input output scenarios for this −

Input − int arr[] = {4, 2, -1, -1, 6, -3, 0}

Output − Rearrangement of positive and negative numbers using inbuilt sort function is: -3 -1 -1 0 2 4 6.

Explanation − we are given an integer array of size 7 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements of an array are sorted i.e. all the negative elements appear before all the positive elements and the final result will be -3 -1 -1 0 2 4 6.

Input − int arr[] = {-9, -10, 2, 3, 10, 5, 8, 4}

Output − Rearrangement of positive and negative numbers using inbuilt sort function is: -10 -9 2 3 4 5 8 10.

Explanation − we are given an integer array of size 8 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements of an array are sorted i.e. all the negative elements appear before all the positive elements and the final result will be -10 -9 2 3 4 5 8 10.

Approach used in the below program is as follows

Using sort()

  • Input an array of integer type elements and calculate the size of an array.

  • Pass the data to the function Rearrangement(int arr[], int size)

  • Inside the function, Rearrangement(int arr[], int size)

    • Call the sort function of C++ STL by passing array and size of an array as a parameter to the function and it will return the sorted array.

  • Print the result.

Using Recursion

  • Input an array of integer type elements and calculate the size of an array.

  • Declare a temporary variable, let's say, temp.

  • Start loop FOR from i to till i less than size of an array. Inside the loop, check if arr[i] less than 0 then increment the temp by 1.

  • Call the Rearrangement(arr, 0, (size - 1)) by passing array, 0, and size -1 as a parameter to the function.

  • Call the rotate function by passing array, temp and size - 1 to the function.

  • Inside the function Rearrangement(int arr[], int first, int last)

    • Check IF first equals last then return.

    • Make a recursive call to the function Rearrangement() and pass array, first + 1 and last element as a parameter.

    • Check IF arr[first] greater than 0 then make a call to the function as Rotate(arr, (first + 1), last) and Rotate(arr, first, last)

  • Inside the function Rotate(int arr[], int first, int last)

    • Start while first less than last. Inside the while, declare the integer variable as temp and set it with arr[first] then set arr[first] to arr[last] and arr[last] to temp. Increment the first by 1 and decrement the last by 1.

  • Print the result.

1. Using the sort() function

Example

#include <bits/stdc++.h>
using namespace std;
//using the sort() function
void Rearrangement(int arr[], int size){
   sort(arr, arr + size);
}
int main(){
   int arr[] = {4, 2, -1, -1, 6, -3, 0};
   int size = sizeof(arr)/sizeof(arr[0]);
   //calling the function to rearrange the array
   Rearrangement(arr, size);
   //print the array after rearranging the values
   cout<<"Rearrangement of positive and negative numbers using inbuilt sort function is: ";
   for(int i = 0; i < size; i++){
      cout<< arr[i] << " ";
   }
   return 0;
}

Output

If we run the above code it will generate the following Output

Rearrangement of positive and negative numbers using inbuilt sort function is: -3 -1 -1 0 2 4 6

2. Recursive call to a function

Example

#include <bits/stdc++.h>
using namespace std;
void Rotate(int arr[], int first, int last){
   while(first < last){
      int temp = arr[first];
      arr[first] = arr[last];
      arr[last] = temp;
      first++;
      last--;
   }
}
void Rearrangement(int arr[], int first, int last){
   if(first == last){
      return;
   }
   Rearrangement(arr, (first + 1), last);
   if(arr[first] >= 0){
      Rotate(arr, (first + 1), last);
      Rotate(arr, first, last);
   }
}
int main(){
   int arr[] = {4, 2, -1, -1, 6, -3, 0};
   int size = sizeof(arr)/sizeof(arr[0]);
   int temp = 0;
   for(int i = 0; i < size; i++){
      if(arr[i] < 0){
         temp++;
      }
   }
   //calling the function to rearrange the array
   Rearrangement(arr, 0, (size - 1));
   Rotate(arr, temp, (size - 1));
   //print the array after rearranging the values
   cout<<"Rearrangement of positive and negative numbers using Recursion is: ";
   for(int i = 0; i < size; i++){
      cout<< arr[i] << " ";
   }
   return 0;
}

Output

If we run the above code it will generate the following Output

Rearrangement of positive and negative numbers using Recursion is: -1 -1 -3 4 2 6 0
raja
Updated on 02-Nov-2021 06:51:10

Advertisements