Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- int a[] = {-1, 0, -2, 3, -4, 5};
- int n = sizeof a / sizeof *a;
- int l = 0, r = n - 1;
- for( ;; )
- {
- /* skip positives on the left */
- while( l < r && a[l] >= 0 )
- l++;
- /* skip negatives on the right */
- while( l < r && a[r] < 0 )
- r--;
- if ( l >= r ) break;
- /* swap the negative on the left with the positive on the right */
- int t = a[l];
- a[l] = a[r];
- a[r] = t;
- l++, r--; /* not necessary but saves comparisons */
- }
- for( l = 0; l < n; l++ )
- printf("%dn", a[l]);
- return 0;
- }
- j=0
- for i=0 to n
- do if (a[i]<0)
- then a[i] <-> a[j] //swapping
- j=j+1
- -------------------------------------------------------
- #include<stdio.h>
- int main()
- {
- int a[]={-1,-2,-4,0,1,4,-5,-3};
- int n=sizeof(a)/sizeof(int);
- int i,j;
- for(i=0,j=0;i<n;i++)
- {
- if(a[i]<0)
- {
- int temp;
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- j++;
- }
- }
- for(i=0;i<n;i++)
- printf("%dt",a[i]);
- return 0;
- }
Add Comment
Please, Sign In to add comment