Given an array arr[]
of integers, the task is to partition the array based on even and odd elements. The partition has to be stable, meaning the relative ordering of all even elements must remain the same before and after partitioning, and the same should hold true for all odd elements.
Note: For a binary array (containing only 0s and 1s), this partition process is equivalent to sorting the array.
Examples:
Input: arr[] = [1, 0, 1, 1, 0, 0]
Output: [0 ,0 ,0, 1, 1, 1]
Explanation: All even numbers came before the odd numbers.
Input: arr[] = [1, 2, 3, 4, 5]
Output: [2, 4, 1, 3, 5]
Explanation: All even numbers [2, 4] came before the odd numbers [1, 3, 5], and the relative ordering was also same.
Input: arr[] = [-5, -2, 0, 4, 7, 9]
Output: [-2, 0, 4, -5, 7, 9]
Explanation: All even numbers [-2, 0, 4] came before the odd numbers [-5, 7, 9], and the relative ordering was also same.
The idea is to to use Naive Partitioning Algorithm, We use a temporary array to store the rearranged elements. We first iterate over the original array and add all the even elements to the temporary array, maintaining their original order. Next, we add all the odd elements to the temporary array in the same manner.
This guarantees that even elements appear before odd elements while preserving the relative order of similar elements. Finally, we copy the elements from the temporary array back into the original array.
C++ // C++ program to partition the array into even // and odd elements using naive partition approach #include <iostream> #include <vector> using namespace std; // Function for stable partitioning of even-odd(binary) array void partition(vector<int> &arr) { int n = arr.size(); // create a temporary array to store the elements vector<int> temp(n); int idx = 0; // First fill even elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) temp[idx++] = arr[i]; } // Now fill odd elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] & 1) temp[idx++] = arr[i]; } // copy the elements from temp to arr arr = temp; } int main() { vector<int> arr = {-5, -2, 0, 4, 7, 9}; partition(arr); for (int i = 0; i < arr.size(); i++) cout << arr[i] << " "; return 0; }
C // C program to partition the array into even // and odd elements using naive partition approach #include <stdio.h> #include <stdlib.h> // Function for stable partitioning of even-odd(binary) array void partition(int arr[], int n) { // create a temporary array to store the elements int *temp = (int *)malloc(n * sizeof(int)); int idx = 0; // First fill even elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) temp[idx++] = arr[i]; } // Now fill odd elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] & 1) temp[idx++] = arr[i]; } // copy the elements from temp to arr for (int i = 0; i < n; i++) { arr[i] = temp[i]; } free(temp); } int main() { int arr[] = {-5, -2, 0, 4, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); partition(arr, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
Java // Java program to partition the array into even // and odd elements using naive partition approach import java.util.Arrays; class GfG { // Function for stable partitioning of even-odd(binary) array static void partition(int[] arr) { int n = arr.length; // create a temporary array to store the elements int[] temp = new int[n]; int idx = 0; // First fill even elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) temp[idx++] = arr[i]; } // Now fill odd elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 != 0) temp[idx++] = arr[i]; } // copy the elements from temp to arr System.arraycopy(temp, 0, arr, 0, n); } public static void main(String[] args) { int[] arr = {-5, -2, 0, 4, 7, 9}; partition(arr); for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); } }
Python # Python program to partition the array into even # and odd elements using naive partition approach # Function for stable partitioning of even-odd(binary) array def partition(arr): # create a temporary list to store the elements temp = [] # First fill even elements into the temporary list for num in arr: if num % 2 == 0: temp.append(num) # Now fill odd elements into the temporary list for num in arr: if num % 2 != 0: temp.append(num) # copy the elements from temp to arr for i in range(len(arr)): arr[i] = temp[i] if __name__ == "__main__": arr = [-5, -2, 0, 4, 7, 9] partition(arr) print(' '.join(map(str, arr)))
C# // C# program to partition the array into even // and odd elements using naive partition approach using System; class GfG { // Function for stable partitioning of even-odd(binary) array static void Partition(int[] arr) { int n = arr.Length; // create a temporary array to store the elements int[] temp = new int[n]; int idx = 0; // First fill even elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) temp[idx++] = arr[i]; } // Now fill odd elements into the temporary array for (int i = 0; i < n; i++) { if (arr[i] % 2 != 0) temp[idx++] = arr[i]; } // copy the elements from temp to arr Array.Copy(temp, arr, n); } static void Main() { int[] arr = {-5, -2, 0, 4, 7, 9}; Partition(arr); Console.WriteLine(string.Join(" ", arr)); } }
JavaScript // JavaScript program to partition the array into even // and odd elements using naive partition approach // Function for stable partitioning of even-odd(binary) array function partition(arr) { // create a temporary array to store the elements let temp = []; // First fill even elements into the temporary array for (let i = 0; i < arr.length; i++) { if (arr[i] % 2 === 0) temp.push(arr[i]); } // Now fill odd elements into the temporary array for (let i = 0; i < arr.length; i++) { if (arr[i] % 2 !== 0) temp.push(arr[i]); } // copy the elements from temp to arr for (let i = 0; i < arr.length; i++) { arr[i] = temp[i]; } } // Driver Code let arr = [-5, -2, 0, 4, 7, 9]; partition(arr); console.log(arr.join(' '));
Time Complexity: O(n), for traversing the array.
Auxiliary Space: O(n), for temporary array.
Similar Reads
Stable Selection Sort A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted output as they appear in the input array to be sorted.Any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison op
6 min read
Stable QuickSort A sorting algorithm is said to be stable if it maintains the relative order of records in the case of equality of keys.Input : (1, 5), (3, 2) (1, 2) (5, 4) (6, 4) We need to sort key-value pairs in the increasing order of keys of first digit There are two possible solution for the two pairs where th
9 min read
Unstable Sort meaning in DSA Unstable sorting algorithm can be defined as sorting algorithm in which the order of objects with the same values in the sorted array are not guaranteed to be the same as in the input array. Properties of Unstable Sorting Algorithm:They do not require the overhead of keeping the order of equal eleme
2 min read
Stable sort for descending order Given an array of n integers, we have to reverse sort the array elements such the equal keys are stable after sorting. Examples: Input : arr[] = {4, 2, 3, 2, 4} Output : 4, 4, 3, 2, 2 Prerequisite : Stability in sorting algorithmsMethod 1 (Writing our own sorting function : Bubble Sort)We know sorti
8 min read
std::stable_partition in C++ The stable_partition( ) algorithm arranges the sequence defined by start and end such that all elements for which the predicate specified by pfn returns true come before those for which the predicate returns false. The partitioning is stable. This means that the relative ordering of the sequence is
3 min read
Stooge Sort Stooge Sort is a recursive sorting algorithm. It is not much efficient but interesting sorting algorithm. It generally divides the array into two overlapping parts (2/3 each). After that it performs sorting in first 2/3 part and then it performs sorting in last 2/3 part. And then, sorting is done on
6 min read