1

Given an array that contains alphabets and numbers, provide an algorithm to move the numbers to the front of the array and alphabets to the end of the array without changing their order in the given array.

Expected Space complexity: In place
Expected Time complexity: O(n)

E.g:

Input:
{1,2,a,3,b,c,4,d,5,e}

Output:
{1,2,3,4,5,a,b,c,d,e}

I came across this question in a website and couldn't figure out an algorithm that satisfies the space and time requirements. Can anyone please tell if it is possible to solve it inplace in O(n) time with the algorithm?

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Is it "expected time complexity" as a statistical term, or an expectation regarding the pessimistic time complexity? – BartoszKP Nov 25 '13 at 17:21
  • A small modification of the code in [this answer](http://stackoverflow.com/a/7747590/56778) will do it. Just set `pivot` to 9, change the "less than" case to "less than or equal to," and remove the equal case. – Jim Mischel Nov 25 '13 at 17:38
  • What you want is a Radix sort, but stable Radix is not possible in-place. – Ja͢ck Nov 27 '13 at 07:00

0 Answers0