Rearrange positive and negative numbers in O(n) time and O(1) extra space | GeeksforGeeks
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
X1. The solution is to first separate positive and negative numbers using partition process of QuickSort. In the partition process, consider 0 as value of pivot element so that all negative numbers are placed before positive numbers. Once negative and positive numbers are separated, we start from the first negative number and first positive number, and swap every alternate negative number with next positive number.
http://algorithms.tutorialhorizon.com/rearrange-positive-and-negative-elements-at-alternate-positions-in-an-array-in-o1-extra-space/
//no need to do this
http://www.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/
DIfferent partition code: less swap.
http://www.geeksforgeeks.org/rearrange-array-alternating-positive-negative-items-o1-extra-space/
process array from left to right. While processing, find the first out of place element in the remaining unprocessed array. An element is out of place if it is negative and at odd index, or it is positive and at even index. Once we find an out of place element, we find the first element after it with opposite sign. We right rotate the subarray between these two elements (including these two).
TODO: https://ideone.com/VgIvYZ
http://comproguide.blogspot.com/2014/05/separating-positive-and-negative.html
Performance is not best, but concise.
http://blog.csdn.net/nicaishibiantai/article/details/43448341
Method-1: Using shift and Swap:
The good thing about above code is that it maintains the order in which element are present in the original array. But it is an O(n2) time algorithm.
http://www.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/
Also check: Rearrange Positive and Negative Numbers of Array On Each Side in O(nlogn) | Algorithms
Objective: Rearrange Positive and Negative Numbers of an Array so that one side you have positive numbers and other side with negative Integers without changing their respective order.
Read full article from Rearrange positive and negative numbers in O(n) time and O(1) extra space | GeeksforGeeks
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
For example, if the input array is [-1, 2, -3, 4, 5, 6, -7, 8, 9], then the output should be [9, -7, 8, -3, 5, -1, 2, 4, 6]
static
void
rearrange(
int
arr[],
int
n)
{
// The following few lines are similar to partition
// process of QuickSort. The idea is to consider 0
// as pivot and divide the array around it.
int
i = -
1
, temp =
0
;
for
(
int
j =
0
; j < n; j++)
{
if
(arr[j] <
0
)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Now all positive numbers are at end and negative numbers at
// the beginning of array. Initialize indexes for starting point
// of positive and negative numbers to be swapped
int
pos = i+
1
, neg =
0
;
// Increment the negative index by 2 and positive index by 1, i.e.,
// swap every alternate negative number with next positive number
while
(pos < n && neg < pos && arr[neg] <
0
)
{
temp = arr[neg];
arr[neg] = arr[pos];
arr[pos] = temp;
pos++;
neg +=
2
;
}
}
//no need to do this
int
high =
0
;
while
(arrA[high] <
0
)
high++;
DIfferent partition code: less swap.
void
arrange(
int
*arr,
int
n)
{
// Partition logic of QuickSort.
// Bring all -ve number at start.
int
low = 0, high = n-1;
while
(low<high)
{
while
(arr[low] < 0) low++;
while
(arr[high] > 0) high--;
if
(low<high)
swap(arr, low, high);
}
// At this point low will be pointint to the first positive number in the array.
// If there are no jpositive numbers in array, then low will be out of bond
if
(low>=n)
return
;
// First indexes of positive and negative numbers will be as below
int
positive = low, negative = 0;
// swap every alternate negative number with next positive number
while
(positive < n && negative < positive && arr[negative] < 0)
{
swap(arr, positive, negative);
positive++;
negative += 2;
}
}
Given an array of positive and negative numbers, arrange them in an alternate fashion such that every positive number is followed by negative and vice-versa maintaining the order of appearance.
Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
Example:
Input: arr[] = {1, 2, 3, -4, -1, 4} Output: arr[] = {-4, 1, -1, 2, 3, 4} Input: arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8} output: arr[] = {-5, 5, -2, 2, -8, 4, 7, 1, 8, 0}
// Utility function to right rotate all elements between [outofplace, cur]
void
rightrotate(
int
arr[],
int
n,
int
outofplace,
int
cur)
{
char
tmp = arr[cur];
for
(
int
i = cur; i > outofplace; i--)
arr[i] = arr[i-1];
arr[outofplace] = tmp;
}
void
rearrange(
int
arr[],
int
n)
{
int
outofplace = -1;
for
(
int
index = 0; index < n; index ++)
{
if
(outofplace >= 0)
{
// find the item which must be moved into the out-of-place
// entry if out-of-place entry is positive and current
// entry is negative OR if out-of-place entry is negative
// and current entry is negative then right rotate
//
// [...-3, -4, -5, 6...] --> [...6, -3, -4, -5...]
// ^ ^
// | |
// outofplace --> outofplace
//
if
(((arr[index] >= 0) && (arr[outofplace] < 0))
|| ((arr[index] < 0) && (arr[outofplace] >= 0)))
{
rightrotate(arr, n, outofplace, index);
// the new out-of-place entry is now 2 steps ahead
if
(index - outofplace > 2)
outofplace = outofplace + 2;
else
outofplace = -1;
}
}
// if no entry has been flagged out-of-place
if
(outofplace == -1)
{
// check if current entry is out-of-place
if
(((arr[index] >= 0) && (!(index & 0x01)))
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}
}
}
}
http://comproguide.blogspot.com/2014/05/separating-positive-and-negative.html
Performance is not best, but concise.
http://www.cnblogs.com/yuzhangcmu/p/4175620.htmlvoid seggregate(int arr[], int len){if( len <= 1 )return;int nIndex = 0;int i;for( i = 0; i < len; i++ ){if( arr[i] <= 0 ){swap(arr[i],arr[nIndex]);nIndex++;}}}
http://blog.csdn.net/nicaishibiantai/article/details/43448341
Method-1: Using shift and Swap:
The good thing about above code is that it maintains the order in which element are present in the original array. But it is an O(n2) time algorithm.
http://www.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/
Also check: Rearrange Positive and Negative Numbers of Array On Each Side in O(nlogn) | Algorithms
Objective: Rearrange Positive and Negative Numbers of an Array so that one side you have positive numbers and other side with negative Integers without changing their respective order.
Read full article from Rearrange positive and negative numbers in O(n) time and O(1) extra space | GeeksforGeeks