Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.
Example 1:
Input: 
N = 5
arr[]= {0 2 1 2 0}
Output:
0 0 1 2 2
Explanation:
0s 1s and 2s are segregated 
into ascending order.
Example 2:
Input: 
N = 3
arr[] = {0 1 0}
Output:
0 0 1
Explanation:
0s 1s and 2s are segregated 
into ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^6
0 <= A[i] <= 2

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3



class Solution:

    def sort012(self,arr,n):

        # code here



Solutions:
class Solution:

	def sort012(self, arr, n):
		arr.sort()


class Solution:

	def sort012(self, arr, n):
		(c, c1, c2) = (0, 0, 0)
		for i in arr:
			if i == 0:
				c += 1
			elif i == 1:
				c1 += 1
			else:
				c2 += 1
		for j in range(c):
			arr[j] = 0
		for j in range(c, c1 + c):
			arr[j] = 1
		for j in range(c1 + c, c2 + c1 + c):
			arr[j] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		low = 0
		mid = 0
		high = n - 1
		while mid <= high:
			if arr[mid] == 1:
				mid += 1
			elif arr[mid] == 0:
				(arr[low], arr[mid]) = (arr[mid], arr[low])
				low += 1
				mid += 1
			else:
				(arr[mid], arr[high]) = (arr[high], arr[mid])
				high -= 1
		return arr


class Solution:

	def sort012(self, arr, n):
		a = arr.count(0)
		b = arr.count(1)
		c = arr.count(2)
		for i in range(a):
			arr[i] = 0
		for i in range(a, a + b):
			arr[i] = 1
		for i in range(a + b, a + b + c):
			arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		lo = 0
		hi = n - 1
		mid = 0
		while mid <= hi:
			val = arr[mid]
			if val == 0:
				temp = arr[lo]
				arr[lo] = val
				arr[mid] = temp
				lo += 1
				mid += 1
			elif val == 1:
				mid += 1
			elif val == 2:
				temp = val
				arr[mid] = arr[hi]
				arr[hi] = temp
				hi -= 1


class Solution:

	def sort012(self, arr, n):
		lo = 0
		mid = 0
		hi = len(arr) - 1
		while mid <= hi:
			if arr[mid] == 0:
				(arr[lo], arr[mid]) = (arr[mid], arr[lo])
				lo += 1
				mid += 1
			elif arr[mid] == 1:
				mid += 1
			else:
				(arr[hi], arr[mid]) = (arr[mid], arr[hi])
				hi -= 1
		return arr


class Solution:

	def sort012(self, arr, n):
		cnt0 = 0
		cnt1 = 0
		cnt2 = 0
		for i in arr:
			if i == 0:
				cnt0 = cnt0 + 1
			elif i == 1:
				cnt1 = cnt1 + 1
			else:
				cnt2 = cnt2 + 1
		for i in range(n):
			if i < cnt0:
				arr[i] = 0
			elif cnt0 <= i < cnt0 + cnt1:
				arr[i] = 1
			else:
				arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		l = [0] * 3
		for i in range(0, len(arr)):
			if arr[i] == 0:
				l[0] = l[0] + 1
			if arr[i] == 1:
				l[1] = l[1] + 1
			if arr[i] == 2:
				l[2] = l[2] + 1
		for i in range(0, l[0]):
			arr[i] = 0
		for i in range(l[0], l[0] + l[1]):
			arr[i] = 1
		for i in range(l[0] + l[1], len(arr)):
			arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		c0 = 0
		c1 = 0
		c2 = 0
		for i in range(n):
			if arr[i] == 0:
				c0 += 1
			if arr[i] == 1:
				c1 += 1
			if arr[i] == 2:
				c2 += 1
		k = 0
		for i in range(c0):
			arr[k] = 0
			k += 1
		for i in range(c1):
			arr[k] = 1
			k += 1
		for i in range(c2):
			arr[k] = 2
			k += 1


class Solution:

	def sort012(self, arr, n):
		x = arr.sort()
		return x


