Challenge: Partitioning of 0s and 1s

Learn how you can solve the problem of partitioning 0s and 1s using a sorting algorithm.

Problem

Given an array of 0s and 1s, write an algorithm to sort the array so that the 0s come first. Find the minimum number of swaps to sort the array.

Input

An integer array.

Output

An integer array and the count of minimum number of swaps.

Sample input

array = {0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1}

Sample output

array = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}
count = 3

Coding exercise

Try to solve this yourself first. If you get stuck or need help, you can always press the ā€œShow Solutionā€ button to see how your problem can be solved. Weā€™ll look at the solution in the next lesson.

Good luck!

Get hands-on with 1400+ tech skills courses.