Sort an array containing two types of elements
Last Updated :
16 Nov, 2024
This problem appears in multiple forms as mentioned below. If we take a closer look at the below problems, we can notice that the problem is mainly a variation of partition algorithm in QuickSort.
Sort a Binary Array
Given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array.
Examples:
Input : arr[] = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
Output : arr[] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
Input : arr[] = [1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
Output : arr[] = [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
Please refer Segregate 0s and 1s in an array for detailed solution.
Move all zeros to end of array
Given an array of integers arr[], the task is to move all the zeros to the end of the array while maintaining the relative order of all non-zero elements.
Examples:
Input: arr[] = [1, 2, 0, 4, 3, 0, 5, 0]
Output: arr[] = [1, 2, 4, 3, 5, 0, 0, 0]
Explanation: There are three 0s that are moved to the end.
Input: arr[] = [10, 20, 30]
Output: arr[] = [10, 20, 30]
Explanation: No change in array as there are no 0s.
Input: arr[] = [0, 0]
Output: arr[] = [0, 0]
Explanation: No change in array as there are all 0s.
Refer here Move all zeros to end of array for detailed solution.
Separate Even and Odd Numbers
Given an array of integers. Segregate even numbers on left side and odd numbers on right side of the array.
Examples:
Input: arr = [0, 1, 2, 3, 4]
Output: arr = [0, 2, 4, 1, 3]
Explanation: [0, 2, 4] are even and [1, 3] are odd numbers. Please note that [2, 0, 4, 3, 1] or [4, 2, 0, 1, 3] are also valid outputs. We only need to make sure that all even elements are before all odd.
Input : arr ={1, 5, 11}
Output : arr= {1, 5, 11}
Explanation All numbers are odd.
Please refer Segregate Even and Odd Numbers and Stable Binary Sort for reference,
Separate Negative and Positive Numbers
Given an array of integers, the task is to arrange the elements such that all negative integers appear before all the positive integers in the array.
Examples:
Input: arr[] = [12, 11, -13, -5, 6, -7, 5, -3, -6]
Output: [-13, -5, -7, -3, -6, 12, 11, 6, 5]
Explanation: All negative elements [-13, -5, -7, -3, -6] were arranged before positive numbers [12, 11, 6, 5]. Note that there can be more than one possible output.
Input: arr[] = [11, -13, 6, -7, 5]
Output: [-13, -7, 11, 6, 5]
Explanation: All negative elements [-13, -7] were arranged before positive numbers [11, 6, 5].
We have two type of approaches to solve this problem:
Similar Reads
Sort an array having 3 types of Values We are given an array containing three different types of elements, and the task is to sort the array. This problem has been asked in various forms, and in this article, we will explore the three most common forms of this problem.Table of ContentSort an array of 0s, 1s and 2sThree way partitioning a
4 min read
Sort an Array of Version Numbers Given an array of strings arr[], consisting of N strings each representing dot separated numbers in the form of software versions. Input: arr[] = {"1.1.2", "0.9.1", "1.1.0"} Output: "0.9.1" "1.1.0" "1.1.2" Input: arr[] = {"1.2", "0.8.1", "1.0"} Output: "0.8.1" "1.0" "1.2" Approach: Follow the steps
6 min read
C Program to Sort an array of names or strings Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string.
2 min read
How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE
3 min read
How to sort an array of dates in C/C++? Given an array of dates, how to sort them. Example: Input: Date arr[] = {{20, 1, 2014}, {25, 3, 2010}, { 3, 12, 1676}, {18, 11, 1982}, {19, 4, 2015}, { 9, 7, 2015}} Output: Date arr[] = {{ 3, 12, 1676}, {18, 11, 1982}, {25, 3, 2010}, {20, 1, 2014}, {19, 4, 2015}, { 9, 7, 2015}} We strongly recommend
3 min read