2016-07-10 15 views
0

名前をアルファベット順に並べ替える機能を持つプログラムを作っていますが、Array.Sort()を使いやすくなっていますが、関数の理解に役立つソート関数のアルゴリズムが必要ですアルファベット順の文字列をソートするC#

ここで
+0

説明しているソート方法は、アルファベット順に基づいています。他の文字を比較する方法はありますか?一度すれば、それは単純なソートアルゴリズムです。 –

+0

どの方法論が使用されているかわかりません。しかし、私が知っているものがここに記述されています。私はそれが最も効率的であると言っているわけではありません:https://en.m.wikipedia.org/wiki/Bubble_sort –

+2

これは宿題のようなにおいがします:[宿題の質問方法](http://meta.stackexchange.com/a/10812)いくつかの助けを与えるかもしれない...最も重要なビットがある**あなた自身が最初に問題を解決するために誠意を持って試みる** – TemporalWolf

答えて

1

Array.csです:http://www.dotnetframework.org/default.aspx/DotNET/DotNET/[email protected]/untmp/whidbey/REDBITS/ndp/clr/src/BCL/System/[email protected]/2/[email protected] 方法

彼らは例外のクイックソート-theyチェックを使用して、すべてがOKであれば、彼らはこの召喚並び替えがあります:

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); 
     } 
    } 

詳細情報は、それについて:https://en.wikipedia.org/wiki/Quicksort

+0

すてきな答え、いい仕事です。 –

関連する問題