Sort an array of 0s 1s and 2s

Given an array which include 3 types of integer numbers (0,1,2). Our goal is to sort this array in ascending order. For example.

Example A
// Befor sort
arr[] = [0, 1, 0, 2, 2, 1, 2,0, 0, 2, 0, 1, 1, 0 ,2]

// After sort
arr[] = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]

Example B
// Befor sort
arr[] = [1,2,0,0,1]
// After sort
arr[] = [0, 0, 1, 1, 2]
/*
  Java Program
  Sort an array of 0s 1s and 2s 
*/
public class Segregation
{
    // Function which is display array elements
    public void display(int[] arr, int size)
    {
        // iterating the array elements
        for (int i = 0; i < size; ++i)
        {
            System.out.print("  " + arr[i]);
        }
        System.out.print("\n");
    }
    // Sort the (0s,1s,2s) array
    public void sortElement(int[] arr, int size)
    {
        // Just before the modifying array elements
        System.out.print("Before Sort  \n");
        // Display given array
        this.display(arr, size);
        // Auxiliary variables
        int zero = 0;
        int one = 0;
        int two = 0;
        // iterating the array elements
        // And count zero, one and two
        for (int i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                zero++;
            }
            else if (arr[i] == 1)
            {
                one++;
            }
            else if (arr[i] == 2)
            {
                two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        int j = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero--;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one--;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two--;
            }
            // Change index
            j++;
        }
        // After modify array elements
        System.out.print("After Sort \n");
        this.display(arr, size);
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        Segregation task = new Segregation();
        // Define arrays of integer elements
        int[] arr1 = {
            0 , 1 , 0 , 2 , 2 , 1 , 2 , 0 , 0 , 2 , 0 , 1 , 1 , 0 , 2
        };
        int[] arr2 = {
            1 , 2 , 1 , 0 , 2 , 1
        };
        // Get the number of elements in arr1
        int size = arr1.length;
        // Check A
        task.sortElement(arr1, size);
        // Get the number of elements in arr2
        size = arr2.length;
        // Check B
        task.sortElement(arr2, size);
    }
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
/*
  C Program 
  Sort an array of 0s 1s and 2s 
*/
#include <stdio.h>

// Function which is display array elements
void display(int arr[], int size)
{
    // iterating the array elements
    for (int i = 0; i < size; ++i)
      {
        printf("%d  ", arr[i]);
      }
      printf("\n");
}
// Sort the (0s,1s,2s) array
    void sortElement(int arr[], int size)
    {
        // Just before the modifying array elements
        printf("Before Sort  \n");
        // Display given array
        display(arr, size);
        // Auxiliary variables
        int zero = 0;
        int one = 0;
        int two = 0;
        // iterating the array elements
        // And count zero, one and two
        for (int i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                zero++;
            }
            else if (arr[i] == 1)
            {
                one++;
            }
            else if (arr[i] == 2)
            {
                two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        int j = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero--;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one--;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two--;
            }
            // Change index
            j++;
        }
        // After modify array elements
        printf("After Sort \n");
        display(arr, size);
        printf("\n");
    }
int main()
{
    // Define arrays of integer elements
    int arr1[] = {
        0 , 1 , 0 , 2 , 2 , 1 , 2 , 0 , 0 , 2 , 0 , 1 , 1 , 0 , 2
    };
    int arr2[] = {
        1 , 2 , 1 , 0 , 2 , 1
    };
    // Get the number of elements in arr1
    int size = sizeof(arr1) / sizeof(arr1[0]);
    // Check A
    sortElement(arr1, size);
    // Get the number of elements in arr2
    size = sizeof(arr2) / sizeof(arr2[0]);
    // Check B
    sortElement(arr2, size); 
}

Output

