【プログラム】C++でバブルソートをつくりました
自分のC++力の低さに気付き、勉強しています。それと同時にアルゴリズムとデータ構造も勉強しています。その一環でバブルソートをつくりました。テンプレートも勉強で入れています。
#include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <sstream> using namespace std; template<class T> void bubble_sort(T A[], const int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (A[i] > A[j]) { T a = A[i]; A[i] = A[j]; A[j] = a; } } } } template<class T> void output(T A[], const int n) { for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; } int main() { const int n = 9; int A[n] = { 3,6,2,9,1,5,4,8,7 }; double B[n] = { 1.1, 2.3, 3.7, 2.7, 4.7, 1.21, 4.8, 9.9, 6.5 }; output(A, n); bubble_sort(A, n); output(A, n); cout << endl; output(B, n); bubble_sort(B, n); output(B, n); system("PAUSE"); return 0; }