📜  Java程序在数组的左侧隔离 0 和在右侧隔离 1

📅  最后修改于: 2022-05-13 01:55:50.764000             🧑  作者: Mango

Java程序在数组的左侧隔离 0 和在右侧隔离 1

您将获得一个随机顺序的 0 和 1 数组。分离数组左侧的 0 和右侧的 1。基本目标是遍历数组元素并在隔离 0 和 1 中排序。

插图:

方法:

  1. 通过计数使用隔离
  2. 使用数组排序
  3. 使用指针

下面详细讨论了所有三种方法:

方法一:

  • 计算 0 的数量。
  • 遍历整个数组以查找存在零的索引
  • 保持计数并在出现 0 时递增
  • 将所有零打印到前面
  • 剩余的 1 数将是 1-(0 的总数)
  • 打印剩余元素

下面是使用上述算法分离 0 和 1 的实现:

Java
// Java code to Segregate 0s and 1s in an array
 
// Importing generic libraries
import java.util.*;
// Importing Array libraries
import java.util.Arrays;
 
public class GFG {
 
    // Function to segregate 0s and 1s
    static void segregate0and1(int arr[], int n)
    {
        // Counts the no of zeros in array
        int count = 0;
 
        // Iteration over array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
 
                // Incrementing the count
                count++;
        }
 
        // Loop fills the arr with 0 until count
        for (int i = 0; i < count; i++)
            arr[i] = 0;
 
        // Loop fills remaining arr space with 1
        for (int i = count; i < n; i++)
            arr[i] = 1;
    }
 
    // Function to print segregated array
    static void print(int arr[], int n)
    {
        System.out.print("Array after segregation is ");
 
        // Iteration over array
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Main driver function
    public static void main(String[] args)
    {
        // Array taken for consideration
        int arr[] = new int[] { 0, 1, 0, 1, 1, 1 };
 
        // Using inbuilt function to store array size
        int n = arr.length;
 
        // Calling function that segregates array
        segregate0and1(arr, n);
 
        // Printing the above segregated array
        print(arr, n);
    }
}


Java
// Java code to Segregate 0s and 1s in an array
 
// Importing generic libraries
import java.util.*;
 
public class GFG {
 
    // Function to print segregated array
    // Taking arguments- array and array size
    static void print(int arr[], int n)
    {
        System.out.print("Array after segregation is ");
 
        // Iteration over array
        for (int i = 0; i < n; ++i)
 
            // Printing array elements
            System.out.print(arr[i] + " ");
    }
 
    // Main driver function
    public static void main(String[] args)
    {
        // Array taken for consideration
        int arr[] = new int[] { 0, 1, 0, 1, 1, 1 };
 
        // Using length inbuilt function to
        int n = arr.length;
 
        // Using sort inbuilt function
        Arrays.sort(arr);
 
        // Printing elements after executing sorting
        print(arr, n);
    }
}


Java
// Java code to Segregate 0s and 1s in an array
 
// Importing generic libraries
import java.util.*;
import java.io.*;
 
class GFG {
 
    // Print function outside main to print elements
    static void print(int a[])
    {
 
        System.out.print("Array after segregation is: ");
 
        // Iteration over array using array
        // class inbuilt function .length()
        for (int i = 0; i < a.length; ++i) {
 
            // Printing elements in array
            System.out.print(a[i] + " ");
        }
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // Random array taken for consideration
        int a[] = { 1, 1, 0, 0, 0, 0, 1, 1 };
 
        // Maintaining left pointer
        int left = 0;
 
        // Iteration over array using length function
        for (int i = 0; i < a.length; ++i) {
 
            // If zeros are present
            if (a[i] == 0) {
 
                // Swap the elements using
                // temporary variable
                int temp = a[left];
                a[left] = a[i];
                a[i] = temp;
 
                // Pre incrementing left pointer
                ++left;
            }
        }
 
        // Calling above function to
        // print updated array
        print(a);
    }
}


输出:

Array after segregation is 0 0 1 1 1 1 

时间复杂度:O(n)

方法二:使用 sort()函数

sort()一个方法,是一个Java.util.Arrays类的方法。

句法:

public static void sort(int[] arr, int from_Index, int to_Index)

参数:

arr        - the array to be sorted
from_Index - the index of the first element, inclusive, to be sorted
to_Index   - the index of the last element, exclusive, to be sorted

退货类型:

This method doesn't return any value

Java

// Java code to Segregate 0s and 1s in an array
 
// Importing generic libraries
import java.util.*;
 
public class GFG {
 
    // Function to print segregated array
    // Taking arguments- array and array size
    static void print(int arr[], int n)
    {
        System.out.print("Array after segregation is ");
 
        // Iteration over array
        for (int i = 0; i < n; ++i)
 
            // Printing array elements
            System.out.print(arr[i] + " ");
    }
 
    // Main driver function
    public static void main(String[] args)
    {
        // Array taken for consideration
        int arr[] = new int[] { 0, 1, 0, 1, 1, 1 };
 
        // Using length inbuilt function to
        int n = arr.length;
 
        // Using sort inbuilt function
        Arrays.sort(arr);
 
        // Printing elements after executing sorting
        print(arr, n);
    }
}

输出:

Array after segregation is 0 0 1 1 1 1 

时间复杂度 O(nlongn)

方法三:保持左指针,在数组中找到零时与左指针交换位置,并递增左指针。

Java

// Java code to Segregate 0s and 1s in an array
 
// Importing generic libraries
import java.util.*;
import java.io.*;
 
class GFG {
 
    // Print function outside main to print elements
    static void print(int a[])
    {
 
        System.out.print("Array after segregation is: ");
 
        // Iteration over array using array
        // class inbuilt function .length()
        for (int i = 0; i < a.length; ++i) {
 
            // Printing elements in array
            System.out.print(a[i] + " ");
        }
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // Random array taken for consideration
        int a[] = { 1, 1, 0, 0, 0, 0, 1, 1 };
 
        // Maintaining left pointer
        int left = 0;
 
        // Iteration over array using length function
        for (int i = 0; i < a.length; ++i) {
 
            // If zeros are present
            if (a[i] == 0) {
 
                // Swap the elements using
                // temporary variable
                int temp = a[left];
                a[left] = a[i];
                a[i] = temp;
 
                // Pre incrementing left pointer
                ++left;
            }
        }
 
        // Calling above function to
        // print updated array
        print(a);
    }
}

输出:

Array after segregation is: 0 0 0 0 1 1 1 1 

时间复杂度O(n)