Hello!
Could anybody help me with conversioning c++ code to java...I'm having a very hard time with pointers and memory allocations (memcpy) written in c++, which I want to write in java.I would be very grateful, if somebody took a couple of minutes and help me to fix this problem, which is bothering me for days.
Here's the code:
#include<iostream>
using namespace std;
class CMO
{
private:
int m_cnt;
public:
/* Constructor/Destructor */
CMO(int in[], int cnt)
{
m_in = new int[cnt];
m_cnt = cnt;
for(int i=0;i<m_cnt;i++)
{
m_in[i]=in[i];
}
}
int Sort()
{
return mSort(0, m_cnt);
}
private:
int mSort(int ind, int cnt)
{
int tmp;
int left, right;
if (cnt == 1) {
return 0;
}
left = (cnt + 1) / 2;
right = cnt / 2;
if (cnt == 2) {
mDisplay(ind, 1, 1);
if (m_in[ind] > m_in[ind+1]) {
tmp = m_in[ind];
m_in[ind] = m_in[ind+1];
m_in[ind+1] = tmp;
}
mDisplay(ind, 2, 0);
} else {
mDisplay(ind, left, right);
mSort(ind, left);
mSort(ind+left, right);
mOrder(ind, left, right);
mDisplay(ind, left + right, 0);
}
return 0;
}
int mOrder(int ind, int left, int right)
{
int i, a, b;
int *tmp;
tmp = new int[left+right];
a = b = 0;
for (i = 0; i < (left+right); ++i) {
if ((m_in[ind+a] > m_in[ind+left+b] && b < right) || a >= left) {
tmp[i] = m_in[ind+left+b];
++b;
} else if (a < left) {
tmp[i] = m_in[ind+a];
++a;
}
}
memcpy(&m_in[ind], tmp, (left + right) * sizeof(int));
delete [] tmp;
return 0;
}
void mDisplay(int ind, int left, int right)
{
int i, j;
for (i = 0; i < left; ++i) {
std::cout << m_in[ind+i] << " ";
}
if (right == 0) {
std::cout << std::endl;
return;
}
std::cout << "| ";
for (j = 0; j < right; ++j) {
std::cout << m_in[ind+i+j] << " ";
}
std::cout << std::endl;
}
};
int main(int argc, char *argv[])
{
int in[] = {8, 5, 6, 1, 7, 2, 0, 9};
CMO cmo(in,8);
cmo.Sort();
system("PAUSE");
return 0;
}