Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void swap (int *a, int i, int j){
- int temp = a[i]; // a[i] = *(a + 1)
- a[i] = a[j];
- a[j] = temp;
- }
- void bubble_sort(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])
- swap (a, i ,j);
- }
- }
- }
- int main(){
- int n, *a, tg;
- do{
- cout << "Nhap n vao ";
- cin >> n;
- if (n < 0)
- cout << "Phai nhap n > 0!" << endl;
- } while (n <= 0);
- a = new int[n];
- for(int i = 0; i < n; i++){
- cout << "a[" << i + 1 << "] = ";
- cin >> a[i];
- }
- bubble_sort(a, n);
- for (int i = 0; i < n; i++){
- cout << a[i] << " ";
- }
- delete[] a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment