📜  使用一次遍历对二进制数组进行排序

📅  最后修改于: 2021-05-06 03:30:21             🧑  作者: Mango

给定一个二进制数组,请使用一个遍历对其进行排序,而无需额外的空间。

例子 :

Input : 1 0 0 1 0 1 0 1 1 1 1 1 1 0 0 1 1 0 1 0 0 
Output : 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
Explanation: The output is a sorted array of 0 and 1

Input : 1 0 1 0 1 0 1 0 
Output : 0 0 0 0 1 1 1 1
Explanation: The output is a sorted array of 0 and 1

方法:这个概念与快速排序的分区有关。在“快速排序”分区中,一次扫描后,数组的左侧是最小的,数组的右侧是所选枢轴元素的最大。

算法:

  1. 创建一个变量索引index = 0
  2. 从头到尾遍历数组
  3. 如果元素为0,则将当前元素与位于索引位置的元素交换,并将索引增加1。
  4. 如果元素为1,则保持元素原样。

执行:

CPP
// CPP program to sort a binary array
#include 
using namespace std;
  
void sortBinaryArray(int a[], int n)
{
    int j = -1;
    for (int i = 0; i < n; i++) {
  
        // if number is smaller than 1
        // then swap it with j-th number
        if (a[i] < 1) {
            j++;
            swap(a[i], a[j]);
        }
    }
}
  
// Driver code
int main()
{
    int a[] = { 1, 0, 0, 1, 0, 1, 0, 1, 1, 1,
                1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0 };
    int n = sizeof(a) / sizeof(a[0]);
    sortBinaryArray(a, n);
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
  
    return 0;
}


Java
// JAVA Code for Sort a binary
// array using one traversal
import java.util.*;
  
class GFG {
  
    static void sortBinaryArray(int a[], int n)
    {
        int j = -1;
        for (int i = 0; i < n; i++) {
  
            // if number is smaller than 1
            // then swap it with j-th number
            if (a[i] < 1) {
                j++;
                int temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
  
    /* Driver program to test above function */
    public static void main(String[] args)
    {
  
        int a[] = { 1, 0, 0, 1, 0, 1, 0, 1, 1, 1,
                    1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0 };
  
        int n = a.length;
  
        sortBinaryArray(a, n);
  
        for (int i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
}


Python3
# A Python program to sort a
# binary array
def sortBinaryArray(a, n):
    j = -1
    for i in range(n):
  
        # if number is smaller
        # than 1 then swap it
        # with j-th number
        if a[i] < 1:
            j = j + 1
              
            # swap
            a[i], a[j] = a[j], a[i]
      
  
# Driver program
a = [1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1,
        1, 1, 0, 0, 1, 1, 0, 1, 0, 0]
n = len(a)
  
sortBinaryArray(a, n)
  
for i in range(n):
        print(a[i], end = " ")
  
# This code is contributed by Shrikant13.


C#
// C# Code for Sort a binary
// array using one traversal
using System;
  
class GFG {
  
    static void sortBinaryArray(int[] a,
                                  int n)
    {
        int j = -1;
        for (int i = 0; i < n; i++)
        {
  
            // if number is smaller than
            // 1 then swap it with j-th 
            // number
            if (a[i] < 1) {
                j++;
                int temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
  
    /* Driver program to test above
    function */
    public static void Main()
    {
  
        int[] a = { 1, 0, 0, 1, 0, 1, 0,
                    1, 1, 1, 1, 1, 1, 0,
                    0, 1, 1, 0, 1, 0, 0 };
  
        int n = a.Length;
  
        sortBinaryArray(a, n);
  
        for (int i = 0; i < n; i++)
            Console.Write(a[i] + " ");
    }
}
  
// This code is contributed by vt_m.


PHP


输出 :

0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1

复杂度分析:

  • 时间复杂度: O(n)。
    只需要遍历数组一次,因此时间复杂度为O(n)。
  • 空间复杂度: O(1)。
    所需空间是恒定的。