#include <iostream>
#include <cstring>
#include<algorithm>
using namespace std;
int main () {
int arr[] = {1,0,2,0,3,0,0,4,5,6,7,0,0,0};
int i=0,j=0;
int len = sizeof(arr)/sizeof(int);
while(1) {
while(j<len && arr[j]!=0) j++;
i=j;
while(i<len && arr[i]==0)i++;
if(i<len)
swap(arr[j], arr[i]);
else break;
}
for (int k = 0; k < len; ++k)
cout << arr[k] << " ";
cout << endl;
return 0;
}
check out this code
O(n) time complexity and O(1) space
It iks stable for the first set of numbers and non stable for series of 0
numbers
--
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.