根据元素的绝对值对已排序的给定数组进行排序
给定一个大小为N的数组arr[] ,根据其元素的绝对值进行排序。任务是根据元素的实际值对该数组进行排序。
例子:
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
方法:这个问题可以使用双端队列来解决。想法是从左到右遍历数组,在deque的前面插入负元素,在后面插入正元素。现在从双端队列前面弹出元素以填充数组并获得答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to sort
void SortWithoutSorting(int arr[], int N)
{
deque dq;
for (int i = 0; i < N; i++) {
// Pushing negative elements in
// the front of the deque
if (arr[i] < 0) {
dq.push_front(arr[i]);
}
// Pushing positive elements in
// the back of the deque
else {
dq.push_back(arr[i]);
}
}
// Preparing the output array
int i = 0;
for (auto it = dq.begin(); it !=
dq.end(); it++)
arr[i++] = *it;
}
// Function to print the array.
void showArray(int arr[], int N)
{
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver Code
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
// Java Program for the above approach
import java.util.*;
class GFG {
// Function to sort
public static void SortWithoutSorting(int arr[], int N)
{
Deque dq = new ArrayDeque();
for (int i = 0; i < N; i++) {
// Pushing negative elements in
// the front of the deque
if (arr[i] < 0) {
dq.addFirst(arr[i]);
}
// Pushing positive elements in
// the back of the deque
else {
dq.addLast(arr[i]);
}
}
// Preparing the output array
int i = 0;
for (Iterator it = dq.iterator();
it.hasNext();) {
arr[i++] = (int)it.next();
}
}
// Function to print the array.
public static void showArray(int arr[], int N)
{
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 1, -2, 3, -4, -5, 6 };
int N = arr.length;
SortWithoutSorting(arr, N);
showArray(arr, N);
}
}
// This code is contributed by Shubham Singh
Python3
# Python code for the above approach
# Function to sort
def SortWithoutSorting(arr, N):
dq = []
for i in range(N):
# Pushing negative elements in
# the front of the deque
if (arr[i] < 0):
dq.insert(0,arr[i])
# Pushing positive elements in
# the back of the deque
else:
dq.append(arr[i])
# Preparing the output array
i = 0
for it in dq:
arr[i] = it
i += 1
return arr
# Function to print the array.
def showArray(arr, N):
for i in range(N):
print(arr[i], end= " ")
# Driver Code
arr = [1, -2, 3, -4, -5, 6]
N = len(arr)
arr = SortWithoutSorting(arr, N)
showArray(arr, N)
# This code is contributed by Shubham Singh
C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to sort
public static void SortWithoutSorting(int[] arr, int N)
{
List dq = new List();
int i;
for (i = 0; i < N; i++) {
// Pushing negative elements in
// the front of the deque
if (arr[i] < 0) {
dq.Insert(0,arr[i]);
}
// Pushing positive elements in
// the back of the deque
else {
dq.Add(arr[i]);
}
}
// Preparing the output array
i = 0;
foreach(int it in dq) {
arr[i++] = it;
}
}
// Function to print the array.
public static void showArray(int[] arr, int N)
{
for (int i = 0; i < N; i++) {
Console.Write(arr[i] + " ");
}
}
// Driver Code
public static void Main ()
{
int[] arr = { 1, -2, 3, -4, -5, 6 };
int N = arr.Length;
SortWithoutSorting(arr, N);
showArray(arr, N);
}
}
// This code is contributed by Shubham Singh
Javascript
输出
-5 -4 -2 1 3 6
时间复杂度: O(N)
辅助空间: O(N)