1. 程式人生 > >c++之冒泡排序

c++之冒泡排序

pub pau 好的 use log iostream ++ logs array

#include<iostream>
using namespace std;
class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        //c++求數組長度:sizeof(array) / sizeof(array[0])
        for (int i = 0; i<n; i++)
            for (int j = 0; j<n - i - 1; j++) {
                if (A[j]>A[j + 1
]) { int temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; } } return A; } }; int main() { int n = 10; int a[10]; for (int i = 0; i<n; i++) { cin >> a[i]; } BubbleSort b; b.bubbleSort(a, n); cout
<< "排好的數為:" << endl; for (int i = 0; i<n; i++) cout << a[i] << "\t"; cout << endl; system("pause"); return 0; }

c++之冒泡排序