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++
#include <bits/stdc++.h>
using namespace std;
void sortArray( int a[], int n)
{
vector< int > ans;
for ( int i = 0; i < n; i++) {
if (a[i] >= 0)
ans.push_back(a[i]);
}
sort(ans.begin(), ans.end());
int j = 0;
for ( int i = 0; i < n; i++) {
if (a[i] >= 0) {
a[i] = ans[j];
j++;
}
}
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
}
int main()
{
int arr[] = { 2, -6, -3, 8, 4, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
sortArray(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void sortArray( int a[], int n)
{
Vector<Integer> ans = new Vector<>();
for ( int i = 0 ; i < n; i++)
{
if (a[i] >= 0 )
ans.add(a[i]);
}
Collections.sort(ans);
int j = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] >= 0 )
{
a[i] = ans.get(j);
j++;
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(a[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 2 , - 6 , - 3 , 8 , 4 , 1 };
int n = arr.length;
sortArray(arr, n);
}
}
|
Python3
def sortArray(a, n):
ans = []
for i in range (n):
if (a[i] > = 0 ):
ans.append(a[i])
ans = sorted (ans)
j = 0
for i in range (n):
if (a[i] > = 0 ):
a[i] = ans[j]
j + = 1
for i in range (n):
print (a[i],end = " " )
arr = [ 2 , - 6 , - 3 , 8 , 4 , 1 ]
n = len (arr)
sortArray(arr, n)
|
C#
using System.Collections.Generic;
using System;
class GFG
{
static void sortArray( int []a, int n)
{
List< int > ans = new List< int >();
for ( int i = 0; i < n; i++)
{
if (a[i] >= 0)
ans.Add(a[i]);
}
ans.Sort();
int j = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] >= 0)
{
a[i] = ans[j];
j++;
}
}
for ( int i = 0; i < n; i++)
Console.Write(a[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 2, -6, -3, 8, 4, 1 };
int n = arr.Length;
sortArray(arr, n);
}
}
|
Javascript
<script>
function sortArray(a, n)
{
var ans = [];
for ( var i = 0; i < n; i++) {
if (a[i] >= 0)
ans.push(a[i]);
}
ans.sort((a,b)=> a-b);
var j = 0;
for ( var i = 0; i < n; i++) {
if (a[i] >= 0) {
a[i] = ans[j];
j++;
}
}
for ( var i = 0; i < n; i++)
document.write( a[i] + " " );
}
var arr = [2, -6, -3, 8, 4, 1];
var n = arr.length;
sortArray(arr, n);
</script>
|
Time Complexity: O(n * log n) // sorting an array takes n logn time and traversing the array takes linear time, hence the overall complexity turns out to be O(n * log n)
Auxiliary Space: O(n) // since an extra array is used so the solution takes space equal to the length of the array
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Aug, 2022
Like Article
Save Article