iSAMrx72's 思い付きBlog

今、新しいアカウントではじめました、への投稿となります。https://blog.goo.ne.jp/isamrx72

C#興味のある方は、居ないでしょうね。

2012-11-24 01:27:25 | プログラミング言語

ソート関連で2題です。前のは年齢をキーにソート。後半は年齢で無く、名前の読みに

変更するとどうなるかです。分類法は三つあります。どれも試しました。話題の無いときの

繋ぎです。w

using System;

namespace Sample
{

 

    public class Entry : IComparable<Entry>
    {
          public int age;
          public string name;

    public Entry(int age, string name)
        {
            this.age = age;
            this.name = name;
        }

    int IComparable<Entry>.CompareTo(Entry other)
        {
            return this.age.CompareTo(other.age);
        }
       


       
    public static void BubbleSort<T>(T[] a)
          where T : IComparable<T>
        {
              int n = a.Length;
              for (int i = 0; i < n - 1; i++)
                for (int j = n - 1; j > i; j--)
                      if (a[j].CompareTo(a[j - 1]) < 0)
                        Swap(ref a[j], ref a[j - 1]);
        }
       
    public static void ShellSort<T>(T[] a)
             where T : IComparable<T>
        {
              int n = a.Length;
              int h;
              for (h = 1; h < n / 9; h = h * 3 + 1) ;
              for (; h > 0; h /= 3)
                for (int i = h; i < n; i++)
                      for (int j = i; j >= h && a[j - h].CompareTo(a[j]) > 0; j -= h)
                        Swap(ref a[j], ref a[j - h]);
        }
       
    public static void Swap<T>(ref T a, ref T b)
        {
              T c = a; a = b; b = c;
        }

   

    static void Main()
        {
                Entry[] list = new Entry[]{
              new Entry(10, "a"),
              new Entry(11, "b"),
              new Entry(12, "c"),
              new Entry(11, "d"),
              new Entry(13, "e"),
              new Entry(10, "f"),
              new Entry(12, "g"),
              new Entry(14, "h")};
         
              //Array.Sort(list);
              //BubbleSort( list );
              ShellSort( list );
             
            foreach (Entry entry in list)
            {
                Console.Write("{0}, {1}\n", entry.age, entry.name);
            }

        }
       
    }//Entry
 
}//namespace Sample

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;

namespace Sample
{

 

    public class Entry : IComparable<Entry>
    {
          public string yomi;
          public string name;

    public Entry(string yomi, string name)
        {
            this.yomi = yomi;
            this.name = name;
        }

    int IComparable<Entry>.CompareTo(Entry other)
        {
            return this.yomi.CompareTo(other.yomi);
        }
       


       
    public static void BubbleSort<T>(T[] a)
          where T : IComparable<T>
        {
              int n = a.Length;
              for (int i = 0; i < n - 1; i++)
                for (int j = n - 1; j > i; j--)
                      if (a[j].CompareTo(a[j - 1]) < 0)
                        Swap(ref a[j], ref a[j - 1]);
        }
       
    public static void ShellSort<T>(T[] a)
             where T : IComparable<T>
        {
              int n = a.Length;
              int h;
              for (h = 1; h < n / 9; h = h * 3 + 1) ;
              for (; h > 0; h /= 3)
                for (int i = h; i < n; i++)
                      for (int j = i; j >= h && a[j - h].CompareTo(a[j]) > 0; j -= h)
                        Swap(ref a[j], ref a[j - h]);
        }
       
    public static void Swap<T>(ref T a, ref T b)
        {
              T c = a; a = b; b = c;
        }

   

    static void Main()
        {
                Entry[] list = new Entry[]{
              new Entry("スズキ", "鈴木"),
              new Entry("サトウ", "佐藤"),
              new Entry("イトウ", "伊藤"),
              new Entry("タカハシ", "高橋"),
              new Entry("スガワラ", "菅原"),
              new Entry("クマガイ", "熊谷"),
              new Entry("チバ", "千葉"),
              new Entry("タカナシ", "小鳥遊")};
         
              //Array.Sort(list);
              //BubbleSort( list );
              ShellSort( list );
             
            foreach (Entry entry in list)
            {
                Console.Write("{0}, {1}\n", entry.yomi, entry.name);
            }

        }
       
    }//Entry
 
}//namespace Sample


最新の画像もっと見る

コメントを投稿