Sort given Array which is already Sorted based on absolute values of elements
Last Updated :
17 Jan, 2024
Given an array arr[] of size N, sorted based on the absolute value of its elements. The task is to sort this array based on the actual values of the elements.
Examples:
Input: arr[] = {5, -7, 10, -11, 18}
Output: -11, -7, 5, 10, 18
Explanation: When the array is sorted the negative values will come at the beginning of the array.
Input: arr[] = {1, -2, -3, 4, -5}
Output: -5, -3, -2, 1, 4
Naive Approach:
The naive approach to solve the problem is to use inbuilt sort function to sort the array.
Algorithm:
- Take the input array arr[] of size N as input.
- Use the inbuilt sort function to sort the array in ascending order based on the absolute values of the elements.
- Print the sorted array.
Below is the implementation of the approach:
C++
#include<bits/stdc++.h>
using namespace std;
int main() {
int arr[] = { 1, -2, 3, -4, -5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
sort(arr, arr + n);
for ( int i=0; i<n; i++) {
cout<<arr[i]<< " " ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
int [] arr = { 1 , - 2 , 3 , - 4 , - 5 , 6 };
int n = arr.length;
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
from typing import List
def main():
arr: List [ int ] = [ 1 , - 2 , 3 , - 4 , - 5 , 6 ]
n: int = len (arr)
arr.sort()
for i in range (n):
print (arr[i], end = " " )
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static void Main()
{
int [] arr = { 1, -2, 3, -4, -5, 6 };
int n = arr.Length;
Array.Sort(arr);
foreach ( var element in arr)
{
Console.Write(element + " " );
}
Console.WriteLine();
}
}
|
Javascript
let arr = [1, -2, 3, -4, -5, 6];
let n = arr.length;
arr.sort((a, b) => a - b);
for (let i = 0; i < n; i++) {
console.log(arr[i] + " " );
}
|
Time Complexity: O(N*logN) as sort function has been called. Here, N is size of input array.
Space Complexity: O(1) as no extra space has been used.
Approach: This problem can be solved using double ended queue. The idea is to traverse the array from left to right and insert the negative elements in the front and the positive elements in the back of the deque. Now pop the elements from the front of the deque to fill the array and get the answer.
Below is the implementation of the above approach:
C++
#include <deque>
#include <iostream>
using namespace std;
void SortWithoutSorting( int arr[], int N)
{
deque< int > dq;
for ( int i = 0; i < N; i++) {
if (arr[i] < 0) {
dq.push_front(arr[i]);
}
else {
dq.push_back(arr[i]);
}
}
int i = 0;
for ( auto it = dq.begin(); it !=
dq.end(); it++)
arr[i++] = *it;
}
void showArray( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, -2, 3, -4, -5, 6 };
int N = sizeof (arr) / sizeof ( int );
SortWithoutSorting(arr, N);
showArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void SortWithoutSorting( int arr[], int N)
{
Deque<Integer> dq = new ArrayDeque<Integer>();
for ( int i = 0 ; i < N; i++) {
if (arr[i] < 0 ) {
dq.addFirst(arr[i]);
}
else {
dq.addLast(arr[i]);
}
}
int i = 0 ;
for (Iterator it = dq.iterator();
it.hasNext();) {
arr[i++] = ( int )it.next();
}
}
public static void showArray( int arr[], int N)
{
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 1 , - 2 , 3 , - 4 , - 5 , 6 };
int N = arr.length;
SortWithoutSorting(arr, N);
showArray(arr, N);
}
}
|
Python3
def SortWithoutSorting(arr, N):
dq = []
for i in range (N):
if (arr[i] < 0 ):
dq.insert( 0 ,arr[i])
else :
dq.append(arr[i])
i = 0
for it in dq:
arr[i] = it
i + = 1
return arr
def showArray(arr, N):
for i in range (N):
print (arr[i], end = " " )
arr = [ 1 , - 2 , 3 , - 4 , - 5 , 6 ]
N = len (arr)
arr = SortWithoutSorting(arr, N)
showArray(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public static void SortWithoutSorting( int [] arr, int N)
{
List< int > dq = new List< int >();
int i;
for (i = 0; i < N; i++) {
if (arr[i] < 0) {
dq.Insert(0,arr[i]);
}
else {
dq.Add(arr[i]);
}
}
i = 0;
foreach ( int it in dq) {
arr[i++] = it;
}
}
public static void showArray( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
Console.Write(arr[i] + " " );
}
}
public static void Main ()
{
int [] arr = { 1, -2, 3, -4, -5, 6 };
int N = arr.Length;
SortWithoutSorting(arr, N);
showArray(arr, N);
}
}
|
Javascript
<script>
function SortWithoutSorting(arr, N)
{
let dq = [];
for (let i = 0; i < N; i++) {
if (arr[i] < 0) {
dq.unshift(arr[i]);
}
else {
dq.push(arr[i]);
}
}
let i = 0;
for (let it of dq)
arr[i++] = it;
}
function showArray(arr, N) {
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " )
}
}
let arr = [1, -2, 3, -4, -5, 6];
let N = arr.length;
SortWithoutSorting(arr, N);
showArray(arr, N);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...