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

C:/temp/src/j2k/DataType/Sort/Sort.hpp File Reference

#include "Const.hpp"
#include "MaxHeap.hpp"

Go to the source code of this file.

Compounds

struct  QEntry

Defines

#define __J2K__Sort_HPP__
#define BITS   6

Functions

void InsertionSort (Elem *array, int n)
void BubbleSort (Elem *array, int n)
void SelectionSort (Elem *array, int n)
void ShellSort (Elem *array, int n)
void InsertionSort (Elem *array, int n, int incr)
void MergeSort (Elem *array, int Size)
void HeapSort (Elem *array, int Size)
void RadixSort (Elem *array, long Size)
void QuickSort (Elem *array, int Size)
void BrightQSort (Elem *array, ulong Size)
void OldQuickSort (Elem *array, ulong Size)
void MergeSort (Elem *array, Elem *temp, int left, int right)
void swap (Elem *A, int i, int j)

Variables

long POW2 [17]


Define Documentation

#define BITS   6
 

Definition at line 16 of file Sort.hpp.

#define __J2K__Sort_HPP__
 

Definition at line 6 of file Sort.hpp.


Function Documentation

void BrightQSort Elem   array,
ulong    Size
 

Definition at line 99 of file OldQsort.cpp.

00100 {
00101    ULONG* ptr = new ULONG[20];
00102    int   l = 0;
00103    int   r = 0;
00104    register int i = 0;
00105    ULONG k = 0;
00106 
00107    int   Trigger;
00108 
00109    Elem  T;
00110 
00111    int Test   = 20;
00112    int Test34 = Test * 3 / 4;     // 75% Ratio to be declare sorted
00113 
00114    int n  = Size / Test;
00115    int n2 = (int)floor(n / 2);
00116 
00117    for( i = 1; i <= Test; i++ )
00118    {
00119       ptr[i] = (ulong)floor( n * i );
00120    }
00121 
00122    for( i = 2; i <= Test; i++ )
00123    {
00124      T = array[ ptr[i] ] - array[ ptr[i - 1] - n2 ];
00125 
00126        if ( T < 0 )
00127        {
00128             l++;
00129        } 
00130        else 
00131        {
00132             r++;
00133        }
00134 
00135        printf( "[%d, %d, %d]", T, l, r );
00136    }
00137 
00138    printf( "[%d, %d, %d]", T, l, r );
00139 
00140    Trigger = r - l;
00141 
00142    printf( "%d", Trigger );
00143 
00144 
00145    if ( Trigger > Test34 )
00146    {
00147      printf( "INC" );
00148      InsertionSort( array, Size );
00149      return;
00150    }
00151 
00152 
00153    if ( Trigger < (-1 * Test34) )
00154    {
00155       printf("DEC");
00156   
00157       // Reverse the array
00158       for( k = 1; k <= ( Size / 2 + 1 ); k++ )
00159       {
00160          swap( array, k, Size - k + 1 );
00161       }
00162 
00163       for( k = 1; k <= Size; k++ )
00164       {
00165          printf("%d", array[k] );
00166       }
00167 
00168       InsertionSort( array, Size );
00169       return;
00170    }
00171 
00172 
00173    printf( "RND" );
00174 
00175    OldQuickSort( array, Size );
00176 
00177 } // End of BrightQSort()

void BubbleSort Elem   array,
int    n
 

Definition at line 26 of file Sort.cpp.

00027 {
00028   n--;  // n-1 optimization
00029 
00030   // Bubble up i'th record
00031   for( register   int i = 0; i < n; i++ )
00032     for( register int j = n; j > i; j-- )
00033       if ( array[j] < array[j-1] )
00034       {
00035         swap( array, j, j-1 );
00036       }
00037 }

void HeapSort Elem   array,
int    Size
 

Definition at line 144 of file Sort.cpp.

00145 {
00146   MaxHeap* heap = new MaxHeap( array, Size, Size );   // Build the maxheap
00147 
00148   for(int i = 0; i < Size; i++)     // Now sort by removing 
00149   {
00150     heap->removemax();               // The max value placed at end of heap
00151   }
00152   
00153   delete heap;
00154 }

void InsertionSort Elem   array,
int    n,
int    incr
 

Definition at line 59 of file Sort.cpp.

Referenced by BrightQSort(), QuickSort(), and ShellSort().

00060 {
00061   for( register int i = incr; i < n; i += incr )
00062     for( register int j = i; 
00063           ( j >= incr ) && ( array[j] < array[j-incr] ); 
00064           j -= incr )
00065     {
00066       swap( array, j, j-incr );
00067     }
00068 }

void InsertionSort Elem   array,
int    n
 

void MergeSort Elem   array,
Elem   temp,
int    left,
int    right
 

Definition at line 93 of file Sort.cpp.

