can this be done in single pass in O(n) .????
On Thu, Jun 21, 2012 at 8:10 PM, rusty <[email protected]> wrote:
> guys this is my solution to the problem, it's a bit sloppy but as far as I
> checked it was working please have a go at it?
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> int arr1[] = {0,-5,3,0,4,-6,-9};
> int arr2[7];
> int j = 0;
> for ( int i = 0 ; i < 7 ; i++ ) {
> //loop for -ve numbers
> if ( arr1[i] < 0 )
> arr2[j++] = arr1[i];
>
> }
>
> //accomodate all the zeros if any
> for ( int i = 0 ; i < 7 ; i++ ) {
> if ( arr1[i] == 0 )
> arr2[j++] = arr1[i];
> }
> for ( int i = 0 ; i < 7 ; i++ ) {
> //loop for +ve numbers
> if ( arr1[i] > 0 )
> arr2[j++] = arr1[i];
>
> }
> //print arr1 for reference
> printf("\nInitially the array was");
> for ( int i = 0 ; i < 7 ; i++ )
> printf("\narr1[%d]: %d",i,arr1[i]);
> printf("\n\n");
> //print arr2
> for ( int i = 0 ; i < 7 ; i++ )
> printf("\narr2[%d]: %d",i,arr2[i]);
>
> return 0;
>
> }
>
> On Wednesday, June 13, 2012 9:49:49 PM UTC+5:30, Krishna Kishore wrote:
>>
>> Given a array of integers both positive and negative and you need to
>> shift positive numbers to one side
>> and negative numbers to other side with the order intact.
>> ex. {-1,5,3,-8,4,-6,9} to {-1,-8,-6,5,3,4,9}. This should be done in O(n).
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/algogeeks/-/H8ANlbL0dmEJ.
>
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
Cheers,
Nishant Pandey |Specialist Tools Development |
[email protected]<[email protected]> |
+91-9911258345
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.