00001 #ifndef __J2K__Sort__QSort4_CPP__
00002 #define __J2K__Sort__QSort4_CPP__
00003
00004 #include <j2k/DataType/Const.hpp>
00005 #include <j2k/DataType/Sort/Sort.hpp>
00006
00007
00008
00009 #define QSTHRESHOLD 8 // Suggested by MicroSoft !
00010
00011 void QuickSort( Elem* array, int Size )
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
00029 i = Stack[top].Left;
00030 j = Stack[top--].Right;
00031
00032
00033 pivotindex = (i+j)/2;
00034 pivot = array[pivotindex];
00035 swap( array, pivotindex, j );
00036
00037
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);
00050 swap(array, l, j);
00051
00052
00053 if ( (l-i) > QSTHRESHOLD )
00054 {
00055 Stack[++top].Left = i;
00056 Stack[ top].Right = l-1;
00057 }
00058
00059 if ( (j-l) > QSTHRESHOLD )
00060 {
00061 Stack[++top].Left = l+1;
00062 Stack[ top].Right = j;
00063 }
00064 }
00065
00066 InsertionSort( array, listsize );
00067 }
00068
00069 #endif // End of Qsort4.cpp