Array Sorting

First a little about me. I'm a GSP student at Devry near the end of my first year of a three year degree. I'm working a head of my class and am about four weeks ahead right now. In week 4 we are going to be covering arrays which we covered in my last class but we will and in object arrays. While reading up I came up with an idea for a different way to sort arrays and was very suprised by the results. The main reason for posting here is to see what is wrong with my code and if there are any ways to improve up on it. My results show that on large arrays that my sort way out performs the default C# sort which I have been told is Quick Sort, one of the better sorting methods out there.

Please I would like feedback, negative or postive.

Code Snippet

classFlagSorter

{

// Fields

int _optimizerUnknown = 20;

int _optimizerKnown = 25;

// Accessors

publicint optimizerUnknown

{

get {return _optimizerUnknown; }

set { _optimizerUnknown =value; }

}

publicint optimizerKnown

{

get {return _optimizerKnown; }

set { _optimizerKnown =value; }

}

// Methods

publicvoid sort(int[] arrayToSort)

{

int[] trackerArray;

int largestInt = 0;

int index = arrayToSort.Length - 1;

foreach (int findLargestin arrayToSort)

if (findLargest > largestInt)

largestInt = findLargest;

//exit

//exit

if (largestInt < index * _optimizerUnknown)

{

trackerArray =newint[largestInt + 1];

foreach (int numberin arrayToSort)

trackerArray[number] += 1;

//exit

for (int i = 0; i <= largestInt - 1; i++)

{

while (trackerArray[i] > 0)

{

arrayToSort[index] = i;

trackerArray[i]--;

index--;

}

i++;

}

}

else

Array.Sort(arrayToSort);

}

publicvoid sort(int[] arrayToSort,int maxValue)

{

int[] trackerArray;

int largestInt = maxValue;

int index = arrayToSort.Length - 1;

if (largestInt < index * _optimizerKnown)

{

trackerArray =newint[largestInt + 1];

foreach (int numberin arrayToSort)

trackerArray[number] += 1;

//exit

for (int i = 0; i <= largestInt - 1; i++)

{

while (trackerArray[i] > 0)

{

arrayToSort[index] = i;

trackerArray[i]--;

index--;

}

i++;

}

}

else

Array.Sort(arrayToSort);

}

}

[6399 byte] By [MichaelHobbs] at [2008-1-6]
# 1
Your algorithm is O(n) whereas Quicksort is O(nlog(n)) on average. However, you'll have considerable memory requirements whereas QS has none and it can only work with simple positive integral values (byte, short, int) with limited range whereas QS has no such restriction. You are missing the check on negative values and seem to sort backwards.

Congratulations, I'm sure you'll do well.

nobugz at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

I was aware of the memory issue and when it reaches a given number size I have it switch to quick sort just for that reason. Still it uses a lot of memory. I was considering adding check for negative numbers as I have worked out how to do that, but this is just more for pratice. Now on the backwards sorting that was originally a mistake but when I fixxed it by changing:

int index = arrayToSort.Length - 1; to int index = 0;

and

index--; to index++;

It runs as slow as quick sort or slower which makes no sense to me. I found I could use Array.Reverse() and not lose any time. Could someone explain why that is?

MichaelHobbs at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified