I am making a program that has a function to sort names alphabetically, I easily used Array.Sort() and it worked but I need the algorithm of the sorting function to help me understand the function more
-
The sorting method you're describing is based off the order of the alphabet. Think, do you have any way to compare the different letters? once you do, its a simple sorting algorithm.A. Abramov– A. Abramov2016-07-10 19:04:04 +00:00Commented Jul 10, 2016 at 19:04
-
I am not sure which methodology is used. But one that I know of is described here. I am not saying it is the most efficient : en.m.wikipedia.org/wiki/Bubble_sortAndrew Truckle– Andrew Truckle2016-07-10 19:08:51 +00:00Commented Jul 10, 2016 at 19:08
-
2This smells like homework to me: How to ask homework questions may offer some help... the most important bit being Make a good faith attempt to solve the problem yourself firstTemporalWolf– TemporalWolf2016-07-10 19:12:28 +00:00Commented Jul 10, 2016 at 19:12
-
@TemporalWolf I googled it and all but I didn't find what I was looking for and I am still in high school and it's summer actually so this is literally for the sake of learning.Yosra– Yosra2016-07-10 19:45:45 +00:00Commented Jul 10, 2016 at 19:45
Add a comment
|
1 Answer
Here is the Array.cs: http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/clr/src/BCL/System/Array@cs/2/Array@cs There is the Sort Method
They use the QuickSort -they check for exceptions, and if everything is OK, they summon this:
internal void QuickSort(int left, int right) {
// Can use the much faster jit helpers for array access.
do {
int i = left;
int j = right;
// pre-sort the low, middle (pivot), and high values in place.
// this improves performance in the face of already sorted data, or
// data that is made up of multiple sorted runs appended together.
int middle = GetMedian(i, j);
SwapIfGreaterWithItems(i, middle); // swap the low with the mid point
SwapIfGreaterWithItems(i, j); // swap the low with the high
SwapIfGreaterWithItems(middle, j); // swap the middle with the high
Object x = keys[middle];
do {
// Add a try block here to detect IComparers (or their
// underlying IComparables, etc) that are bogus.
try {
while (comparer.Compare(keys[i], x) < 0) i++;
while (comparer.Compare(x, keys[j]) < 0) j--;
}
catch (IndexOutOfRangeException) {
throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", x, x.GetType().Name, comparer));
}
catch (Exception e) {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
}
catch {
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
BCLDebug.Assert(i>=left && j<=right, "(i>=left && j<=right) Sort failed - Is your IComparer bogus?");
if (i > j) break;
if (i < j) {
Object key = keys[i];
keys[i] = keys[j];
keys[j] = key;
if (items != null) {
Object item = items[i];
items[i] = items[j];
items[j] = item;
}
}
i++;
j--;
} while (i <= j);
if (j - left <= right - i) {
if (left < j) QuickSort(left, j);
left = i;
}
else {
if (i < right) QuickSort(i, right);
right = j;
}
} while (left < right);
}
}
More info about it: https://en.wikipedia.org/wiki/Quicksort