0

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

4
  • 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. Commented 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_sort Commented Jul 10, 2016 at 19:08
  • 2
    This 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 first Commented 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. Commented Jul 10, 2016 at 19:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.