Referenced by MergeSort().

00094 {
00095   register int i, j, k;
00096   register int mid = (left+right) / 2;
00097 
00098   if (left == right) return;
00099 
00100   if ( (mid-left) < MSTHRESHOLD ) 
00101   {
00102     SelectionSort( &array[left], mid-left);
00103   } 
00104   else 
00105   {
00106     MergeSort( array, temp, left, mid );   // MergeSort first half
00107   }
00108 
00109   if ( (right-mid-1) < MSTHRESHOLD ) 
00110   {
00111     SelectionSort( &array[mid+1], right-mid-1 );
00112   } 
00113   else 
00114   {
00115     MergeSort(array, temp, mid+1, right);  // MergeSort second half
00116   }
00117 
00118   // Do the merge operation.  First, copy 2 halves to temp.
00119 
00120   for( i = mid; i >= left; i--) 
00121   {
00122     temp[i] = array[i];
00123   }
00124 
00125   for( j = 1; j <= right-mid; j++) 
00126   {
00127     temp[ right - j + 1 ] = array[ j + mid ];
00128   }
00129 
00130   // Merge sublists back to array
00131   for( i = left, j = right, k = left; k <= right; k++) 
00132   {
00133      if ( temp[ i ] < temp[ j ] ) 
00134      {
00135        array[ k ] = temp[ i++ ];
00136      } 
00137      else 
00138      {
00139        array[ k ] = temp[ j-- ];
00140      }
00141   } 
00142 }

void MergeSort Elem   array,
int    Size
 

void OldQuickSort Elem   array,
ulong    Size
 

Definition at line 16 of file OldQsort.cpp.

Referenced by BrightQSort().

00017 {                                 
00018   struct QEntry QStack[100];        // Use to store variables for recursivity
00019 
00020   long Top = 0;                     // Top & Bottom ptr
00021   long Bottom = 0;
00022 
00023   long a = 0;                       // Inner Left & Right working ptr
00024   long b = Size-1;                       
00025  
00026   Elem z;                           // Temp location for swapping stuff
00027  
00028   QStack[0].Left  = a;              // Define the absolute left ptr 
00029   QStack[0].Right = b;              // Define the absolute right ptr
00030 
00031   long x = 1;                       // Normal Middle ptr
00032 
00033   while ( x > 0 )
00034   {
00035     x--;
00036     a = QStack[x].Left;             // Store Left and Right Stack value
00037     b = QStack[x].Right;
00038 
00039     z = array[a];                   // Give me some z value
00040    
00041     Top    = a;                     // Fix Top and Bottom range
00042     Bottom = b + 1;
00043 
00044 QSBottom: // Address #1  -- Let's play with Bottom !
00045 
00046     Bottom--;
00047 
00048     if ( Bottom == Top ) goto QSz;  // If there is no middle than fix z
00049 
00050     if ( z <= array[Bottom] )       // If z is smaller than bottom-- and retry
00051     {    
00052       goto QSBottom;
00053     } 
00054     else 
00055     { 
00056       array[Top] = array[Bottom];
00057     }
00058 
00059 QSTop:  // Address #2  -- Let's play with Top !
00060 
00061     Top++;
00062 
00063     if ( Bottom == Top) goto QSz;    // If there is no middle than fix z
00064 
00065     if ( z >= array[ Top ] )         // If z is bigger than top++ and retry
00066     {       
00067       goto QSTop;
00068     } 
00069     else 
00070     {
00071       array[ Bottom ] = array[ Top ];
00072       goto QSBottom;
00073     }
00074 
00075 QSz:  // Address #3  -- Let's fix z and continue.
00076  
00077     array[Top] = z;
00078 
00079     if ( (b - Top) >= 2 )
00080     {
00081       QStack[x].Left  = Top + 1;
00082       QStack[x].Right = b;
00083       x++;
00084     }
00085 
00086     if ( (Bottom - a) >= 2 )
00087     {
00088       QStack[x].Left  = a;
00089       QStack[x].Right = Bottom - 1;
00090       x++;
00091     }
00092   }
00093 }

void QuickSort Elem   array,
int    Size
 

Definition at line 11 of file Qsort4.cpp.

