2

I was asked an interesting question at my interview at Atrenta. It was to sort an array with an complexity of O(n) which I said is not possible but he insisted it is, even after the interview.

It is like this.

You have an array, lets say : [1,0,1,0,1,1,0] and this needs to be sorted. What ever has to be done within the array (in the sense no other data structure involved.

I haven't and I don't think it is possible to do any sort with an complexity of O(n). The best I could think of is O(n * log n) and my knowledge comes from Wikipedia.

Please give me your ideas and well a way to do it if you know.

6
  • Read the rest of the Wikipedia article on sorting. O(n log n) only need apply to comparison based sorts, not e.g. radix sort. Commented Nov 15, 2012 at 17:09
  • This has been asked and answered numerous times on SO, e.g. stackoverflow.com/questions/7655659/…. Commented Nov 15, 2012 at 17:10
  • any constraint on the data set can result in a simplification of the algorithm. Here, if data are 0 and 1, a bucket sort is O(n+k), which is O(n+2), which is O(n) Commented Nov 15, 2012 at 17:17
  • a counting sort would work too. Commented Nov 15, 2012 at 17:19
  • Feeling soooo horrible and ashamed... :D You guys are right! So a restriction would have done the magic. Commented Nov 15, 2012 at 17:23

3 Answers 3

9

In your example there are only two different values in the array, so you could use counting sort:

zero_count = 0
for value in array:
    if value == 0:
        zero_count += 1
for i in 0 ... zero_count:
    array[i] = 0
for i in zero_count ... array.size:
    array[i] = 1

Radix sorts are a family of more generally applicable O(n) sorts.

It is comparison sorts that require Omega(n * log n) comparisons on average and hence cannot run in worst-case or average-case linear time.

Sign up to request clarification or add additional context in comments.

2 Comments

that looks like python, so for beauty of short code, array.count(0) does it. (however, for big O measure, a for loop is more explicit, of course)
@njzk2: yeah, it's a Python-influenced pseudo-code. In actual Python, zeros = array.count(0); return [0] * zeros + [1] * (len(array) - zeros) would do it but like you say that doesn't make the complexity very explicit.
2

Traverse the array from both ends and Swap 1's and 0's when needed.Runs in O(n) but has all the if conditions(bruteforce like approach.probably not the expected answer) ;)

int i = 0 ;
int j = array.size -1 ;
for ( i = 0 ; i < j ; ) {

    if( array[i] == 1) {
        if( array[j] == 0 ) {
            array[j] = 1 ; array[i] = 0 ; //Swap 
            i++; j--; 
            continue;
        }
        //else
            j--;
            continue;

    }
   //else
        if( array[j] == 0 ) {
            i++; 
            continue;
        }

        //else 
            i++ ;
            j--;            
}

3 Comments

I think Steve's answer using counting sort was the expected answer there
Both works! this is kind of sort, that is called counting I guess!
both answers are correct and are classified in the complexity of O(n)
0

Since it is a binary array, It is possible to solve it with complexity of O(n) using the two pointer method. The code will look like this :

void sort01(int arr[], int n){
int i = 0, j = n - 1;
while (i <= j){
    if (arr[i] == 0)
    {
        i++;
    }
    else
    {
        swap(arr[i], arr[j]);
        j--;
    }
  }
}

1 Comment

swap() is one more complexity about this algorithm not being classified as O(n). And this is wrong because it will not order in the second loop, creating an exit like this: {0,0,1,1,1,0,1}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.