Segregate 0’s, 1’s and 2’s in an array

Segregate 0’s, 1’s and 2’s in an array in C

Here, in this section we will discuss the C program for segregate 0’s, 1’s and 2’s in an array :

Given an array with 0’s, 1’s and 2’s, we are segregating the 0’s, 1’s and 2’s.

INPUT : 0 1 2 0 1 1 1 1 1 0 1 2 1 0 2 2 2 0 1 2 0 1

OUTPUT : 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2

Segregate 0’s and 1’s in an array

Segregate 0’s, 1's and 2’s in an array in C

Segregating 0’s, 1’s, and 2’s in an array is a problem that involves sorting an array of integers that contains only the values 0, 1, and 2. The goal is to rearrange the array such that all the 0’s are placed before all the 1’s, and all the 1’s are placed before all the 2’s. This problem is also known as the Dutch National Flag problem, as it was originally posed as a metaphor for the three bands of color in the Dutch flag.On this page we will discuss two methods-

  1. Brute Force Method
  2. Dutch National Flag Method
Segregate 0’s, 1's and 2’s in an array in C

Brute force approach to segregate 0’s, 1’s, and 2’s in an array is to count the number of 0’s, 1’s, and 2’s in the array, and then overwrite the array with the correct number of each value

Algorithm for brute force

Here’s how the algorithm works:

  1. Initialize three counters for 0’s, 1’s, and 2’s to 0.
  2. Loop through the array and count the number of 0’s, 1’s, and 2’s.
  3. Overwrite the array with the correct number of each value, starting with 0’s, then 1’s, then 2’s.

Code for brute force method in C

Run
#include <stdio.h>

void segregate (int arr[], int n)
{
  int count_0 = 0, count_1 = 0, count_2 = 0;

  // Step 1: Count the number of 0's, 1's, and 2's
  for (int i = 0; i < n; i++)
    {
      if (arr[i] == 0)
	{
	  count_0++;
	}
      else if (arr[i] == 1)
	{
	  count_1++;
	}
      else if (arr[i] == 2)
	{
	  count_2++;
	}
    }

  // Step 2: Overwrite the array with the correct number of each value
  int i = 0;
  while (count_0 > 0)
    {
      arr[i] = 0;
      count_0--;
      i++;
    }
  while (count_1 > 0)
    {
      arr[i] = 1;
      count_1--;
      i++;
    }
  while (count_2 > 0)
    {
      arr[i] = 2;
      count_2--;
      i++;
    }
}

int main ()
{
  int arr[] = { 2, 0, 1, 2, 0, 1, 0, 2, 1 };
  int n = sizeof (arr) / sizeof (arr[0]);

  printf ("Array: ");
  for (int i = 0; i < n; i++)
    {
      printf ("%d ", arr[i]);
    }
  printf ("\n");


  segregate (arr, n);

  printf ("Segregated array: ");
  for (int i = 0; i < n; i++)
    {
      printf ("%d ", arr[i]);
    }
  printf ("\n");

  return 0;
}

Output

Array: 2 0 1 2 0 1 0 2 1 
Segregated array: 0 0 0 1 1 1 2 2 2 

One approach to segregate 0’s, 1’s, and 2’s in an array is to use the Dutch National Flag algorithm. The basic idea of this algorithm is to maintain three pointers to partition the array into three parts: the 0’s, the 1’s, and the 2’s.

Algorithm for dutch national flag algorithm

Here’s how the algorithm works:

  1. Initialize three pointers: low, mid, and high.

    • low points to the beginning of the array
    • mid points to the beginning of the unsorted part of the array
    • high points to the end of the array
  2. Loop through the array from mid to high:

    • If the current element is 0, swap it with the element at the low pointer and increment both low and mid pointers.
    • If the current element is 1, leave it in place and just increment the mid pointer.
    • If the current element is 2, swap it with the element at the high pointer and decrement the high pointer.
  3. Continue this process until the mid pointer crosses over the high pointer.

Code for dutch national flag method in C

Run

#include <stdio.h>

void swap (int *a, int *b)
{
  int temp = *a;
  *a = *b;
  *b = temp;
}

void segregate (int arr[], int n)
{
  int low = 0, mid = 0, high = n - 1;
  while (mid <= high)
    {
      switch (arr[mid])
	{
	case 0:
	  swap (&arr[low], &arr[mid]);
	  low++;
	  mid++;
	  break;
	case 1:
	  mid++;
	  break;
	case 2:
	  swap (&arr[mid], &arr[high]);
	  high--;
	  break;
	}
    }
}

int main ()
{
  int arr[] = { 2, 0, 1, 2, 0, 1, 0, 2, 1 };
  int n = sizeof (arr) / sizeof (arr[0]);
  
  printf ("Array: ");
  for (int i = 0; i < n; i++)
    {
      printf ("%d ", arr[i]);
    }
  printf ("\n");
  

  segregate (arr, n);

  printf ("Segregated array: ");
  for (int i = 0; i < n; i++)
    {
      printf ("%d ", arr[i]);
    }
  printf ("\n");

  return 0;
}

Output

Array: 2 0 1 2 0 1 0 2 1 
Segregated array: 0 0 0 1 1 1 2 2 2 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java