将所有负面元素按顺序移动到末尾,并允许额外的空间
给定一个负整数和正整数的未排序数组。任务是将所有负元素放在数组的末尾,而不改变正元素和负元素的顺序。
例子:
Input : arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 1 3 2 11 6 -1 -7 -5
Input : arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output : 7 9 10 11 -5 -3 -4 -1
我们在下面的帖子中讨论了解决这个问题的不同方法。
用恒定的额外空间重新排列正数和负数
如果我们被允许使用额外的空间,问题就会变得更容易。想法是创建一个空数组(temp[])。首先我们存储给定数组的所有正元素,然后我们将数组的所有负元素存储在 Temp[] 中。最后我们将 temp[] 复制到原始数组。
下面是上述想法的实现:
C/C++
C++
// C++ program to Move All -ve Element At End
// Without changing order Of Array Element
#include
using namespace std;
// Moves all -ve element to end of array in
// same order.
void segregateElements(int arr[], int n)
{
// Create an empty array to store result
int temp[n];
// Traversal array and store +ve element in
// temp array
int j = 0; // index of temp
for (int i = 0; i < n ; i++)
if (arr[i] >= 0 )
temp[j++] = arr[i];
// If array contains all positive or all negative.
if (j == n || j == 0)
return;
// Store -ve element in temp array
for (int i = 0 ; i < n ; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
// Copy contents of temp[] to arr[]
memcpy(arr, temp, sizeof(temp));
}
// Driver program
int main()
{
int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
segregateElements(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to Move All -ve Element At End
// Without changing order Of Array Element
import java.util.Arrays;
class GFG {
// Moves all -ve element to end of array in
// same order.
static void segregateElements(int arr[], int n)
{
// Create an empty array to store result
int temp[] = new int[n];
// Traversal array and store +ve element in
// temp array
int j = 0; // index of temp
for (int i = 0; i < n; i++)
if (arr[i] >= 0)
temp[j++] = arr[i];
// If array contains all positive or all
// negative.
if (j == n || j == 0)
return;
// Store -ve element in temp array
for (int i = 0; i < n; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
// Copy contents of temp[] to arr[]
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
// Driver code
public static void main(String arg[])
{
int arr[] = { 1, -1, -3, -2, 7, 5, 11, 6 };
int n = arr.length;
segregateElements(arr, n);
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to Move All -ve Element At End
# Without changing order Of Array Element
# Moves all -ve element to end of array in
# same order.
def move(arr,n):
j = 0
ans=[None]*n
i=0;j=n-1
for k in range(n):
if arr[k]>=0:
ans[i]=arr[k]
i+=1
else:
ans[j]=arr[k]
j-=1
ans[i:]=ans[n-1:i-1:-1]
return ans
# Driver program
arr = [1 ,-1 ,-3 , -2, 7, 5, 11, 6 ]
n = len(arr)
print(move(arr, n))
# Contributed by Venkatesh hegde
C#
// C# program to Move All -ve Element At End
// Without changing order Of Array Element
using System;
class GFG {
// Moves all -ve element to
// end of array in same order.
static void segregateElements(int[] arr, int n)
{
// Create an empty array to store result
int[] temp = new int[n];
// Traversal array and store +ve element in
// temp array
int j = 0; // index of temp
for (int i = 0; i < n; i++)
if (arr[i] >= 0)
temp[j++] = arr[i];
// If array contains all positive or all
// negative.
if (j == n || j == 0)
return;
// Store -ve element in temp array
for (int i = 0; i < n; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
// Copy contents of temp[] to arr[]
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
// Driver code
public static void Main()
{
int[] arr = { 1, -1, -3, -2, 7, 5, 11, 6 };
int n = arr.Length;
segregateElements(arr, n);
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This Code is contributed by vt_m.
PHP
= 0 )
$temp[$j++] = $arr[$i];
// If array contains all positive
// or all negative.
if ($j == $n || $j == 0)
return;
// Store -ve element in temp array
for ($i = 0 ; $i < $n ; $i++)
if ($arr[$i] < 0)
$temp[$j++] = $arr[$i];
// Copy contents of temp[] to arr[]
for($i = 0; $i < $n; $i++)
$arr[$i] = $temp[$i];
}
// Driver Code
$arr = array(1 ,-1 ,-3 , -2, 7, 5, 11, 6 );
$n = sizeof($arr);
segregateElements($arr, $n);
for ($i = 0; $i < $n; $i++)
echo $arr[$i] ." ";
// This code is contributed
// by ChitraNayal
?>
Javascript
输出:
1 7 5 11 6 -1 -3 -2