Sort an array without changing position of negative numbers

Given an array arr[] of N integers, the task is to sort the array without changing the position of negative numbers (if any) i.e. the negative numbers need not be sorted.

Examples:

Input: arr[] = {2, -6, -3, 8, 4, 1}
Output: 1 -6 -3 2 4 8

Input: arr[] = {-2, -6, -3, -8, 4, 1}
Output: -2 -6 -3 -8 1 4

Approach: Store all the non-negative elements of the array in another vector and sort this vector. Now, replace all the non-negative values in the original array with these sorted values.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to sort the array such that
// negative values do not get affected
void sortArray(int a[], int n)
{
  
    // Store all non-negative values
    vector<int> ans;
    for (int i = 0; i < n; i++) {
        if (a[i] >= 0)
            ans.push_back(a[i]);
    }
  
    // Sort non-negative values
    sort(ans.begin(), ans.end());
  
    int j = 0;
    for (int i = 0; i < n; i++) {
  
        // If current element is non-negative then
        // update it such that all the
        // non-negative values are sorted
        if (a[i] >= 0) {
            a[i] = ans[j];
            j++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 2, -6, -3, 8, 4, 1 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    sortArray(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to sort the array such that
// negative values do not get affected
static void sortArray(int a[], int n)
{
  
    // Store all non-negative values
    Vector<Integer> ans = new Vector<>();
    for (int i = 0; i < n; i++)
    {
        if (a[i] >= 0)
            ans.add(a[i]);
    }
  
    // Sort non-negative values
    Collections.sort(ans);
  
    int j = 0;
    for (int i = 0; i < n; i++)
    {
  
        // If current element is non-negative then
        // update it such that all the
        // non-negative values are sorted
        if (a[i] >= 0
        {
            a[i] = ans.get(j);
            j++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < n; i++)
        System.out.print(a[i] + " ");
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 2, -6, -3, 8, 4, 1 };
  
    int n = arr.length;
  
    sortArray(arr, n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to sort the array such that
# negative values do not get affected
def sortArray(a, n):
  
    # Store all non-negative values
    ans=[]
    for i in range(n):
        if (a[i] >= 0):
            ans.append(a[i])
  
    # Sort non-negative values
    ans = sorted(ans)
  
    j = 0
    for i in range(n):
  
        # If current element is non-negative then
        # update it such that all the
        # non-negative values are sorted
        if (a[i] >= 0):
            a[i] = ans[j]
            j += 1
  
    # Pr the sorted array
    for i in range(n):
        print(a[i],end = " ")
  
  
# Driver code
  
arr = [2, -6, -3, 8, 4, 1]
  
n = len(arr)
  
sortArray(arr, n)
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System.Collections.Generic;
using System;
  
class GFG 
{
  
// Function to sort the array such that
// negative values do not get affected
static void sortArray(int []a, int n)
{
  
    // Store all non-negative values
    List<int> ans = new List<int>();
    for (int i = 0; i < n; i++)
    {
        if (a[i] >= 0)
            ans.Add(a[i]);
    }
  
    // Sort non-negative values
    ans.Sort();
  
    int j = 0;
    for (int i = 0; i < n; i++)
    {
  
        // If current element is non-negative then
        // update it such that all the
        // non-negative values are sorted
        if (a[i] >= 0) 
        {
            a[i] = ans[j];
            j++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < n; i++)
        Console.Write(a[i] + " ");
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 2, -6, -3, 8, 4, 1 };
  
    int n = arr.Length;
  
    sortArray(arr, n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

1 -6 -3 2 4 8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.