retagged by
1,380 views
0 votes
0 votes
Consider an array consisting of –ve and +ve numbers. What would be the worst time comparisons an algorithm can take in order to segregate the numbers having same sign altogether i.e all +ve on one side and then all -ve on the other ?

N-1

N

N+1

(N*(N-1))/2

I'm getting N but the solution says N-1 they are using using first element as pivot

but shouldn't we use 0 as pivot hence N should be the answer
retagged by

1 Answer

Best answer
0 votes
0 votes
Lets take numbers -2, 1 , 2,3

Now, what they are saying is have 2 partitions, put -2 in one partition and hence, - will be base for comparison.

Now, start comparing.

 

Compare -2 and 1.  Sign is differnt. So put 1 in different partition as 1 doesnt have same sign as -2.

 

So, N-1 comparisons
selected by

Related questions

0 votes
0 votes
1 answer
4
Sumit Singh Chauhan asked Aug 18, 2018
3,459 views
What is time complexity of fun()?int fun(int n){ int count = 0; for (int i = n; i 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count;}(A) O(n^...