00012 {
00013   int i = 0;
00014   int j = Size - 1;
00015   int listsize = Size;
00016 
00017   struct QEntry Stack[100];
00018 
00019   int top = 0;
00020   int pivot;
00021   int pivotindex, l, r;
00022 
00023   Stack[top].Left  = i;
00024   Stack[top].Right = j;
00025 
00026   while ( top >= 0 ) 
00027   {
00028     // Pop Stack
00029     i = Stack[top].Left;
00030     j = Stack[top--].Right;
00031 
00032     // Findpivot
00033     pivotindex = (i+j)/2;
00034     pivot = array[pivotindex];         
00035     swap( array, pivotindex, j );      // Stick pivot at end
00036 
00037     // Partition
00038     l = i-1;
00039     r = j;
00040 
00041     do {
00042       while (       array[++l] < pivot );
00043       while (r  &&  array[--r] > pivot );
00044 
00045       swap(array, l, r);
00046 
00047     } while (l < r);
00048 
00049     swap(array, l, r);                 // Undo final swap
00050     swap(array, l, j);                 // Put pivot value in place
00051 
00052     // Load up Stack
00053     if ( (l-i) > QSTHRESHOLD )         // Left partition
00054     {
00055       Stack[++top].Left   = i;
00056       Stack[  top].Right  = l-1;
00057     }
00058 
00059     if ( (j-l) > QSTHRESHOLD )         // Right partition
00060     {
00061       Stack[++top].Left   = l+1;
00062       Stack[  top].Right  = j;
00063     }
00064   }
00065 
00066   InsertionSort( array, listsize );    // Final Insertion Sort
00067 }

void RadixSort Elem   array,
long    Size
 

Definition at line 156 of file Sort.cpp.

00157 {
00158   // Count[i] stores number of records in bin[i]
00159 
00160   Elem* count = new Elem[ Size ];
00161   Elem* A     = array;
00162   Elem* B     = new Elem[ Size ];
00163 
00164   register long n    = Size;
00165   register int  k    = 16 / BITS;
00166   register int  r    = POW2[ BITS ];
00167 
00168   register int  i    = 0;
00169   register int  j    = 0;
00170   register int  rtok = 1;
00171 
00172   for( i = 0, rtok = 1; i < k; i++, rtok *= r)    // For k digits
00173   {
00174     for( j = 0; j < r; j++)                       // Initialize count
00175     {
00176        count[ j ] = 0;
00177     }
00178 
00179     // Count the number of records for each bin on this pass
00180     for( j = 0; j < n; j++ ) 
00181     {
00182        count[ ( A[j] / rtok ) % r ]++;
00183     }
00184 
00185     // Index B: count[j] will be index for last slot of bin j.
00186     for( j = 1; j < r; j++ ) 
00187     {
00188        count[ j ] = count[ j - 1 ] + count[ j ];
00189     }
00190 
00191     // Put records into bins working from bottom of each bin.
00192     // Since bins fill from bottom, j counts downwards
00193     for( j = n-1; j >= 0; j-- ) 
00194      {
00195        B[ --count[ ( A[j] / rtok ) % r ] ] = A[j];
00196     }
00197 
00198     for( j = 0; j < n; j++ )    // Copy B back to A
00199     {
00200        A[ j ] = B[ j ];         
00201     }
00202   }
00203 
00204   if ( B != NULL ) 
00205   {
00206       delete[] B;
00207   }
00208 
00209   if ( count != NULL ) 
00210   {
00211       delete[] count;
00212   }
00213 }

void SelectionSort Elem   array,
int    n
 

Definition at line 40 of file Sort.cpp.

Referenced by MergeSort().

00041 {
00042   n--;  // n-1 optimization
00043 
00044   // Select i'th record
00045   for( register int i = 0; i < n; i++ ) 
00046   {
00047     register int lowindex = i;           // Remember its index
00048     for(int j = n; j > i; j--)          // Find the least value
00049       if ( array[j] < array[lowindex] )
00050       {
00051          lowindex = j;                   // Put it in place
00052       }
00053 
00054     swap( array, i, lowindex );
00055   }
00056 }

void ShellSort Elem   array,
int    n
 

Definition at line 71 of file Sort.cpp.

00072 {
00073   for(   register int i = n / 2; i > 2; i /= 2 )   // For each increment
00074     for( register int j = 0;     j < i; j++ )      // Sort each sublist
00075     {
00076       InsertionSort( &array[j], n-j, i);
00077     }
00078 
00079   InsertionSort( array, n );
00080 }

void swap Elem   A,
int    i,
int    j
[inline]
 

Definition at line 40 of file Sort.hpp.

Referenced by BrightQSort(), BubbleSort(), InsertionSort(), smatrix::PivotMap(), QuickSort(), SelectionSort(), smatrix::Transpose(), MaxHeap::insert(), MaxHeap::remove(), MaxHeap::removemax(), and MaxHeap::siftdown().

00041 {
00042   Elem temp = A[i];
00043   A[i] = A[j];
00044   A[j] = temp;
00045 }


Variable Documentation

long POW2[17] [static]
 

Initial value:

 {   1,    2,     4,     8,    16,   
                                32,    64,   128,   256,  
                               512,  1024,  2048,  4096, 
                              8192, 16384, 32768, 65536  }

Definition at line 11 of file Sort.hpp.


Generated on Sun Oct 14 18:47:04 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001