void quicksort(int m, int n) { int i, j, k; leaf_t key; if (m < n) { k = choose_pivot(m, n); swap(m, k); key = leafs[m]; i = m + 1; j = n; while (i <= j) { while ((i <= n) && (hilbert_ieee_cmp(3, leafs[i].pos, key.pos) <= 0)) i++; while ((j >= m) && (hilbert_ieee_cmp(3, leafs[j].pos, key.pos) > 0)) j--; if (i < j) swap(i, j); } // swap two elements swap(m, j); // recursively sort the lesser list quicksort(m, j - 1); quicksort(j + 1, n); } }
int sort (process_sorted_output_fn process_sorted_output) { std::cerr << "\nstart sorting"; int count=0; if (buffer.empty()==false) makeBucket(); int Bsize = buckets.size(); buffer.reserve(Bsize); int eofCount=0; int outindex=0; for (int i=0; i<Bsize; i++) { Vertex_info* temp = new Vertex_info(); if (temp->load(*(buckets.at(i)))==false) { delete buckets.at(i); buckets.at(i)=NULL; delete temp; temp=NULL; eofCount++; } buffer.push_back(temp); } while (eofCount<Bsize) { // set pointer to first item for (int i=0; i<Bsize; i++) if (buckets.at(i)!=NULL) { outindex=i; break; } if (outindex==Bsize) break; // find min item for (int i=0; i<Bsize; i++) if (buckets.at(i)!=NULL) { double d1[3],d2[3]; d1[0] = buffer.at(i)->vi.coords[0]; d1[1] = buffer.at(i)->vi.coords[1]; d1[2] = buffer.at(i)->vi.coords[2]; d2[0] = buffer.at(outindex)->vi.coords[0]; d2[1] = buffer.at(outindex)->vi.coords[1]; d2[2] = buffer.at(outindex)->vi.coords[2]; if (hilbert_ieee_cmp(3,d1,d2)>=0) outindex=i; } //Sorted Output (process_sorted_output) (buffer.at(outindex)->vid, buffer.at(outindex)->vi); #ifdef VERBOSE std::cerr << buffer.at(outindex)->vi.coords[0] << " " << buffer.at(outindex)->vi.coords[1] << " " << buffer.at(outindex)->vi.coords[2]<< std::endl; #endif if (count%10000000==0 &&count>0) std::cout << "\n Points Sorted: " << count; count++; delete buffer.at(outindex); Vertex_info* temp = new Vertex_info(); if (temp->load(*(buckets.at(outindex)))==false) { delete buckets.at(outindex); buckets.at(outindex)=NULL; delete temp; temp=NULL; eofCount++; } buffer.at(outindex) = temp; } return count; }