So in terms of the HW this is done, although I=E2=80=99m not happy this =
the code. It errors on me when the merge is too large, likely due to a comb=
ination of compile deficiency and my coding not doing enough error checking=
. It might help to define the standard c++ to 11, but I=E2=80=99m not certa=
in of that. I need some sleep.
Reuvain
=E2=80=93 So many immigrant groups have swept through our town that Broo=
klyn, like Atlantis, reaches mythological proportions in the mind of the wo=
rld - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.=
com - Leadership Development in Free Software http://www2.mrbrklyn.com/reso=
urces - Unpublished Archive http://www.coinhangout.com - coins! http://www.=
brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but in=
compatible with living as a free human being. -RI Safir 2013 / =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Filename: msort.h Desc=
ription: Header File for Parrell Msort algorthm Version: 1.0 * C=
reated: 11/09/2016 04:28:51 AM * Revision: none * Compiler: gcc =
Author: Ruben Safir * Company: NYLXS Inc =3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D */ #ifndef MSORT_INC #define MSORT_INC
namespace merge{
int min(int x, int y); void track(int , int, int * ); void merge(int j, =
int l, int m, int k, int * parent); } #endif /* =E2=80=94=E2=80=93 #ifndef =
MSORT_INC =E2=80=94=E2=80=93 */
CXX:=3Dg++ CXXFLAGS:=3D-Wall -ggdb -pg -pthread
LDFLAGS:=3D-L/usr/local/lib/mysql -lmysqlpp -lmysqlclient
msort : msort.o main.o ${CXX} ${CXXFLAGS} -o msort msort.o main.o
main.o : main.cpp ${CXX} ${CXXFLAGS} -o main.o -c main.cpp
msort.o : msort.cpp msort.h ${CXX} ${CXXFLAGS} -c msort.cpp
clean : rm nodes .o make.deps touch .cpp *.h
include make.deps make.deps: .cpp ; gcc -M .cpp >$-at- #include=
=E2=80=9Cmsort.h=E2=80=9D #include #include namespace =
merge{
int min(int x, int y){ int ret; x std::cout << x << ":" << y << "=3D=3D>" << ret << std::e=
ndl; return ret; }
void merge(int j, int l, int m, int k, int * value){ int size =3D (k-j) =
+ 1; std::cout << =E2=80=9CAllocating scratch =3D=3D>=E2=80=9D <=
;< size << std::endl; int * scratch =3D new int[size];
int l=
_pos1 =3D j; int l_pos2 =3D l; int r_pos1 =3D m; int r_pos2 =3D k; int i =
=3D 0; std::cout << =E2=80=9CScratch index:=E2=80=9D << i <&=
lt; std::endl; std::cout << =E2=80=9Cl_pos1:=E2=80=9D << l_pos1=
<< =E2=80=9C_pos1: " << r_pos1 << std::endl; std::c=
out << =E2=80=9Cl_pos2:=E2=80=9D << l_pos2 << =E2=80=9C_p=
os2: " << r_pos2 << std::endl; int l_next =3D l_pos1; int =
r_next =3D r_pos1; int cur =3D -1;//cursor rest position int cursor[3] =3D =
{cur, l_next, r_next}; while((cursor[1] <=3D l_pos2) && (cursor[=
2] <=3D r_pos2)) { std::cout << =E2=80=9CPositions=E2=80=9D <&l=
t; cursor[0] << =E2=80=9C:=E2=80=9D << cursor[1] << =E2=
=80=9C:=E2=80=9D << cursor[2] << std::endl; std::cout << =
=E2=80=9CValues=E2=80=9D << value[cursor[1]] << =E2=80=9C:=E2=
=80=9D << value[cursor[2]] << std::endl; if(value[cursor[1]] &l=
t;=3D value[cursor[2]]) { scratch[i] =3D value[cursor[1]]; cursor[0] =3D cu=
rsor[1]; cursor[1]++; }else if(value[cursor[1]] > value[cursor[2]]){ scr=
atch[i] =3D value[cursor[2]]; cursor[0] =3D cursor[2]; cursor[2]++; } std::=
cout << =E2=80=9CPositions=E2=80=9D << cursor[0] << =E2=
=80=9C:=E2=80=9D << cursor[1] << =E2=80=9C:=E2=80=9D << c=
ursor[2] << std::endl; std::cout << =E2=80=9CValues=E2=80=9D &l=
t;< value[cursor[1]] << =E2=80=9C:=E2=80=9D << value[cursor[=
2]] << std::endl; i++; } //we hit the top while(1){ std::cout <<=
; =E2=80=9Cclearing data=E2=80=A6=E2=80=9D; if(cursor[2] <=3D r_pos2 ){ =
scratch[i] =3D value[cursor[2]]; i++; cursor[2]++; }else if(cursor[1] <=
=3D l_pos2){ scratch[i] =3D value[cursor[1]]; i++; cursor[1]++; }else{ brea=
k; } }
std::cout << =E2=80=9Cdone=E2=80=9D << std::endl; =
//copy scratch to the array std::cout << =E2=80=9Ccopying scratch=E2=
=80=9D << std::endl; for(int pos =3D j, i=3D0; i <=3D (k-j); pos++=
, i++){ std::cout << i << " : " << scratch[i] &=
lt;<" to position " << pos << std::endl; value[pos=
] =3D scratch[i]; } std::cout << =E2=80=9Cfree scratch=E2=80=9D <&=
lt; std::endl; delete[] scratch; std::cout << =E2=80=9Cdone with scra=
tch=E2=80=9D << std::endl; return; }
//j is far left of array //k is far right of array //l is middle of arra=
y - right of left child array //m is middle + 1 of the array - left of righ=
t child array // // j l m k //_____________________________________ //| 0| =
1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| //=E2=80=94=E2=80=94=E2=80=94=E2=80=94=
=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=
=80=93 //
void track(int j, int k, int * parent ) { //std::cout << =E2=80=9C=
j is far left of arrayk is far right of arrayl is middle of array - right o=
f left child arraym is middle + 1 of the array - left of right child arrayj=
l m k_____________________________________| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| =
10| 11|=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=
=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=93=E2=80=9D;
std:: cout << =E2=80=9C=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=E2=80=9D << std::endl;
std::cout << =E2=80=9C|| NEW NODE |||| j =3D>=
=E2=80=9D << j << " " << =E2=80=9Ck=3D>=E2=
=80=9D << k << " ||" << std::endl; std:: cout &=
lt;< =E2=80=9C=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=E2=80=9D << std::endl; int length =3D k - j; int l =3D j + =
(length / 2); int m =3D l + 1;
if(j =3D=3D k ) { std::cout << =E2=80=9C*********RETURN**********=
=E2=80=9D << std::endl; return; }
std::cout << =E2=80=9CLeft Branch: j =3D>=E2=80=9D << j &=
lt;< =E2=80=9C=E2=80=9D << =E2=80=9Cl =3D>=E2=80=9D << l =
<< std::endl; std::cout << =E2=80=9CRight Branch: m =3D>=E2=
=80=9D << m << =E2=80=9C=E2=80=9D << =E2=80=9Ck =3D>=
=E2=80=9D << k << std::endl; std::cout << std::endl; std:=
:cout << std::endl; std::cout << std::endl; std::cout << =
=E2=80=9C***Enter left : j =3D>" << j << =E2=80=9C=E2=
=80=9D << =E2=80=9Cl =3D>=E2=80=9D << l << =E2=80=9C**=
*" << std::endl; std::thread sinetro( track, j, l, parent ) ; st=
d::cout << =E2=80=9CReturned to Node j =3D>=E2=80=9D << j &l=
t;< =E2=80=9C=3D>=E2=80=9D << k << " From the left&q=
uot; << std::endl; std::cout << std::endl;
std::cout << =E2=80=9C***Enter right : m =3D>" << m =
<< =E2=80=9C=E2=80=9D << =E2=80=9Ck =3D>=E2=80=9D << k=
<< std::endl; std::thread dextro( track, m, k, parent); std::cout &l=
t;< =E2=80=9CReturned to Node j =3D>=E2=80=9D << j << =E2=
=80=9C=3D>=E2=80=9D << k << " From the right" <=
< std::endl; std::cout << =E2=80=9C**********MERGE**********=E2=80=
=9D << std::endl; sinetro.join(); dextro.join(); merge(j,l,m,k, paren=
t);
}
}//end namespace / =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =
Filename: main.cpp Description: merge sort main Versi=
on: 1.0 * Created: 11/09/2016 09:33:20 AM * Revision: none * Compiler: gcc =
Author: Ruben Safir * Company: NYLXS =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D */ #include #include =E2=80=9Cmsort.h=E2=80=9D #=
include #include #include #define SIZE 10000
int main(int argv, char **argc) { using namespace std;
int data =3D new int[SIZE]; // int cache =3D new int[SIZE]; str=
uct timespec now; clock_gettime(CLOCK_REALTIME, &now); long seed =3D no=
w.tv_nsec; srand( (unsigned int) seed); for(int i =3D 0; i < SIZE; i++){=
data[i] =3D rand() % 1000000; cout << data[i] << =E2=80=9C=E2=
=80=9D; } cout << endl;
cout << =E2=80=9C_________________________________________________=
_______=E2=80=9D << endl;
cout << =E2=80=9C***Size of Array " << SIZE << =
=E2=80=9C****=E2=80=9D << endl; cout << =E2=80=9C______________=
______=E2=80=9D << endl; cout << =E2=80=9CArray Before=E2=80=9D=
<< endl; for(int i =3D 0; i < SIZE; i++){ cout << =E2=80=9C=
=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=93=E2=80=9D &l=
t;< endl; cout << =E2=80=9C|=E2=80=9D << i << " =
=3D=3D> " << data[i] << =E2=80=9C|=E2=80=9D << en=
dl; } cout << =E2=80=9C=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=
=E2=80=94=E2=80=93=E2=80=9D << endl; cout << =E2=80=9C=E2=80=94=
=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=93=E2=80=9D << en=
dl;
merge::track(0, SIZE - 1, data);
cout << "____________________" << endl;
cout << "Array AFTER" << endl;
for(int i =3D 0; i < SIZE; i++){ cout << =E2=80=9C=E2=80=94=E2=
=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=93=E2=80=9D << endl;=
cout << =E2=80=9C|=E2=80=9D << i << " =3D=3D> &q=
uot; << data[i] << =E2=80=9C|=E2=80=9D << endl; } cout &l=
t;< =E2=80=9C=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=
=80=93=E2=80=9D << endl; cout << =E2=80=9C=E2=80=94=E2=80=94=E2=
=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=93=E2=80=9D << endl;
return 0;
}
Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/lis=
tinfo/learn