Re: The code for "sorting in C sharp considered superior" in five parts for easier download



// ***** Part three of five *****

//
------------------------------------------------------------
// Exchange values on behalf of sortArray__()
//
//
private void sortArray__exchangeValues_
(TYPofArray[] objArray,
int intIndex1,
int intIndex2)
{
TYPofArray objExchange = objArray[intIndex1];
objArray[intIndex1] = objArray[intIndex2];
objArray[intIndex2] = objExchange;
if (exchangeEvent != null)
exchangeEvent(this, intIndex1, intIndex2);
}

//
------------------------------------------------------------
// Sort array using interchange
//
//
private bool sortArray__interchangeSort_(TYPofArray[]
objArray,
int intLeft,
int intRight)
{
if (sortStartEvent != null)
sortStartEvent(this, intLeft, intRight);
int intIndex1;
int intIndex2;
for (intIndex1 = intLeft;
intIndex1 < intRight;
intIndex1++)
for (intIndex2 = intIndex1 + 1;
intIndex2 <= intRight;
intIndex2++)
{
if (compareValues_
(objArray[intIndex1],
objArray[intIndex2],
intIndex1, intIndex2)
>
0)
{
sortArray__exchangeValues_(objArray,
intIndex1,
intIndex2);
}
}
return true;
}

//
------------------------------------------------------------
// Sort array using quickSort
//
//
private bool sortArray__quickSort_(TYPofArray[] objArray,
int intLeft,
int intRight)
{
if (sortStartEvent != null)
sortStartEvent(this, intLeft, intRight);
if (intRight > intLeft)
{
int intPivotNewIndex =
sortArray__quickSort_partition_
(objArray,
intLeft,
intRight,
intLeft);
if (partitionEvent != null)
partitionEvent(this,
objArray[intPivotNewIndex],
intPivotNewIndex);
sortArray__quickSort_(objArray,
intLeft,
intPivotNewIndex - 1);
sortArray__quickSort_(objArray,
intPivotNewIndex + 1,
intRight);
}
return true;
}

//
------------------------------------------------------------
// Partition array on behalf of sort()
//
//
// Pseudo-code (Wikipedia 2008):
//
// function partition(array, left, right, pivotIndex)
// pivotValue := array[pivotIndex]
// swap array[pivotIndex] and array[right] //
Move pivot to end
// storeIndex := left
// for i from left to right // left ≤ i <
right
// if array[i] ≤ pivotValue
// swap array[i] and array[storeIndex]
// storeIndex := storeIndex + 1
// swap array[storeIndex] and array[right] //
Move pivot to its final place
// return storeIndex
//
private int sortArray__quickSort_partition_
(TYPofArray[] objArray,
int intLeft,
int intRight,
int intPivotIndex)
{
TYPofArray objPivotValue = objArray[intPivotIndex];
sortArray__exchangeValues_(objArray,
intPivotIndex,
intRight);
int intStoreIndex = intLeft;
int intIndex1;
for (intIndex1 = intLeft;
intIndex1 < intRight;
intIndex1++)
{
if (compareValues_(objArray[intIndex1],
objPivotValue,
intIndex1,
intPivotIndex)
<
0)
{
sortArray__exchangeValues_(objArray,
intStoreIndex,
intIndex1);
intStoreIndex += 1;
}
}
sortArray__exchangeValues_(objArray,
intStoreIndex,
intRight);
return intStoreIndex;
}

//
------------------------------------------------------------
// Return and assign affiliated Tag
//
//
public object Tag
{
get
{
if (!checkUsability_("Tag get", "Returning null"))
return null;
return USRstate.OBJtag;
}
set
{
if (!checkUsability_("Tag set", "No change made"))
return;
USRstate.OBJtag = null;
}
}

//
------------------------------------------------------------
// Return usability
//
//
public bool Usable
{
get { return USRstate.booUsable; }
}

// ***** Private procedures
***********************************

//
------------------------------------------------------------
// Algorithm to string
//
//
private string algorithm2String_(ENUalgorithm enuAl)
{
string strName = "";
checkAlgorithm_(enuAl, ref strName);
return strName;
}

