ime.saurabh
BAN USER
Comments (5)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
It uses O(1) space but time used is O(m^2) where m is number of negetive integers there.
- ime.saurabh August 27, 2013Comment hidden because of low score. Click to expand.
0
of 0 vote
#include<stdio.h>
void swap(int*,int*);
void insert(int*, int*);
int main()
{
int arr[5] = {-1,1,3,-2,-4};
int negcount = 0, i, p;
/*Counting negetive numbers*/
for(i=0;i<5;i++)
if(arr[i]<0)
negcount++;
/*p points to the next location to insert positive number */
p = neg;
/*Main algo part*/
for(i=0;i<neg;i++)
{
if(arr[i] >0)
{
printf("swapping\n");
swap(&arr[i], &arr[p]);
p++;
if(arr[i] <= 0)
insert(&arr[i], &arr[neg-1]);
i--;
}
}
/*Print The Result*/
for(i=0;i<5;i++)
printf("%d\t", arr[i]);
printf("\n");
}
Comment hidden because of low score. Click to expand.
0
of 0 vote
In O(n) time and O(1) space.
- ime.saurabh August 27, 2013Comment hidden because of low score. Click to expand.
0
of 0 vote
-
ime.saurabh
August 27, 2013 | Flag
Reply
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
@fire It's very easy for general cases.
- ime.saurabh August 27, 2013If u have a single level loop in ur program then it is o(n). If there is a loop inside the loop(2 level), then its O(n^2). If inner loop executes only log n times(approx) then it is O(nlogn). If u hav loop inside loop inside loop then its O(n^3) and so on.