以负数、零和正数顺序重新排列数组
给定一个大小为N的arr[ ] ,包含正整数、负整数和一个零。任务是重新排列数组,使所有负数都在 0 的左侧,所有正数都在右侧。
注意:保持数字的顺序不是强制性的。如果有多个可能的答案,请打印其中任何一个。
例子:
Input: arr[] = {1, 0, -2, 3, 4, -5, -7, 9, -3}
Output: -2 -3 -5 -7 0 1 9 3 4
Explanation: The negative numbers are -2, -5, -7 and -3.
Input: arr[] = {-10, 5, -2, 0, -4, 5, 17, -9, -13, 11}
Output: -10 -2 -4 -13 -9 0 17 5 5 11
幼稚的方法:创建一个大小为N的数组,并从开头推送负数,从末尾推送正数。另外,用 0 填充单个剩余位置。
时间复杂度: O(N)
辅助空间: O(N)
高效方法:想法是找到数组中存储0的索引。找到 0 后,将该索引视为枢轴。将所有负值移动到枢轴的左侧,因此右侧值将自动为正。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to rearrange the array,
// such that all the negative appear
// on the left side of 0 and all
// positive appear on the right side of 0
void rearrangeArray(int arr[], int N)
{
int i, ind;
// Find index of 0
for (i = 0; i < N; i++) {
if (arr[i] == 0) {
ind = i;
break;
}
}
// Pivot the 0 element.
int j = -1, k, temp;
for (k = 0; k < N; k++) {
if (arr[k] < 0) {
j += 1;
// Don't swap with pivot.
if (arr[j] == 0)
j += 1;
swap(arr[j], arr[k]);
}
}
// swap the pivot with last negative.
swap(arr[ind], arr[j]);
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 0, -2, 3, 4, -5,
-7, 9, -3 };
int N = sizeof(arr) / sizeof(int);
rearrangeArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to rearrange the array,
// such that all the negative appear
// on the left side of 0 and all
// positive appear on the right side of 0
static void rearrangeArray(int arr[], int N)
{
int i, ind = -1;
// Find index of 0
for (i = 0; i < N; i++) {
if (arr[i] == 0) {
ind = i;
break;
}
}
// Pivot the 0 element.
int j = -1, k, temp;
for (k = 0; k < N; k++) {
if (arr[k] < 0) {
j += 1;
// Don't swap with pivot.
if (arr[j] == 0)
j += 1;
//swap
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
// swap the pivot with last negative.
int temp2 = arr[j];
arr[j] = arr[ind];
arr[ind] = temp2;
for (j = 0; j < N; j++) {
System.out.print(arr[j] + " ");
}
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 1, 0, -2, 3, 4, -5,
-7, 9, -3 };
int N = arr.length;
rearrangeArray(arr, N);
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python code for the above approach
# Function to rearrange the array,
# such that all the negative appear
# on the left side of 0 and all
# positive appear on the right side of 0
def rearrangeArray(arr, N):
ind = None
# Find index of 0
for i in range(N):
if (arr[i] == 0):
ind = i;
break;
# Pivot the 0 element.
j = -1
temp = None
for k in range(N):
if (arr[k] < 0):
j += 1;
# Don't swap with pivot.
if (arr[j] == 0):
j += 1;
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
# swap the pivot with last negative.
temp = arr[ind];
arr[ind] = arr[j];
arr[j] = temp;
for i in range(N):
print(arr[i], end=" ");
# Driver Code
arr = [1, 0, -2, 3, 4, -5, -7, 9, -3];
N = len(arr)
rearrangeArray(arr, N);
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to rearrange the array,
// such that all the negative appear
// on the left side of 0 and all
// positive appear on the right side of 0
static void rearrangeArray(int[] arr, int N)
{
int i, ind = -1;
// Find index of 0
for (i = 0; i < N; i++)
{
if (arr[i] == 0)
{
ind = i;
break;
}
}
// Pivot the 0 element.
int j = -1, k, temp;
for (k = 0; k < N; k++)
{
if (arr[k] < 0)
{
j += 1;
// Don't swap with pivot.
if (arr[j] == 0)
j += 1;
//swap
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
// swap the pivot with last negative.
int temp2 = arr[j];
arr[j] = arr[ind];
arr[ind] = temp2;
for (j = 0; j < N; j++)
{
Console.Write(arr[j] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 0, -2, 3, 4, -5, -7, 9, -3 };
int N = arr.Length;
rearrangeArray(arr, N);
}
}
// This code is contributed by gfgking
Javascript
输出
-2 -3 -5 -7 0 1 3 9 4
时间复杂度: 在)
辅助空间: O(1)