挿入ソート
ナビゲーションに移動
検索に移動
挿入ソート
using namespace System; using namespace System::Collections; static void insertionSort( array<String^>^ ary) { String^ key; for (long i=1; i < ary->Length; i++) { for (long j=0; j<i; j++) { if ( ary[i]->CompareTo(ary[j]) < 0 ) { key = ary[i]; for (long k=i; k>j; k--) { ary[k] = ary[k-1]; } ary[j] = key; break; } } } } static void sort( array<String^>^ ary ) { insertionSort(ary); } static void print( array<String^>^ ary ) { for (int i=0; i<ary->Length; i++) { Console::WriteLine(ary[i]); } } int main(array<System::String ^> ^args) { String^ line; IList^ list = gcnew ArrayList(); while ( (line = Console::ReadLine()) != nullptr ) { list->Add(line); } array<String^>^ ary = gcnew array<String^>(list->Count); list->CopyTo(ary, 0); sort(ary); print(ary); return 0; }
© 2006 矢木浩人