東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

安定なソート(しょにょに)

安定なソート のコメント:

  添え字配列作って、比較を(上と同じ様に)2段階にしたものを作り、
  添え字で取り出せばいい・・・
  単純にできると思うのですが、オブジェクト指向じゃないからダメですかー?

アリですねぇ、ええ、アリアリです。
んでは早速:

using System;

// 添え字による間接比較
struct IndexedCompare<T> {
  public static T[] data;
  public static Comparison<T> compare;
  // 比較結果が0(等値)のときは添え字を比較する
  public static int Comparison(int x, int y) {
    int result = compare(data[x],data[y]);
    if ( result == 0 ) result = x - y;
    return result;
  }
}

public class Program {

  // 安定なソート
  static public void StableSort<T>(T[] array, Comparison<T> comp) {
    int[] index = new int[array.Length];
    T[] clone = (T[])array.Clone();
    IndexedCompare<T>.compare = comp;
    IndexedCompare<T>.data = clone;
    for ( int i = 0; i < index.Length; ++i ) {
      index[i] = i;
    }
    Array.Sort(index,IndexedCompare<T>.Comparison);
    for ( int i = 0; i < index.Length; ++i ) {
      array[i] = clone[index[i]];
    }
  }

  public static void Main() {
    string[] master = {
      "0", "00", "000", "0000", "00000",
      "1", "11", "111", "1111", "11111",
      "2", "22", "222", "2222", "22222",
      "3", "33", "333", "3333", "33333",
    };
    string[] data;

    data = (string[])master.Clone();
    Array.Sort(data,delegate (string x, string y) { return x.Length - y.Length;});
    Console.WriteLine("Array.Sort");
    foreach ( string item in data ) Console.WriteLine(item);

    data = (string[])master.Clone();
    StableSort(data,delegate (string x, string y) { return x.Length - y.Length;});
    Console.WriteLine("StableSort");
    foreach ( string item in data ) Console.WriteLine(item);
  }
}

投稿日時 : 2008年2月15日 16:43

コメントを追加

# re: 安定なソート(しょにょに) 2008/02/15 16:59 επιστημη

ところで。

.NET Frameworkには安定にソートするメソッドは
用意されてねぇのですか? 教えてえらいひと。

# re: 安定なソート(しょにょに) 2008/02/15 17:19 れい

オブジェクトを包んで自分でComparerとかIComparableを実装して安定になるようにするしかないのではと思います。

# re: 安定なソート(しょにょに) 2008/02/15 18:08 επιστημη

そーなのかー...

ところで↑のコード、delegate使うんならIndexedCompare<T>は要らねーですね

// 安定なソート
static public void StableSort<T>(T[] array, Comparison<T> comp) {
 int[] index = new int[array.Length];
 T[] clone = (T[])array.Clone();
 for ( int i = 0; i < index.Length; ++i ) {
  index[i] = i;
 }
 Array.Sort(index,
  delegate(int x, int y) {
   int result = comp(clone[x],clone[y]);
   return result != 0 ? result : (x - y);
  });
 for ( int i = 0; i < index.Length; ++i ) {
  array[i] = clone[index[i]];
 }
}

# re: 安定なソート(しょにょに) 2008/02/16 12:56 NyaRuRu

>.NET Frameworkには安定にソートするメソッドは
用意されてねぇのですか? 教えてえらいひと。

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1420485&SiteID=1

回答している Matt Warren は『The Origin of LINQ to SQL』の人ですな.いわゆる神.
http://d.hatena.ne.jp/NyaRuRu/20080101/p1

# re: 安定なソート(しょにょに) 2008/02/16 13:03 NyaRuRu

あと例によって引き算で比較は危険っすよ.

# re: 安定なソート(しょにょに) 2008/02/16 13:39 επιστημη

へぇ、LINQ to Object の OrderBy は stable-sort なのかー。

> あと例によって引き算で比較は危険っすよ.

はぁい。

int result = comp(clone[x],clone[y]);
if ( result != 0 ) return result;
return ( x > y ) ? 1 : ((x < y) ? -1 : 0);

# re: 安定なソート(しょにょに) 2008/02/16 21:49 επιστημη

って、いいお返事してはみたものの、
ここでのint値の比較は配列indexなのでx,yどっちも正。
なのでそんなにナーバスになることもないか ^^;

# re: 安定なソート(しょにょに) 2008/02/17 16:58 NyaRuRu

個人的には,

x.Length.CompareTo(y.Length)

とか

Comparer<int>.Default.Compare(x.Length, y.Length)

が好きですね.

# vOQiScrVnOVzIZCfecZ 2014/07/19 1:44 http://crorkz.com/

8lp6sW Really enjoyed this blog article.Really looking forward to read more. Fantastic.

# pKPBUsZqNBHcakus 2014/09/05 20:45 http://alpenforum.forumsmotion.com/f1-forum

Fantastic website. Plenty of useful info here. I am sending it to several friends ans also sharing in delicious. And obviously, thanks for your effort!

# scHKLQzskiTmQxz 2014/09/11 20:51 http://www.ukessays.com

This website can be a stroll-by means of for all of the data you wanted about this and didn't know who to ask. Glimpse here, and also you'll positively discover it.

# ZaUfcNYLYDiiEZOhO 2014/09/14 8:48 http://www.needpeep.com/

I am constantly browsing online for tips that can benefit me. Thanks!

# PXEFwastdyM 2014/09/18 16:56 http://vergessene-helden.info/story/91890

lWcsrs I truly appreciate this blog post.Thanks Again. Great.

タイトル
名前
URL
コメント