「挿入ソート」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==挿入ソート== [VC++.NET]{{category ロジック}} VC++.NET 2005 using namespace System; using namespace System::Collections; static void insertionSort(…」) |
|||
1行目: | 1行目: | ||
==挿入ソート== | ==挿入ソート== | ||
− | [VC++.NET]{{category ロジック}} | + | [[VC++.NET]]{{category ロジック}} |
VC++.NET 2005 | VC++.NET 2005 | ||
6行目: | 6行目: | ||
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) |
{ | { | ||
43行目: | 43行目: | ||
while ( (line = Console::ReadLine()) != nullptr ) { | while ( (line = Console::ReadLine()) != 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月15日 (土) 08:00時点における版
挿入ソート
VC++.NETテンプレート:Category ロジック VC++.NET 2005
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 矢木浩人