12345678910111213141516171819202122232425262728293031323334353637 |
- //
- // Created by tangs on 2018/11/18.
- //
- #include <iostream>
- using namespace std;
- void BubbleSort(int A[], int n) {
- int i, j;
- bool flag;
- for (i = 0; i < n - 1; i++) {
- flag = false;
- for (j = n - 1; j > i; j--) {
- if (A[j - 1] > A[j]) {
- swap(A[j - 1], A[j]);
- flag = true;
- };
- }
- if (flag == false) {
- return;
- }
- }
- }
- int main() {
- int A1[] = {6, 1, 5, 2, 1, 9, 10, 24, 7, 0};
- BubbleSort(A1, 10);
- // print:
- // 0 1 1 2 5 6 7 9 10 24
- for (int i = 0; i < 10; i++) {
- cout << A1[i] << " ";
- }
- cout << endl;
- return 0;
- }
|