//
------------------------------------------------------------
// Check algorithm enum
//
//
private bool checkAlgorithm_
(sort<TYPofArray>.ENUalgorithm enuAl)
{
string strDummy = "";
return checkAlgorithm_(enuAl, ref strDummy);
}
private bool checkAlgorithm_
(sort<TYPofArray>.ENUalgorithm enuAl,
ref string strToString)
{
switch (enuAl)
{
case sort<TYPofArray>.ENUalgorithm.bubbleSort:
{
strToString = "bubbleSort";
return true;
}
case
sort<TYPofArray>.ENUalgorithm.interchangeSort:
{
strToString = "interchangeSort";
return true;
}
case sort<TYPofArray>.ENUalgorithm.quickSort:
{
strToString = "quickSort";
return true;
}
default:
{
strToString = "unknown";
return false;
}
}
}

//
------------------------------------------------------------
// Check data type enum
//
//
private bool checkDatatype_
(sort<TYPofArray>.ENUknownDatatype enuDT)
{
string strDummy = "";
return checkDatatype_(enuDT, ref strDummy);
}
private bool checkDatatype_
(sort<TYPofArray>.ENUknownDatatype enuDT,
ref string strToString)
{
switch (enuDT)
{
case
sort<TYPofArray>.ENUknownDatatype.booleanDatatype:
{ strToString = "booleanDatatype"; return
true; }
case
sort<TYPofArray>.ENUknownDatatype.byteDatatype:
{ strToString = "byteDatatype"; return true; }
case
sort<TYPofArray>.ENUknownDatatype.shortDatatype:
{ strToString = "shortDatatype"; return
true; }
case
sort<TYPofArray>.ENUknownDatatype.integerDatatype:
{ strToString = "integerDatatype"; return
true; }
case
sort<TYPofArray>.ENUknownDatatype.longDatatype:
{ strToString = "longDatatype"; return true; }
case
sort<TYPofArray>.ENUknownDatatype.floatDatatype:
{ strToString = "floatDatatype"; return
true; }
case
sort<TYPofArray>.ENUknownDatatype.doubleDatatype:
{ strToString = "doubleDatatype"; return
true; }
case
sort<TYPofArray>.ENUknownDatatype.stringDatatype:
{ strToString = "stringDatatype"; return
true; }
case sort<TYPofArray>.ENUknownDatatype.unknown:
{ strToString = "unknownDatatype"; return
true; }
default:
{ strToString = "invalidDatatype"; return
true; }
}
}

//
------------------------------------------------------------
// Check usability
//
//
private bool checkUsability_(string strProcedure,
string strAction)
{
if (USRstate.booUsable) return true;
return errorHandler_("Class instance is not usable",
strProcedure,
strAction);
}

//
------------------------------------------------------------
// Compare values
//
//
private int compareValues_
(TYPofArray objComparand1,
TYPofArray objComparand2,
int intIndex1,
int intIndex2)
{
// --- Use event
int intCompareResult = 0;
if (compareEvent != null)
compareEvent(this,
objComparand1,
objComparand2,
intIndex1,
intIndex2,
ref intCompareResult);
else
{
// --- Determine data type and if available use
this too
if (USRstate.enuDatatype ==
ENUknownDatatype.unknown
&&
((USRstate.enuDatatype
=
compareValues__determineDatatype_())
==
ENUknownDatatype.unknown)
||
USRstate.enuDatatype ==
ENUknownDatatype.otherDatatype)
{
errorHandler_("Compare event not provided, or
" +
"didn't complete: data type of "
+
"array cannot be determined",
"compareValues_",
"Marking class instance
unusable: " +
"returning 0");
return 0;
}
object objComparand1copy = objComparand1;
object objComparand2copy = objComparand2;
switch (USRstate.enuDatatype)
{
case ENUknownDatatype.booleanDatatype:
return (int)(objComparand1copy)
<
(int)(objComparand2copy)
?
-1
:
(int)(objComparand1copy)
>
(int)(objComparand2copy)
?
1
:
0;
case ENUknownDatatype.byteDatatype:
return (byte)(objComparand1copy)
<
(byte)(objComparand2copy)
?
-1
:
(byte)(objComparand1copy)
>
(byte)(objComparand2copy)
?
1 :
0;
case ENUknownDatatype.doubleDatatype:
return (double)(objComparand1copy)
<
(double)(objComparand2copy)
?
-1
:
(double)(objComparand1copy)
>
(double)(objComparand2copy)
?
1
:
0;
case ENUknownDatatype.integerDatatype:
return (int)(objComparand1copy)
<
(int)(objComparand2copy)
?
-1
:
(int)(objComparand1copy)
>
(int)(objComparand2copy)
?
1 :
0;
case ENUknownDatatype.longDatatype:
return (long)(objComparand1copy)
<
(long)(objComparand2copy)
?
-1
:
(long)(objComparand1copy)
>
(long)(objComparand2copy)
?
1
:
0;
case ENUknownDatatype.shortDatatype:
return (short)(objComparand1copy)
<
(short)(objComparand2copy)
?
-1
:
(short)(objComparand1copy)
>
(short)(objComparand2copy)
?
1 :
0;
case ENUknownDatatype.floatDatatype:
return (float)(objComparand1copy)
<
(float)(objComparand2copy)
?
-1
:
(float)(objComparand1copy)
>
(float)(objComparand2copy)
?
1
:
0;
case ENUknownDatatype.stringDatatype:
return ((string)
(objComparand1copy)).CompareTo
((string)(objComparand1copy));
default:
{
errorHandler_("Programming error",
"compareValues_()",
"Marking object
unusable: " +
"returning zero",
null);
return 0;
}
}
}
return intCompareResult;
}

