Optimised code for sorting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Optimised code for sorting

Hello I came across a question related to sorting... Array is of size 1 lacs and possible values are two only either 0 or 1. Can we sort an array with O(N)?

25th Feb 2021, 4:13 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
Does the sorting has to be stable? Just count the number of 1 and 0 and run another loop to set same no of 1's and 0's. Space O(1) time O(n) For integers take a look at radix sort and counting sort.
26th Feb 2021, 6:17 AM
Akib
Akib - avatar