示例#1
0
void qsortads(PPD &ppd,VArrayL &ndx,ulong left0, ulong right0) {
   struct Range {
      ulong l,r;
   };
   Range stack[35];  // should be enough for 2**35 records! See Data Structures
                     // book page 259 or (Hoare, 1962)
   int sptr = 0;     // stack pointer

   cout << "   QuickSorting records alphabetically... ";
   stack[sptr].l=left0;     // push initial values into stack
   stack[sptr++].r=right0;

   while (sptr) { // while stack is not empty
      ulong left=stack[--sptr].l;  // retrieve next range from stack
		ulong right=stack[sptr].r;
      cout.width(7);
      cout << (long)(right-left) << "\x8\x8\x8\x8\x8\x8\x8";
// here comes the median of three qsort method.  Figure 6.40 on page 258 of
// Data Structures Book
      swap(ndx,(left+right)/2,left+1,Amount); // finding the median of three
      if (cmpa(ppd,ndx(left+1),ndx(right))>0)
         swap(ndx,left+1,right,Amount);
#ifdef DEBUG
      if (cmpa(ppd,ndx(left+1),ndx(right))>0) {
            cout << "e1!\n";
            exit(1);
         }
#endif
      if (cmpa(ppd,ndx(left),ndx(right))>0)
         swap(ndx,left,right);
#ifdef DEBUG
      if (cmpa(ppd,ndx(left),ndx(right))>0) {
            cout << "e2!\n";
            cout << cmpa(ppd,ndx(left),ndx(right));
            exit(1);
         }
#endif
      if (cmpa(ppd,ndx(left+1),ndx(left))>0)
         swap(ndx,left+1,left);
#ifdef DEBUG
      if (cmpa(ppd,ndx(left+1),ndx(left))>0) {
            cout << "e3!\n";
            exit(1);
			}
      if (cmpa(ppd,ndx(left+1),ndx(left))>0 ||
         cmpa(ppd,ndx(left),ndx(right))>0 ||
         cmpa(ppd,ndx(left+1),ndx(right))>0) {
            cout << "ajsghkasjfgkasjfaskj!\n";
            exit(1);
         }
#endif
      ulong j=left+1, k=right;
      ulong nleft=ndx(left);
      do {
         do { j++;
#ifdef DEBUG
            if (j>right) {
               cout << cmpa(ppd,ndx(left+1),ndx(right));
         }
#endif
         } while (cmpa(ppd,nleft,ndx(j))>0);
         do { k--;
#ifdef DEBUG
            if (k==left) {
               cout << cmpa(ppd,nleft,ndx(left+1));
            }
#endif
         } while (cmpa(ppd,nleft,ndx(k))<0);
         if (j<k)
            swap(ndx,j,k);
      } while (j<=k);
      swap(ndx,left,k);
		if (k-left>right-k) {
         if (k-left>10) {
            stack[sptr].l=left;
            stack[sptr++].r=k-1;
         }
         if (right-k>10) {
            stack[sptr].l=k+1;
            stack[sptr++].r=right;
         }
      } else {
         if (right-k>10) {
            stack[sptr].l=k+1;
            stack[sptr++].r=right;
         }
         if (k-left>10) {
            stack[sptr].l=left;
            stack[sptr++].r=k-1;
         }
      }  // new values are in stack
      if (sptr>34) {
         cout << "Stack overflow error!\n";
         exit(1);
      }
   } // while

   cout << "Almost done.  \n";

// now insertsort on the remaining stuff...
   cout << "   Finishing sorting with InsertSort... ";
	for (long k=right0-1; k>=0; k--) {
      ulong save=ndx(k);   // save k-th element
      for (long j=k+1; (j<=right0) && cmpa(ppd,save,ndx(j))>0; j++)
         ndx[j-1]=ndx(j);
      ndx[j-1] = save;
      if (k%33==0) {
         cout.width(7);
         cout << k << "\x8\x8\x8\x8\x8\x8\x8";
      }
   }
   cout << "Done sorting.  \n";
}
示例#2
0
void cmp(const cv::Mat& m, const T t[6])
{
    cmpa(m.at<T>(0, 0), t[0]); cmpa(m.at<T>(0, 1), t[1]);
    cmpa(m.at<T>(1, 0), t[2]); cmpa(m.at<T>(1, 1), t[3]);
    cmpa(m.at<T>(2, 0), t[4]); cmpa(m.at<T>(2, 1), t[5]);
}