| Line 2: | Line 2: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
//Makes the positive numbers appear after the negative numbers in an array. | //Makes the positive numbers appear after the negative numbers in an array. | ||
| − | //Complexity <math>\theta(n)</math> | + | //Complexity </syntaxhighlight><math>\theta(n)</math> |
| + | <syntaxhighlight lang="c"> | ||
//No. of swaps in the worst case = n/2 | //No. of swaps in the worst case = n/2 | ||
#include<stdio.h> | #include<stdio.h> | ||
<syntaxhighlight lang="c"> //Makes the positive numbers appear after the negative numbers in an array. //Complexity </syntaxhighlight><math>\theta(n)</math> <syntaxhighlight lang="c"> //No. of swaps in the worst case = n/2
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>
<syntaxhighlight lang="c"> //Makes the positive numbers appear after the negative numbers in an array. //Complexity </syntaxhighlight><math>\theta(n)</math> <syntaxhighlight lang="c"> //No. of swaps in the worst case = n/2
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>