Before Sort
0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
1  2  1  0  2  1
After Sort
0  1  1  1  2  2
package main
import "fmt"
/*
  Go Program
  Sort an array of 0s 1s and 2s 
*/
type Segregation struct {}
func getSegregation() * Segregation {
    var me *Segregation = &Segregation {}
    return me
}
// Function which is display array elements
func(this Segregation) display(arr[] int, size int) {
    // iterating the array elements
    for i := 0 ; i < size ; i++ {
        fmt.Print("  ", arr[i])
    }
    fmt.Print("\n")
}
// Sort the (0s,1s,2s) array
func(this Segregation) sortElement(arr[] int, size int) {
    // Just before the modifying array elements
    fmt.Print("Before Sort  \n")
    // Display given array
    this.display(arr, size)
    // Auxiliary variables
    var zero int = 0
    var one int = 0
    var two int = 0
    // iterating the array elements
    // And count zero, one and two
    for i := 0 ; i < size ; i++ {
        if arr[i] == 0 {
            zero++
        } else if arr[i] == 1 {
            one++
        } else if arr[i] == 2 {
            two++
        } else {
            // When array contains something else
            return
        }
    }
    var j int = 0
    // Put 0,1,2
    for (j < size) {
        if zero > 0 {
            // Put zero
            arr[j] = 0
            zero--
        } else if one > 0 {
            // Put one
            arr[j] = 1
            one--
        } else {
            // Put two
            arr[j] = 2
            two--
        }
        // Change index
        j++
    }
    // After modify array elements
    fmt.Print("After Sort \n")
    this.display(arr, size)
    fmt.Print("\n")
}
func main() {
    var task * Segregation = getSegregation()
    // Define arrays of integer elements
    var arr1 = [] int {0, 1, 0, 2, 2, 1, 2,0, 0, 2, 0, 1, 1, 0 ,2}
    var arr2 = [] int {1, 2, 1, 0,2,1}
    // Get the number of elements in arr1
    var size int = len(arr1)
    // Check A
    task.sortElement(arr1, size)
    // Get the number of elements in arr2
    size = len(arr2)
    // Check B
    task.sortElement(arr2, size)
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation
{
    public:
        // Function which is display array elements
        void display(int arr[], int size)
        {
            // iterating the array elements
            for (int i = 0; i < size; ++i)
            {
                cout << "  " << arr[i];
            }
            cout << "\n";
        }
    // Sort the (0s,1s,2s) array
    void sortElement(int arr[], int size)
    {
        // Just before the modifying array elements
        cout << "Before Sort  \n";
        // Display given array
        this->display(arr, size);
        // Auxiliary variables
        int zero = 0;
        int one = 0;
        int two = 0;
        // iterating the array elements
        // And count zero, one and two
        for (int i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                zero++;
            }
            else if (arr[i] == 1)
            {
                one++;
            }
            else if (arr[i] == 2)
            {
                two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        int j = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero--;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one--;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two--;
            }
            // Change index
            j++;
        }
        // After modify array elements
        cout << "After Sort \n";
        this->display(arr, size);
        cout << "\n";
    }
};
int main()
{
    Segregation *task = new Segregation();
    // Define arrays of integer elements
    int arr1[] = {
        0 , 1 , 0 , 2 , 2 , 1 , 2 , 0 , 0 , 2 , 0 , 1 , 1 , 0 , 2
    };
    int arr2[] = {
        1 , 2 , 1 , 0 , 2 , 1
    };
    // Get the number of elements in arr1
    int size = sizeof(arr1) / sizeof(arr1[0]);
    // Check A
    task->sortElement(arr1, size);
    // Get the number of elements in arr2
    size = sizeof(arr2) / sizeof(arr2[0]);
    // Check B
    task->sortElement(arr2, size);
    return 0;
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
// Include namespace system
using System;
/*
  Csharp Program
  Sort an array of 0s 1s and 2s 
*/
public class Segregation
{
    // Function which is display array elements
    public void display(int[] arr, int size)
    {
        // iterating the array elements
        for (int i = 0; i < size; ++i)
        {
            Console.Write("  " + arr[i]);
        }
        Console.Write("\n");
    }
    // Sort the (0s,1s,2s) array
    public void sortElement(int[] arr, int size)
    {
        // Just before the modifying array elements
        Console.Write("Before Sort  \n");
        // Display given array
        this.display(arr, size);
        // Auxiliary variables
        int zero = 0;
        int one = 0;
        int two = 0;
        // iterating the array elements
        // And count zero, one and two
        for (int i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                zero++;
            }
            else if (arr[i] == 1)
            {
                one++;
            }
            else if (arr[i] == 2)
            {
                two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        int j = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero--;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one--;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two--;
            }
            // Change index
            j++;
        }
        // After modify array elements
        Console.Write("After Sort \n");
        this.display(arr, size);
        Console.Write("\n");
    }
    public static void Main(String[] args)
    {
        Segregation task = new Segregation();
        // Define arrays of integer elements
        int[] arr1 = {
            0 , 1 , 0 , 2 , 2 , 1 , 2 , 0 , 0 , 2 , 0 , 1 , 1 , 0 , 2
        };
        int[] arr2 = {
            1 , 2 , 1 , 0 , 2 , 1
        };
        // Get the number of elements in arr1
        int size = arr1.Length;
        // Check A
        task.sortElement(arr1, size);
        // Get the number of elements in arr2
        size = arr2.Length;
        // Check B
        task.sortElement(arr2, size);
    }
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
<?php
/*
  Php Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation
{
    // Function which is display array elements
    public  function display($arr, $size)
    {
        // iterating the array elements
        for ($i = 0; $i < $size; ++$i)
        {
            echo("  ".$arr[$i]);
        }
        echo("\n");
    }
    // Sort the (0s,1s,2s) array
    public  function sortElement(&$arr, $size)
    {
        // Just before the modifying array elements
        echo("Before Sort  \n");
        // Display given array
        $this->display($arr, $size);
        // Auxiliary variables
        $zero = 0;
        $one = 0;
        $two = 0;
        // iterating the array elements
        // And count zero, one and two
        for ($i = 0; $i < $size; ++$i)
        {
            if ($arr[$i] == 0)
            {
                $zero++;
            }
            else if ($arr[$i] == 1)
            {
                $one++;
            }
            else if ($arr[$i] == 2)
            {
                $two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        $j = 0;
        // Put 0,1,2
        while ($j < $size)
        {
            if ($zero > 0)
            {
                // Put zero
                $arr[$j] = 0;
                $zero--;
            }
            else if ($one > 0)
            {
                // Put one
                $arr[$j] = 1;
                $one--;
            }
            else
            {
                // Put two
                $arr[$j] = 2;
                $two--;
            }
            // Change index
            $j++;
        }
        // After modify array elements
        echo("After Sort \n");
        $this->display($arr, $size);
        echo("\n");
    }
}

function main()
{
    $task = new Segregation();
    // Define arrays of integer elements
    $arr1 = array(0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2);
    $arr2 = array(1, 2, 1, 0, 2, 1);
    // Get the number of elements in arr1
    $size = count($arr1);
    // Check A
    $task->sortElement($arr1, $size);
    // Get the number of elements in arr2
    $size = count($arr2);
    // Check B
    $task->sortElement($arr2, $size);
}
main();

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
/*
  Node JS Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation
{
    // Function which is display array elements
    display(arr, size)
    {
        // iterating the array elements
        for (var i = 0; i < size; ++i)
        {
            process.stdout.write("  " + arr[i]);
        }
        process.stdout.write("\n");
    }
    // Sort the (0s,1s,2s) array
    sortElement(arr, size)
    {
        // Just before the modifying array elements
        process.stdout.write("Before Sort  \n");
        // Display given array
        this.display(arr, size);
        // Auxiliary variables
        var zero = 0;
        var one = 0;
        var two = 0;
        // iterating the array elements
        // And count zero, one and two
        for (var i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                zero++;
            }
            else if (arr[i] == 1)
            {
                one++;
            }
            else if (arr[i] == 2)
            {
                two++;
            }
            else
            {
                // When array contains something else
                return;
            }
        }
        var j = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero--;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one--;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two--;
            }
            // Change index
            j++;
        }
        // After modify array elements
        process.stdout.write("After Sort \n");
        this.display(arr, size);
        process.stdout.write("\n");
    }
}

function main()
{
    var task = new Segregation();
    // Define arrays of integer elements
    var arr1 = [0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2];
    var arr2 = [1, 2, 1, 0, 2, 1];
    // Get the number of elements in arr1
    var size = arr1.length;
    // Check A
    task.sortElement(arr1, size);
    // Get the number of elements in arr2
    size = arr2.length;
    // Check B
    task.sortElement(arr2, size);
}
main();

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
#  Python 3 Program
#  Sort an array of 0s 1s and 2s 
class Segregation :
    #  Function which is display list elements
    def display(self, arr, size) :
        i = 0
        #  iterating the list elements
        while (i < size) :
            print("  ", arr[i], end = "")
            i += 1
        
        print(end = "\n")
    
    #  Sort the (0s,1s,2s) list
    def sortElement(self, arr, size) :
        #  Just before the modifying list elements
        print("Before Sort  ")
        #  Display given list
        self.display(arr, size)
        #  Auxiliary variables
        zero = 0
        one = 0
        two = 0
        i = 0
        #  iterating the list elements
        #  And count zero, one and two
        while (i < size) :
            if (arr[i] == 0) :
                zero += 1
            elif (arr[i] == 1) :
                one += 1
            elif (arr[i] == 2) :
                two += 1
            else :
                #  When list contains somethingelse
                return
            
            i += 1
        
        j = 0
        #  Put 0,1,2
        while (j < size) :
            if (zero > 0) :
                #  Put zero
                arr[j] = 0
                zero -= 1
            elif (one > 0) :
                #  Put one
                arr[j] = 1
                one -= 1
            else :
                #  Put two
                arr[j] = 2
                two -= 1
            
            #  Change index
            j += 1
        
        #  After modify list elements
        print("After Sort ")
        self.display(arr, size)
        print(end = "\n")
    

def main() :
    task = Segregation()
    #  Define lists of integer elements
    arr1 = [0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2]
    arr2 = [1, 2, 1, 0, 2, 1]
    #  Get the number of elements in arr1
    size = len(arr1)
    #  Check A
    task.sortElement(arr1, size)
    #  Get the number of elements in arr2
    size = len(arr2)
    #  Check B
    task.sortElement(arr2, size)

if __name__ == "__main__": main()

Output

Before Sort
   0   1   0   2   2   1   2   0   0   2   0   1   1   0   2
After Sort
   0   0   0   0   0   0   1   1   1   1   2   2   2   2   2

Before Sort
   1   2   1   0   2   1
After Sort
   0   1   1   1   2   2
#  Ruby Program
#  Sort an array of 0s 1s and 2s 
class Segregation 
    #  Function which is display array elements
    def display(arr, size) 
        i = 0
        #  iterating the array elements
        while (i < size) 
            print("  ", arr[i])
            i += 1
        end

        print("\n")
    end

    #  Sort the (0s,1s,2s) array
    def sortElement(arr, size) 
        #  Just before the modifying array elements
        print("Before Sort  \n")
        #  Display given array
        self.display(arr, size)
        #  Auxiliary variables
        zero = 0
        one = 0
        two = 0
        i = 0
        #  iterating the array elements
        #  And count zero, one and two
        while (i < size) 
            if (arr[i] == 0) 
                zero += 1
            elsif (arr[i] == 1) 
                one += 1
            elsif (arr[i] == 2) 
                two += 1
            else
                #  When array contains something else
                return
            end

            i += 1
        end

        j = 0
        #  Put 0,1,2
        while (j < size) 
            if (zero > 0) 
                #  Put zero
                arr[j] = 0
                zero -= 1
            elsif (one > 0) 
                #  Put one
                arr[j] = 1
                one -= 1
            else
                #  Put two
                arr[j] = 2
                two -= 1
            end

            #  Change index
            j += 1
        end

        #  After modify array elements
        print("After Sort \n")
        self.display(arr, size)
        print("\n")
    end

end

def main() 
    task = Segregation.new()
    #  Define arrays of integer elements
    arr1 = [0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2]
    arr2 = [1, 2, 1, 0, 2, 1]
    #  Get the number of elements in arr1
    size = arr1.length
    #  Check A
    task.sortElement(arr1, size)
    #  Get the number of elements in arr2
    size = arr2.length
    #  Check B
    task.sortElement(arr2, size)
end

main()

Output

Before Sort  
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort 
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort  
  1  2  1  0  2  1
After Sort 
  0  1  1  1  2  2

/*
  Scala Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation()
{
    // Function which is display array elements
    def display(arr: Array[Int], size: Int): Unit = {
        var i: Int = 0;
        // iterating the array elements
        while (i < size)
        {
            print("  " + arr(i));
            i += 1;
        }
        print("\n");
    }
    // Sort the (0s,1s,2s) array
    def sortElement(arr: Array[Int], size: Int): Unit = {
        // Just before the modifying array elements
        print("Before Sort  \n");
        // Display given array
        this.display(arr, size);
        // Auxiliary variables
        var zero: Int = 0;
        var one: Int = 0;
        var two: Int = 0;
        var i: Int = 0;
        // iterating the array elements
        // And count zero, one and two
        while (i < size)
        {
            if (arr(i) == 0)
            {
                zero += 1;
            }
            else if (arr(i) == 1)
            {
                one += 1;
            }
            else if (arr(i) == 2)
            {
                two += 1;
            }
            else
            {
                // When array contains something else
                return;
            }
            i += 1;
        }
        var j: Int = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr(j) = 0;
                zero -= 1;
            }
            else if (one > 0)
            {
                // Put one
                arr(j) = 1;
                one -= 1;
            }
            else
            {
                // Put two
                arr(j) = 2;
                two -= 1;
            }
            // Change index
            j += 1;
        }
        // After modify array elements
        print("After Sort \n");
        this.display(arr, size);
        print("\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Segregation = new Segregation();
        // Define arrays of integer elements
        var arr1: Array[Int] = Array(
          0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2
        );
        var arr2: Array[Int] = Array(
          1, 2, 1, 0, 2, 1
        );
        // Get the number of elements in arr1
        var size: Int = arr1.length;
        // Check A
        task.sortElement(arr1, size);
        // Get the number of elements in arr2
        size = arr2.length;
        // Check B
        task.sortElement(arr2, size);
    }
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2
import Foundation;
/*
  Swift 4 Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation
{
    // Function which is display array elements
    func display(_ arr: [Int], _ size: Int)
    {
        var i: Int = 0;
        // iterating the array elements
        while (i < size)
        {
            print("  ", arr[i], terminator: "");
            i += 1;
        }
        print(terminator: "\n");
    }
    // Sort the (0s,1s,2s) array
    func sortElement(_ arr: inout[Int], _ size: Int)
    {
        // Just before the modifying array elements
        print("Before Sort  ");
        // Display given array
        self.display(arr, size);
        // Auxiliary variables
        var zero: Int = 0;
        var one: Int = 0;
        var two: Int = 0;
        var i: Int = 0;
        // iterating the array elements
        // And count zero, one and two
        while (i < size)
        {
            if (arr[i] == 0)
            {
                zero += 1;
            }
            else if (arr[i] == 1)
            {
                one += 1;
            }
            else if (arr[i] == 2)
            {
                two += 1;
            }
            else
            {
                // When array contains something else
                return;
            }
            i += 1;
        }
        var j: Int = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero -= 1;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one -= 1;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two -= 1;
            }
            // Change index
            j += 1;
        }
        // After modify array elements
        print("After Sort ");
        self.display(arr, size);
        print(terminator: "\n");
    }
}
func main()
{
    let task: Segregation = Segregation();
    // Define arrays of integer elements
    var arr1: [Int] = [0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2];
    var arr2: [Int] = [1, 2, 1, 0, 2, 1];
    // Get the number of elements in arr1
    var size: Int = arr1.count;
    // Check A
    task.sortElement(&arr1, size);
    // Get the number of elements in arr2
    size = arr2.count;
    // Check B
    task.sortElement(&arr2, size);
}
main();

Output

Before Sort
   0   1   0   2   2   1   2   0   0   2   0   1   1   0   2
After Sort
   0   0   0   0   0   0   1   1   1   1   2   2   2   2   2

Before Sort
   1   2   1   0   2   1
After Sort
   0   1   1   1   2   2
/*
  Kotlin Program
  Sort an array of 0s 1s and 2s 
*/
class Segregation
{
    // Function which is display array elements
    fun display(arr: Array < Int > , size: Int): Unit
    {
        var i: Int = 0;
        // iterating the array elements
        while (i < size)
        {
            print("  " + arr[i]);
            i += 1;
        }
        print("\n");
    }
    // Sort the (0s,1s,2s) array
    fun sortElement(arr: Array < Int > , size: Int): Unit
    {
        // Just before the modifying array elements
        print("Before Sort  \n");
        // Display given array
        this.display(arr, size);
        // Auxiliary variables
        var zero: Int = 0;
        var one: Int = 0;
        var two: Int = 0;
        var i: Int = 0;
        // iterating the array elements
        // And count zero, one and two
        while (i < size)
        {
            if (arr[i] == 0)
            {
                zero += 1;
            }
            else if (arr[i] == 1)
            {
                one += 1;
            }
            else if (arr[i] == 2)
            {
                two += 1;
            }
            else
            {
                // When array contains something else
                return;
            }
            i += 1;
        }
        var j: Int = 0;
        // Put 0,1,2
        while (j < size)
        {
            if (zero > 0)
            {
                // Put zero
                arr[j] = 0;
                zero -= 1;
            }
            else if (one > 0)
            {
                // Put one
                arr[j] = 1;
                one -= 1;
            }
            else
            {
                // Put two
                arr[j] = 2;
                two -= 1;
            }
            // Change index
            j += 1;
        }
        // After modify array elements
        print("After Sort \n");
        this.display(arr, size);
        print("\n");
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Segregation = Segregation();
    // Define arrays of integer elements
    val arr1: Array < Int > = arrayOf(0, 1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 1, 1, 0, 2);
    val arr2: Array < Int > = arrayOf(1, 2, 1, 0, 2, 1);
    // Get the number of elements in arr1
    var size: Int = arr1.count();
    // Check A
    task.sortElement(arr1, size);
    // Get the number of elements in arr2
    size = arr2.count();
    // Check B
    task.sortElement(arr2, size);
}

Output

Before Sort
  0  1  0  2  2  1  2  0  0  2  0  1  1  0  2
After Sort
  0  0  0  0  0  0  1  1  1  1  2  2  2  2  2

Before Sort
  1  2  1  0  2  1
After Sort
  0  1  1  1  2  2


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved