Segregate 0's and 1's in an array list using Python?

Arrays are linear data structures that store elements in contiguous memory locations. Given an array containing only 0s and 1s, we need to segregate them so all 0s appear on the left and all 1s on the right.

Problem Statement

The goal is to rearrange an array of 0s and 1s such that all zeros come before all ones ?

Input: [0,1,1,0,0,1,0,0,0]
Output: [0,0,0,0,0,0,1,1,1]

Let's explore different methods to achieve this segregation in Python.

Method 1: Using Count Approach

Count the total number of 0s, then place all 0s at the beginning and fill remaining positions with 1s ?

def segregate_by_count(array):
    # Count zeros in array
    count_zeros = 0
    for num in array:
        if num == 0:
            count_zeros += 1
    
    # Place all zeros at beginning
    for i in range(count_zeros):
        array[i] = 0
    
    # Place all ones after zeros
    for i in range(count_zeros, len(array)):
        array[i] = 1
    
    return array

# Example usage
array = [0, 1, 1, 0, 0, 1, 0, 0, 0]
print("Original array:", array)
result = segregate_by_count(array.copy())
print("Segregated array:", result)
Original array: [0, 1, 1, 0, 0, 1, 0, 0, 0]
Segregated array: [0, 0, 0, 0, 0, 0, 1, 1, 1]

Method 2: Using Two Pointers

Use left and right pointers to swap misplaced elements. Move left pointer past 0s and right pointer past 1s, then swap when both point to wrong elements ?

def segregate_two_pointers(array):
    left, right = 0, len(array) - 1
    
    while left < right:
        # Move left pointer past all zeros
        while array[left] == 0 and left < right:
            left += 1
        
        # Move right pointer past all ones
        while array[right] == 1 and left < right:
            right -= 1
        
        # Swap if both pointers point to wrong elements
        if left < right:
            array[left], array[right] = array[right], array[left]
            left += 1
            right -= 1
    
    return array

# Example usage
array = [0, 1, 1, 0, 0, 1, 0, 0, 0]
print("Original array:", array)
result = segregate_two_pointers(array.copy())
print("Segregated array:", result)
Original array: [0, 1, 1, 0, 0, 1, 0, 0, 0]
Segregated array: [0, 0, 0, 0, 0, 0, 1, 1, 1]

Method 3: Using List Comprehension

Create separate lists for 0s and 1s, then concatenate them ?

def segregate_list_comprehension(array):
    zeros = [x for x in array if x == 0]
    ones = [x for x in array if x == 1]
    return zeros + ones

# Example usage
array = [0, 1, 1, 0, 0, 1, 0, 0, 0]
print("Original array:", array)
result = segregate_list_comprehension(array)
print("Segregated array:", result)
Original array: [0, 1, 1, 0, 0, 1, 0, 0, 0]
Segregated array: [0, 0, 0, 0, 0, 0, 1, 1, 1]

Method 4: Using Built-in Sort

Since 0 < 1, sorting naturally segregates the array ?

def segregate_with_sort(array):
    return sorted(array)

# Example usage
array = [0, 1, 1, 0, 0, 1, 0, 0, 0]
print("Original array:", array)
result = segregate_with_sort(array)
print("Segregated array:", result)
Original array: [0, 1, 1, 0, 0, 1, 0, 0, 0]
Segregated array: [0, 0, 0, 0, 0, 0, 1, 1, 1]

Comparison

Method Time Complexity Space Complexity In-place
Count Approach O(n) O(1) Yes
Two Pointers O(n) O(1) Yes
List Comprehension O(n) O(n) No
Built-in Sort O(n log n) O(1) Depends on implementation

Conclusion

For optimal performance, use the two-pointers approach as it's O(n) time and O(1) space with in-place modification. The count approach is simpler to understand, while list comprehension offers readable code at the cost of extra memory.

Updated on: 2026-03-24T20:52:43+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements