Line 49: Line 49:
  
  
<div class="fb-like" data-href="http://armi.in/wiki/NP,_NP_Complete,_NP_Hard" data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
+
<div class="fb-like" data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
  
  
<div class="fb-share-button" data-href="http://armi.in/wiki/NP,_NP_Complete,_NP_Hard" data-type="button_count"></div>
+
<div class="fb-share-button" data-type="button_count"></div>
  
  

Revision as of 15:34, 1 December 2013

Makes the positive numbers appear after the negative numbers in an array.

Complexity <math>\theta(n)</math>

No. of swaps in the worst case = <math> \lfloor n/2 \rfloor</math>

<syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

       int i, n, pi, ni, count = 0;
       int a[1000];
       printf("Enter size of array: ");
       scanf("%d",&n);
       printf("Enter numbers of array\n");
       for(i=0; i<n; i++)
       {
               scanf("%d",&a[i]);
       }
       ni = n-1;
       while(a[ni] >= 0)
               ni--;
       pi = 0;
       while(a[pi] < 0)
               pi++;
       while(ni > pi)
       {
               int temp = a[pi];
               a[pi] = a[ni];
               a[ni] = temp;
               while(a[ni] >= 0)
                       ni--;
               while(a[pi] < 0)
                       pi++;
       }
       for(i=0; i<n; i++)
       {
               printf("%d ", a[i]);
       }


} </syntaxhighlight>





blog comments powered by Disqus

Makes the positive numbers appear after the negative numbers in an array.

Complexity <math>\theta(n)</math>

No. of swaps in the worst case = <math> \lfloor n/2 \rfloor</math>

<syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

       int i, n, pi, ni, count = 0;
       int a[1000];
       printf("Enter size of array: ");
       scanf("%d",&n);
       printf("Enter numbers of array\n");
       for(i=0; i<n; i++)
       {
               scanf("%d",&a[i]);
       }
       ni = n-1;
       while(a[ni] >= 0)
               ni--;
       pi = 0;
       while(a[pi] < 0)
               pi++;
       while(ni > pi)
       {
               int temp = a[pi];
               a[pi] = a[ni];
               a[ni] = temp;
               while(a[ni] >= 0)
                       ni--;
               while(a[pi] < 0)
                       pi++;
       }
       for(i=0; i<n; i++)
       {
               printf("%d ", a[i]);
       }


} </syntaxhighlight>





blog comments powered by Disqus