Diploma Thesis Percolation Simulation
C++ Sourcecode Documentation

www.AndreasKrueger.de/thesis/code

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

quicksort.h

Go to the documentation of this file.
00001 // quicksort.h
00002 // Quicksort-Template       
00003 // laut Rehmann (sein Zitat: cf. K&R, p.87)
00004 // implemented 2.5.2000
00005 // version 2.0, last change 2.5.2000
00006 
00007 template <class T>
00008 inline void swap1(T& x, T& y) {
00009         if (&x != &y){
00010                 T tmp = x; 
00011                 x = y; y = tmp;
00012         }
00013 }
00014 
00015 template <class T>                                                                             
00016 void quicksort1(T v, NUMBER left, NUMBER right) {   
00017   if (left>=right) return;
00018 
00019   swap1(v[left],v[(left+right)/2]);
00020   int last=left;
00021 
00022   for (NUMBER i = left+1; i <= right; i++) {
00023           if ((v[i].c) < (v[left].c)){
00024                   swap1(v[++last], v[i]);
00025           };
00026   }
00027   swap1(v[left],v[last]);
00028   quicksort1(v, left, last-1); 
00029   quicksort1(v,last+1, right);
00030 }
00031 
00032 //compare two sphere-numbers by the coordinate of the sphere
00033 
00034 bool greatercoord (sphere *spheres, NUMBER sphno1, NUMBER sphno2,int coordinate) {
00035                 return (spheres[sphno1].c.coord(coordinate) > spheres[sphno2].c.coord(coordinate));
00036 }
00037 
00038 // slow sort:
00039 
00040 bool sort_by_coordinate_slow (sphere* spheres, 
00041                                                           NUMLIST &source, 
00042                                                           NUMLIST &target, 
00043                                                           int direction) {
00044         if (source.size()==0 || &source==&target) return false;
00045         NUMLIST::iterator s1,s2;
00046         int coordinate=abs(direction);
00047         target.insert(target.end(),source.begin(),source.end());
00048         bool swapyes;
00049         NUMBER temp1,temp2;
00050 
00051         for (s1=target.begin();s1!=target.end();s1++){
00052                 for (s2=s1;s2!=target.end();s2++) {
00053                         if (direction>0) swapyes=greatercoord(spheres, *s1,*s2,coordinate);
00054                          else swapyes=!greatercoord(spheres, *s1,*s2,coordinate);
00055                         if (swapyes){
00056                                 temp1=*s1;
00057                                 temp2=*s2;
00058                                 s2=target.erase(s2);
00059                                 s1=target.erase(s1);
00060                                 s1=target.insert(s1,temp2);
00061                                 s2=target.insert(s2,temp1);
00062                         }
00063                 }
00064         }
00065         return true;
00066 }
00067 
00068 
00069 // using quicksort: 
00070 
00071 void swap2(NUMBER &x, NUMBER &y) {
00072         if (&x != &y){
00073                 NUMBER tmp = x; 
00074                 x = y; y = tmp;
00075         }
00076 }
00077 
00078 // void quicksort2(T v, NUMBER left, NUMBER right) {   
00079 
00080 void quicksort_by_coordinate(sphere* spheres, NUMBER* sphno , NUMBER left, NUMBER right, int coordinate) {   
00081 
00082   if (left>=right) return;
00083 
00084   swap2(sphno[left],sphno[(left+right)/2]);
00085 
00086   NUMBER last=left;
00087 
00088   for (NUMBER i = left+1; i <= right; i++) {
00089           // if (   (v[i].c) < (v[left].c)   ){
00090           if ( ! greatercoord(spheres, sphno[i],sphno[left],coordinate)   ){
00091                   swap2(sphno[++last], sphno[i]);
00092           };
00093   }
00094   swap2(sphno[left],sphno[last]);
00095   quicksort_by_coordinate(spheres,sphno,left,  last-1,coordinate); 
00096   quicksort_by_coordinate(spheres,sphno,last+1, right,coordinate);
00097 }
00098 
00099 
00100 bool sort_by_coordinate_quick (sphere* spheres, 
00101                                                                                  NUMBER* temp_sphno, 
00102                                                                                  NUMLIST &source, 
00103                                                                                  NUMLIST &target, 
00104                                                                                  int direction) {
00105 
00106         if (source.size()==0 || &source==&target || direction== 0) return false;
00107 
00108         NUMLIST::iterator s1;
00109         NUMBER index=0;
00110         for (s1=source.begin();s1!=source.end();s1++) {
00111                 temp_sphno[index]=*s1;
00112                 index++;
00113         }
00114 
00115         int coordinate=abs(direction);
00116 
00117         quicksort_by_coordinate (spheres, temp_sphno, 0,index-1, coordinate);
00118 
00119         if (direction<0) {
00120                 for (NUMBER i=0;i<index;i++) {
00121                         target.push_front(temp_sphno[i]);
00122                 }
00123         }
00124         else {
00125                 for (NUMBER i=0;i<index;i++) {
00126                         target.push_back(temp_sphno[i]);
00127                 }
00128         }
00129 
00130         return true;
00131 }
00132 




Diploma Thesis Sourcecode Documentation
check out the text and the executable binaries

www.AndreasKrueger.de/thesis/code