# Minimum Adjacent Swaps Required to Sort the given Binary Array

There are different approaches, we can use to minimize the number of swaps required on adjacent elements to get a sorted array. The given array as the output only contains two types of elements i.e., 0 and 1. We will discuss two different approaches to solve the problem in which the first solution uses extra space to store the number of zeroes, while the second one uses only constant space.

## Problem Statement

We are given an array which contains only two types of elements 0 & 1. Our aim is to find out how many minimum swaps on adjacent elements are required to sort the given binary array.

### Example

Given Array: [1, 1, 0, 0, 0, 1, 0]
Result: 9 swaps required


### Explanation

Swap 1: [0, 1, 1, 0, 0, 0, 0]
Swap 2: [0, 1, 0, 1, 0, 0, 0]
Swap 3: [0, 1, 0, 0, 1, 0, 0]
Swap 4: [0, 1, 0, 0, 0, 1, 0]
Swap 5: [0, 1, 0, 0, 0, 0, 1]
Swap 6: [0, 0, 1, 0, 0, 0, 1]
Swap 7: [0, 0, 0, 1, 0, 0, 1]
Swap 8: [0, 0, 0, 0, 1, 0, 1]
Swap 9: [0, 0, 0, 0, 0, 1, 1]


Let us now discuss a simple approach to solve this problem.

## Approach 1

In this approach, we will count the total number of 0’s and ones, we can do this just by counting the number of zeros appearing after every 1 and then just add them. As we know, all the ones will be on the rightmost side and all the 0’s will be on the leftmost side of the array after sorting. That means, we have to swap every 1’s with every 0’s on their right side in the array. Number of swaps needed for everyone in the array will be the total number of 0’s appearing to its right in the array. We will keep on adding the total number of 0’s appearing to the left side for each 1’s to get the desired number of swaps.

### Example

In the example below, we create a binary array of seven numbers. We find the minimum number of adjacent swaps required to sort the array using the above-given approach.

#include <bits/stdc++.h>
using namespace std;

// this function calculates the minimum number of swaps
int minimum_number_of_swaps(int given_array[], int nums){
int Number_of_zeroes[nums];
memset( Number_of_zeroes, 0, sizeof(Number_of_zeroes));
int iterator, number = 0;
Number_of_zeroes[nums - 1] = 1 - given_array[nums - 1];
for (iterator = nums - 2; iterator >= 0; iterator--) {
Number_of_zeroes[iterator] = Number_of_zeroes[iterator + 1];
if (given_array[iterator] == 0)
Number_of_zeroes[iterator]++;
}
for (iterator = 0; iterator < nums; iterator++) {
if (given_array[iterator] == 1)
number += Number_of_zeroes[iterator];
}
return number;
}
// main code goes from here
int main(){
int given_array[] = { 1, 1, 0, 0, 0, 1, 0 };
int nums = sizeof(given_array) / sizeof(given_array);
cout  << " Minimum number of swaps required to sort the given binary array is " << minimum_number_of_swaps(given_array, nums);
return 0;
}


### Output

When you run the above C++ program, it will produce the following output −

Minimum number of swaps required to sort the given binary array is 9


Time complexity of this approach − Since we are iterating in one loop n number of times, time complexity is: O(n)

Space complexity − As we have used an extra array to store number of zeroes, the space complexity for this approach is O(n)

Now let us look at a better and efficient approach to solve the same problem. Our new solution saves memory as it does not take any additional space.

## Approach 2

In this approach, we will minimize the auxiliary space to constant space. Instead of reading out the array from beginning, we will start iteration from last and count all the zeros we encounter. If we get a 1, the number of swaps required to put that 1 in its sorted position is the number of zeros we encountered before it.

### Example

Below is the C++ implementation for the above approach −

#include <iostream>
using namespace std;

// this function finds out the least number of swaps needed
int minimum_number_of_swaps(int nums[], int number){
int c = 0;
int zeros_unplaced = 0;
for(int iterator=number-1;iterator>=0;iterator--){
if(nums[iterator] == 0)
zeros_unplaced += 1;
if(nums[iterator] == 1)
c += zeros_unplaced;
}
return c;
}
// Main code goes here
int main(){
int nums[] = { 1, 1, 0, 0, 0, 1, 0 };
cout<< " Minimum number of swaps required to sort the given binary array is " << minimum_number_of_swaps(nums, 7);
return 0;
}


### Output

When you run the above C++ program, it will produce the following output −

Minimum number of swaps required to sort the given binary array is 9


Time complexity of this approach − Since we are iterating in one loop n number of times, time complexity is: O(n)

Space complexity − As we are not using any extra space, the space complexity is linear that is O(1).

In this article, we have discussed two ways to calculate the minimum number of swaps required to sort an array containing only 0’s and 1’s. In the first approach, we used an extra array to store our solution at every step while in the second approach, we did it in constant space resulting in better space complexity.

Updated on: 11-Apr-2023

215 Views 