//
------------------------------------------------------------
// Determine data type on behalf of compareValues_()
//
//
// The data type is determined in this way:
//
//
// * The default() method is used to tell if the
data
// type is one of the value types (bool, byte,
short,
// int, long, float, double or string). If the
data
// type defaults to null, we return our "other"
// data type.
//
// * Then, the ToString() of an instance's GetType
is
// used to find the name of the data type.
//
// * However, we don't trust this ToString()
completely.
// Instead, based on the ToString() GetType, we
make
// certain that the instance's value can be
assigned
// to this data type.
//
//
public ENUknownDatatype
compareValues__determineDatatype_()
{
TYPofArray objTest = default(TYPofArray);
if (objTest == null) return
ENUknownDatatype.otherDatatype;
string strGetType = objTest.GetType().ToString();
try
{
bool booTest = false;
if (strGetType == booTest.GetType().ToString())
{
booTest = (bool)(object)objTest;
return ENUknownDatatype.booleanDatatype;
}
byte bytTest = 0;
if (strGetType == bytTest.GetType().ToString())
{
bytTest = (byte)(object)objTest;
return ENUknownDatatype.byteDatatype;
}
short shrTest = 0;
if (strGetType == shrTest.GetType().ToString())
{
shrTest = (short)(object)objTest;
return ENUknownDatatype.shortDatatype;
}
int intTest = 0;
if (strGetType == intTest.GetType().ToString())
{
intTest = (int)(object)objTest;
return ENUknownDatatype.integerDatatype;
}
long lngTest = 0;
if (strGetType == lngTest.GetType().ToString())
{
lngTest = (long)(object)objTest;
return ENUknownDatatype.longDatatype;
}
float floTest = 0;
if (strGetType == floTest.GetType().ToString())
{
floTest = (float)(object)objTest;
return ENUknownDatatype.floatDatatype;
}
double dblTest = 0;
if (strGetType == dblTest.GetType().ToString())
{
dblTest = (Double)(object)objTest;
return ENUknownDatatype.doubleDatatype;
}
string strTest = "";
if (strGetType == strTest.GetType().ToString())
{
strTest = (string)(object)objTest;
return ENUknownDatatype.stringDatatype;
}
}
catch
{
}
return ENUknownDatatype.unknown;
}

//
------------------------------------------------------------
// Datatype to string
//
//
private string datatype2String_(ENUknownDatatype enuDT)
{
string strName = "";
checkDatatype_(enuDT, ref strName);
return strName;
}
.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... int addFactor(char *strInfix, \ ... int intMaxPolishLength) ... int intIndex1 = intStartIndex; ...
    (comp.lang.c)
  • Re: How to convert Infix notation to postfix notation
    ... int addFactor(char *strInfix, \ ... int intMaxPolishLength) ... int intIndex1 = intStartIndex; ...
    (comp.lang.c)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • problem with vmr9 image compositor - method Draw
    ... I play video with playlist, if i play video about 5x it is ... but so 6round of video playing throw exception int ... private void ConfigureVMR9InWindowlessMode() ... Surface renderTarget = new Surface; ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: using ref keyword performance
    ... // private fields... ... int valueCount = values.Length; ... static void TestWithRefLoop() ...
    (microsoft.public.dotnet.languages.csharp)