In a one-dimensional array, move all negative numbers to the beginning of the array, and the rest to the end while maintaining the order of the sequence. An additional array is not allowed.
|
2 answers
I will offer some alternatives, suddenly it will be interesting:
#include <algorithm> int array[64]; // Каким-то образом заполняем массив. std::sort(array, array + 64); For the case of C++0x :
std::sort(std::begin(array), std::end(array)); For the case of non- C++0x , if you want to have analogues std::begin and std::end :
template<class Cont> typename Cont::iterator begin(Cont& c){ return c.begin(); } template<class Cont> typename Cont::iterator end(Cont& c){ return c.end(); } template<class Cont> typename Cont::const_iterator begin(Cont const& c){ return c.begin(); } template<class Cont> typename Cont::const_iterator end(Cont const& c){ return c.end(); } // Версии для C-style массивов. template<class T, std::size_t N> T* begin(T (&arr)[N]){ return &arr[0]; } template<class T, std::size_t N> T* end(T (&arr)[N]){ return arr + N; } // Используются наши локально определенные функции. std::sort(begin(array), end(array)); |
void sort(int *a,int razmer) { for(int i=0;i<razmer-1;i++) for(int j=0;j<razmer-1-i;j++) if(a[j]>0) if(a[j+1]<0) { int temp; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } |