Sort an array of 0s, 1s and 2s Using Python4DSA

admin
By -
0

#PythonDSA



Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

We using some normal python function to solve this without using any  sorting algo .

Problem Link:GeeksforGeeks


Solution:

class Solution:

    def sort012(self,arr,n):

          # code here

          zero = [num for num in arr[:n] if num == 0]

          one = [num for num in arr[:n] if num == 1]

          two = [num for num in arr[:n] if num == 2]

           arr.clear()

          arr.extend(zero)

          arr.extend(one)

          arr.extend(two) 

Post a Comment

0Comments

Put Your Thought or Query Here

Post a Comment (0)