| 
#include <iostream> 
using namespace std; 
void bubbleSort(int a[] , int n) { 
    for(int i = 0 ; i < n- 1; i++) { 
        for(int j = i+1 ; j<n ; j++) { 
            if(a[i]> a[j]) {  // sắp xếp tăng dần 
                int temp = a[i] ; 
                a[i] = a[j] ; 
                a[j] = temp ; 
            } 
        } 
    } 
} 
void printArray(int a[] , int n) { 
     for(int i = 0 ; i < n; i++) { 
        cout<<a[i] <<" " ; 
     } 
     cout<<endl ; 
} 
int main() 
{ 
    int a[] = { 26 ,33 , 19    , 29 ,    18} ; 
    printArray(a,5) ; 
    bubbleSort(a,5) ; 
    printArray(a,5) ; 
    return 0; 
} | 
Thứ Năm, 28 tháng 8, 2014
Thuật toán Bubble Sort
Đăng ký:
Đăng Nhận xét (Atom)
 
Không có nhận xét nào:
Đăng nhận xét