「挿入ソート」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==挿入ソート== [VC++.NET]{{category ロジック}} VC++.NET 2005 using namespace System; using namespace System::Collections; static void insertionSort(…」) |
|||
(同じ利用者による、間の3版が非表示) | |||
1行目: | 1行目: | ||
− | ==挿入ソート== | + | ==[[挿入ソート]]== |
− | [VC++.NET] | + | [[VC++.NET]] | [[Category:ロジック]] |
− | VC++.NET 2005 | + | VC++[[.NET]] 2005 |
using namespace System; | using namespace System; | ||
using namespace System::Collections; | using namespace System::Collections; | ||
− | static void insertionSort( array | + | static void insertionSort( array<String^>^ ary) { |
String^ key; | String^ key; | ||
− | for (long i=1; i | + | for (long i=1; i < ary->Length; i++) { |
− | for (long j=0; j | + | for (long j=0; j<i; j++) { |
− | if ( ary[i]- | + | if ( ary[i]->CompareTo(ary[j]) < 0 ) { |
key = ary[i]; | key = ary[i]; | ||
− | for (long k=i; k | + | for (long k=i; k>j; k--) { |
ary[k] = ary[k-1]; | ary[k] = ary[k-1]; | ||
} | } | ||
23行目: | 23行目: | ||
} | } | ||
− | static void sort( array | + | static void sort( array<String^>^ ary ) { |
insertionSort(ary); | insertionSort(ary); | ||
29行目: | 29行目: | ||
} | } | ||
− | static void print( array | + | static void print( array<String^>^ ary ) { |
− | for (int i=0; i | + | for (int i=0; i<ary->Length; i++) { |
Console::WriteLine(ary[i]); | Console::WriteLine(ary[i]); | ||
} | } | ||
} | } | ||
− | int main(array | + | int main(array<System::String ^> ^args) |
{ | { | ||
42行目: | 42行目: | ||
IList^ list = gcnew ArrayList(); | IList^ list = gcnew ArrayList(); | ||
− | while ( (line = Console:: | + | while ( (line = Console::[[R]]eadLine()) != nullptr ) { |
− | list- | + | list->Add(line); |
} | } | ||
− | array | + | array<String^>^ ary = gcnew array<String^>(list->Count); |
− | list- | + | list->CopyTo(ary, 0); |
sort(ary); | sort(ary); |
2020年2月16日 (日) 04:20時点における最新版
挿入ソート
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 矢木浩人