Sort an array of 0s 1s and 2s

Given an array which include 3 types of integer numbers (0,1,2). Our goal is to sort this array in ascending order. For example.

Example A
// Befor sort
arr[] = [0, 1, 0, 2, 2, 1, 2,0, 0, 2, 0, 1, 1, 0 ,2]

// After sort
arr[] = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]

Example B
// Befor sort
arr[] = [1,2,0,0,1]
// After sort
arr[] = [0, 0, 1, 1, 2]

Solution by using counting




Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2022, kalkicode.com, All rights reserved