class Solution:

	def sort012(self, arr, n):
		a = arr.sort()
		return a


class Solution:

	def sort012(self, arr, n):
		z = 0
		o = 0
		t = 0
		for x in arr:
			if x == 0:
				z += 1
			elif x == 1:
				o += 1
			elif x == 2:
				t += 1
		for i in range(n):
			if z > 0 and i < z:
				arr[i] = 0
			elif o > 0 and i < z + o:
				arr[i] = 1
			elif t > 0 and i < z + o + t:
				arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		s = arr.sort()
		return s


from collections import OrderedDict
import copy

class Solution:

	def sort012(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		d1 = OrderedDict(sorted(d.items()))
		l = []
		k = -1
		for i in range(0, len(arr)):
			arr.pop()
		for i in d.values():
			k += 1
			for j in range(0, i):
				arr.append(k)
		return arr


class Solution:

	def sort012(self, arr, n):
		sorted_arr = []
		hash = {}
		for ele in arr:
			if ele not in hash:
				hash[ele] = 1
			else:
				hash[ele] += 1
		hash_key = list(hash.keys())
		hash_key.sort()
		sorted_hash = {k: hash[k] for k in hash_key}
		for (key, val) in sorted_hash.items():
			sorted_arr += [key] * val
		for i in range(len(sorted_arr)):
			arr[i] = sorted_arr[i]
		return arr


class Solution:

	def sort012(self, arr, n):
		s = arr[0]
		arr.sort()
		l = []
		for i in range(len(arr)):
			l.append(arr)
			l.remove(arr)
		l.sort
		for i in l:
			i = str(i) + ' '
			print(i)
			break


class Solution:

	def sort012(self, arr, n):
		low = 0
		mid = 0
		high = n - 1
		for i in range(n):
			if arr[mid] == 0:
				(arr[low], arr[mid]) = (arr[mid], arr[low])
				low += 1
				mid += 1
			elif arr[mid] == 1:
				mid += 1
			elif arr[mid] == 2:
				(arr[mid], arr[high]) = (arr[high], arr[mid])
				high -= 1
		return arr


class Solution:

	def sort012(self, a, n):
		low = 0
		mid = 0
		high = n - 1
		while mid <= high:
			if a[mid] == 1:
				mid += 1
				continue
			if a[mid] == 0:
				(a[low], a[mid]) = (a[mid], a[low])
				low += 1
				mid += 1
				continue
			if a[mid] == 2:
				(a[mid], a[high]) = (a[high], a[mid])
				high -= 1


class Solution:

	def sort012(self, arr, n):
		indx0 = 0
		indx1 = 0
		for i in range(n):
			if arr[i] == 0:
				arr[indx0] = 0
				indx0 += 1
			if arr[i] == 1:
				indx1 += 1
			while indx1 > 0:
				arr[indx0] = 1
				indx0 += 1
				indx1 -= 1
			for i in range(indx0, n):
				arr[i] = 2
			return arr

class Solution:

	def sort012(self, arr, n):
		indx0 = 0
		indx1 = 0
		for i in range(n):
			if arr[i] == 0:
				arr[indx0] = 0
				indx0 += 1
			if arr[i] == 1:
				indx1 += 1
		while indx1 > 0:
			arr[indx0] = 1
			indx0 += 1
			indx1 -= 1
		for i in range(indx0, n):
			arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		zeroCount = 0
		oneCount = 0
		twoCount = 0
		for val in arr:
			if val == 0:
				zeroCount += 1
			elif val == 1:
				oneCount += 1
			elif val == 2:
				twoCount += 1
		for idx in range(n):
			if zeroCount > 0 and idx < zeroCount:
				arr[idx] = 0
			elif oneCount > 0 and idx < zeroCount + oneCount:
				arr[idx] = 1
			elif twoCount > 0 and idx < zeroCount + oneCount + twoCount:
				arr[idx] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		zeros = 0
		ones = 0
		twos = 0
		for i in range(n):
			if arr[i] == 0:
				zeros = zeros + 1
			if arr[i] == 1:
				ones = ones + 1
			if arr[i] == 2:
				twos = twos + 1
		j = 0
		for i in range(zeros):
			arr[j] = 0
			j = j + 1
		for i in range(ones):
			arr[j] = 1
			j = j + 1
		for i in range(twos):
			arr[j] = 2
			j = j + 1


class Solution:

	def sort012(self, arr, n):
		count = {0: 0, 1: 0, 2: 0}
		for num in arr:
			count[num] += 1
		arr.clear()
		for (num, freq) in count.items():
			arr.extend([num] * freq)
		return arr


class Solution:

	def sort012(self, arr, n):
		count0 = 0
		count1 = 0
		count2 = 0
		for i in range(n):
			if arr[i] == 0:
				count0 += 1
			if arr[i] == 1:
				count1 += 1
			if arr[i] == 2:
				count2 += 1
		k = 0
		while count0 != 0:
			arr[k] = 0
			count0 -= 1
			k += 1
		while count1 != 0:
			arr[k] = 1
			count1 -= 1
			k += 1
		while count2 != 0:
			arr[k] = 2
			count2 -= 1
			k += 1
		return arr


class Solution:

	def sort012(self, nums, n):
		(low, mid) = (0, 0)
		high = len(nums) - 1
		while mid <= high:
			if nums[mid] == 2:
				(nums[mid], nums[high]) = (nums[high], nums[mid])
				high -= 1
			elif nums[mid] == 0:
				(nums[mid], nums[low]) = (nums[low], nums[mid])
				low += 1
				mid += 1
			elif nums[mid] == 1:
				mid += 1


class Solution:

	def sort012(self, arr, n):
		l = m = 0
		h = n - 1
		while m <= h:
			a = arr[m]
			if a == 0:
				temp = arr[l]
				arr[l] = arr[m]
				arr[m] = temp
				m += 1
				l += 1
			elif a == 1:
				m += 1
			else:
				temp = arr[m]
				arr[m] = arr[h]
				arr[h] = temp
				h -= 1


class Solution:

	def sort012(self, a, n):
		(l, m, h) = (0, 0, n - 1)
		for i in range(n):
			if a[m] == 0:
				(a[l], a[m]) = (a[m], a[l])
				l += 1
				m += 1
			elif a[m] == 1:
				m += 1
			else:
				(a[m], a[h]) = (a[h], a[m])
				h -= 1


class Solution:

	def sort012(self, arr, n):
		start = 0
		end = len(arr) - 1
		last_1 = 0
		while start <= end:
			if arr[start] == 0:
				(arr[last_1], arr[start]) = (arr[start], arr[last_1])
				last_1 += 1
			elif arr[start] == 1:
				pass
			else:
				(arr[end], arr[start]) = (arr[start], arr[end])
				end -= 1
				start -= 1
			start += 1
		return arr


class Solution:

	def sort012(self, arr, n):
		arr.sort()
		for i in range(n):
			if arr[i - 1] == arr[i]:
				return arr[i]


class Solution:

	def sort012(self, arr, n):
		cnt = [0, 0, 0]
		for c in arr:
			cnt[int(c)] += 1
		for i in range(cnt[0]):
			arr[i] = 0
		for i in range(cnt[1]):
			arr[i + cnt[0]] = 1
		for i in range(cnt[2]):
			arr[i + cnt[0] + cnt[1]] = 2
		return cnt


class Solution:

	def sort012(self, arr, n):
		for (i, x) in enumerate(sorted(arr)):
			arr[i] = x
		return arr


class Solution:

	def sort012(self, arr, n):
		count_0 = 0
		count_1 = 0
		count_2 = 0
		for i in range(0, n):
			if arr[i] == 0:
				count_0 += 1
			if arr[i] == 1:
				count_1 += 1
			if arr[i] == 2:
				count_2 += 1
		for i in range(0, n):
			if i < count_0:
				arr[i] = 0
			elif i < count_1 + count_0:
				arr[i] = 1
			else:
				arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		zero_count = arr.count(0)
		one_count = arr.count(1)
		two_count = n - (zero_count + one_count)
		i = 0
		while zero_count > 0:
			arr[i] = 0
			i += 1
			zero_count -= 1
		while one_count > 0:
			arr[i] = 1
			i += 1
			one_count -= 1
		while two_count > 0:
			arr[i] = 2
			i += 1
			two_count -= 1
		return arr


class Solution:

	def sort012(self, arr, n):
		t = arr.sort()
		return t


class Solution:

	def sort012(self, arr, n):
		c0 = 0
		c1 = 0
		c2 = 0
		for ele in arr:
			if ele == 0:
				c0 += 1
			elif ele == 1:
				c1 += 1
			elif ele == 2:
				c2 += 1
		for i in range(c0):
			arr[i] = 0
		for i in range(c0, c1 + c0):
			arr[i] = 1
		for i in range(c0 + c1, n):
			arr[i] = 2
		return arr


class Solution:

	def sort012(self, arr, n):
		count = [0] * 3
		for num in arr:
			count[num] += 1
		index = 0
		for i in range(3):
			for _ in range(count[i]):
				arr[index] = i
				index += 1
		return arr


class Solution:

	def sort012(self, arr, n):
		hashmap = {}
		for i in arr:
			if i in hashmap:
				hashmap[i] += 1
			else:
				hashmap[i] = 1
		j = 0
		for i in range(3):
			while hashmap[i] > 0:
				arr[j] = i
				hashmap[i] -= 1
				j += 1


class Solution:

	def sort012(self, a, n):
		l = 0
		mid = 0
		h = n - 1
		while mid <= h:
			if a[mid] == 0:
				(a[l], a[mid]) = (a[mid], a[l])
				l += 1
				mid += 1
			elif a[mid] == 1:
				mid += 1
			else:
				(a[mid], a[h]) = (a[h], a[mid])
				h -= 1
		return a


class Solution:

	def sort012(self, arr, n):
		r = arr.sort()
		return r


class Solution:

	def sort012(self, arr, n):
		lis = list.sort(arr)
		return lis


class Solution:

	def sort012(self, arr, n):
		start = 0
		end = n - 1
		i = 0
		while i <= end:
			if arr[i] == 0:
				(arr[start], arr[i]) = (arr[i], arr[start])
				start += 1
				i += 1
			elif arr[i] == 2:
				(arr[end], arr[i]) = (arr[i], arr[end])
				end -= 1
			else:
				i += 1


class Solution:

	def sort012(self, arr, n):
		arr.sort()
		for i in arr:
			return i


class Solution:

	def sort012(self, arr, n):
		cnt_0 = arr.count(0)
		cnt_1 = arr.count(1)
		cnt_2 = arr.count(2)
		i = 0
		while cnt_0 > 0:
			arr[i] = 0
			i += 1
			cnt_0 -= 1
		while cnt_1 > 0:
			arr[i] = 1
			i += 1
			cnt_1 -= 1
		while i < n:
			arr[i] = 2
			i += 1
		return arr


class Solution:

	def sort012(self, arr, n):
		low = 0
		high = len(arr) - 1
		mid = 0
		while mid <= high:
			if arr[mid] == 0:
				(arr[low], arr[mid]) = (arr[mid], arr[low])
				low += 1
				mid += 1
			elif arr[mid] == 1:
				mid += 1
			else:
				(arr[mid], arr[high]) = (arr[high], arr[mid])
				high -= 1
		return arr

	def print(arr):
		for k in arr:
			print(k, end=' ')


class Solution:

	def sort012(self, arr, n):
		zero = 0
		one = 0
		two = 0
		for i in range(n):
			if arr[i] == 0:
				zero += 1
			if arr[i] == 1:
				one += 1
			if arr[i] == 2:
				two += 1
		for j in range(zero):
			arr[j] = 0
		for j in range(zero, zero + one):
			arr[j] = 1
		for j in range(zero + one, zero + one + two):
			arr[j] = 2