1 #include <iostream>
2 using namespace std;
3 template<typename T>
4 //整数或浮点数皆可使用
5 void bubble_sort(T arr[], int len)
6 {
7 int i, j; T temp;
8 for (i = 0; i < len - 1; i++)
9 for (j = 0; j < len - 1 - i; j++)
10 if (arr[j] > arr[j + 1])
11 {
12 temp = arr[j];
13 arr[j] = arr[j + 1];
14 arr[j + 1] = temp;
15 }
16 }
17 int main()
18 {
19 int arr[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 };
20 int len = (int) sizeof(arr) / sizeof(*arr);
21 bubble_sort(arr, len);
22 for (int i = 0; i < len; i++)
23 cout << arr[i] << ' ';
24
25 cout << endl;
26
27 float arrf[] = { 17.5, 19.1, 0.6, 1.9, 10.5, 12.4, 3.8, 19.7, 1.5, 25.4, 28.6, 4.4, 23.8, 5.4 };
28 len = (int) sizeof(arrf) / sizeof(*arrf);
29 bubble_sort(arrf, len);
30 for (int i = 0; i < len; i++)
31 cout << arrf[i] << ' ';
32
33 return 0;
34 }
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.