「マージソート」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==マージソート== [VC++.NET]{{category ロジック}} VC++.NET 2005 using namespace System; using namespace System::Collections; static void marge( a…」) |
|||
(同じ利用者による、間の3版が非表示) | |||
1行目: | 1行目: | ||
− | ==マージソート== | + | ==[[マージソート]]== |
− | [VC++.NET] | + | [[VC++.NET]] | [[Category:ロジック]] |
− | VC++.NET 2005 | + | VC++[[.NET]] 2005 |
using namespace System; | using namespace System; | ||
7行目: | 7行目: | ||
− | static void marge( array | + | static void marge( array<String^>^ ary, |
− | array | + | array<String^>^ ary1, |
− | array | + | array<String^>^ ary2 ) { |
int i = 0; | int i = 0; | ||
int j = 0; | int j = 0; | ||
int k = 0; | int k = 0; | ||
− | while(i | + | while(i < ary1->Length && j < ary2->Length ) { |
− | if ( ary1[i]- | + | if ( ary1[i]->CompareTo(ary2[j]) < 0 ) { |
ary[k++] = ary1[i++]; | ary[k++] = ary1[i++]; | ||
} else { | } else { | ||
21行目: | 21行目: | ||
} | } | ||
} | } | ||
− | while(i | + | while(i < ary1->Length ) { |
ary[k++] = ary1[i++]; | ary[k++] = ary1[i++]; | ||
} | } | ||
− | while(j | + | while(j < ary2->Length ) { |
ary[k++] = ary2[j++]; | ary[k++] = ary2[j++]; | ||
} | } | ||
} | } | ||
− | static void margeSort( array | + | static void margeSort( array<String^>^ ary ) { |
− | if (ary- | + | if (ary->Length > 1) { |
− | int m = ary- | + | int m = ary->Length / 2; |
− | int n = ary- | + | int n = ary->Length - m; |
− | array | + | array<String^>^ a1 = gcnew array<String^>(m); |
− | array | + | array<String^>^ a2 = gcnew array<String^>(n); |
− | for (int i=0; i | + | for (int i=0; i<m; i++) { |
a1[i] = ary[i]; | a1[i] = ary[i]; | ||
} | } | ||
− | for (int i=0; i | + | for (int i=0; i<n; i++){ |
a2[i] = ary[m + i]; | a2[i] = ary[m + i]; | ||
} | } | ||
49行目: | 49行目: | ||
} | } | ||
− | static void sort( array | + | static void sort( array<String^>^ ary ) { |
margeSort(ary); | margeSort(ary); | ||
55行目: | 55行目: | ||
} | } | ||
− | 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) |
{ | { | ||
68行目: | 68行目: | ||
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 marge( array<String^>^ ary, array<String^>^ ary1, array<String^>^ ary2 ) { int i = 0; int j = 0; int k = 0; while(i < ary1->Length && j < ary2->Length ) { if ( ary1[i]->CompareTo(ary2[j]) < 0 ) { ary[k++] = ary1[i++]; } else { ary[k++] = ary2[j++]; } } while(i < ary1->Length ) { ary[k++] = ary1[i++]; } while(j < ary2->Length ) { ary[k++] = ary2[j++]; } } static void margeSort( array<String^>^ ary ) { if (ary->Length > 1) { int m = ary->Length / 2; int n = ary->Length - m; array<String^>^ a1 = gcnew array<String^>(m); array<String^>^ a2 = gcnew array<String^>(n); for (int i=0; i<m; i++) { a1[i] = ary[i]; } for (int i=0; i<n; i++){ a2[i] = ary[m + i]; } margeSort(a1); margeSort(a2); marge(ary, a1, a2); } } static void sort( array<String^>^ ary ) { margeSort(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 矢木浩人