Mẹo Hay

Thuật toán sắp xếp nổi bọt – Bubble Sort.

Rate this post



giới thiệu thuật toán sắp xếp nổi bọt trong ngôn ngữ c. tự học lập trình. hướng dẫn lập trình Căn bản và nâng cao. C programing for beginner. triệu thân channel – let’s grow together!

link get source code:

Tag: shell sort trong c++, bubble, bubbleSort, thuật toán sắp xếp, sắp xếp nổi bọt, thuật toán sắp xếp nổi bọt, chương trình sắp xếp dãy, sắp xếp dãy bằng thuật toán sắp xếp nổi bọt, triệu thân, triệu thân channel, let, let’s grow together

Xem Thêm Bài Viết Về Mẹo Hay Khác: https://meocongnghe.vn/meo-hay

Nguồn: https://meocongnghe.vn

30 Comments

  1. anh xem cho em thiếu chỗ nào sao nó không chạy với e cảm ơn

    void nhap(int a[], int n)

    {

    printf("nNhap n = ");

    scanf("%d", &n);

    int i;

    for(i = 0; i < n; i++)

    {

    printf("Nhap a[%d] = ", i + 1);

    scanf("%d", &a[i]);

    }

    }

    void bubbleSort(int a[], int n)

    {

    int i, j;

    for(i = 0; i < n – 1; i++)

    {

    for(j = n – 1; j > i; j–)

    {

    if(a[j] > a[j – 1])

    {

    int tg = a[j];

    a[j] = a[j – 1];

    a[j – 1] = tg;

    }

    }

    }

    }

    void xuat(int a[], int n){

    int i;

    for(i = 0; i < n; i++)

    {

    printf("%5d", a[i]);

    printf("n");

    }

    }

    int main()

    {

    int a[10];

    int n;

    nhap(a, n);

    printf("nTruoc khi sx: ");

    xuat(a, n);

    printf("nSau khi sx: ");

    bubbleSort(a, n);

    xuat(a, n);

    return 0;

    }

    Reply
  2. Sao nó bị lỗi này ở phần void vậy ạ: [Error] a function-definition is not allowed here before '{' token

    #include<stdio.h>

    int main()

    {

    void nhap(int a[], int n)

    {

    printf("n Nhap n: ");

    scanf("%d", &n)

    for(int i=0; i<n; i++)

    {printf("Nhap a[%d]", i);

    scanf("%d", &a[i]);

    }

    }

    Reply
  3. cho e hoi sai ở đâu mà nó chỉ nhao n với xuat mảng, k có sắp xếp ạ
    #include <stdio.h>

    void nhap(int a[], int n){

    int i;

    printf("nnhap n=");

    scanf("%d",&n);

    for ( i = 0;i < n; i++){

    printf("nNhap phan tu a[%d] = ", i+1);

    scanf("%d", &a[i]);}

    }

    void bubbleSort ( int a[], int n){

    int i,j;

    for (i=0 ; i<n-1; i++){

    for (j= n-1; j>i ; j–){

    if( a[j]< a[j-1]){

    int tam=a[j];

    a[j]=a[j-1];

    a[j-1]=tam;

    }

    }

    }

    }

    void xuat(int a[], int n){

    int i;

    for (i = 0;i < n; i++)

    printf("n%5d", a[i]);

    printf("n");

    }

    int main(){

    int a[10];

    int n;

    nhap(a,n);

    bubbleSort(a,n);

    xuat(a,n);

    return 0;

    }

    Reply
  4. a ơi giúp e , ct nó hiện lỗi comparison between pointer and integer ở hàm nhap(), và ct chỉ cho nhập số n , nhập xong thì kết thúc luôn ạ , chứ ko sắp xếp , e cảm ơn a
    #include<stdio.h>

    void nhap(int a[], int *n ){
    printf("nNhap n = ");
    scanf("%d", &n);
    int i;
    for(i = 0; i < n; i++ ){
    printf("nNhap a[%d] = ", i+1); scanf("%d", &a[i]);
    }
    }

    void bubbleSort(int a[], int n ){
    int i,j;
    for(i= 0; i< n-1; i++ ){
    for(j = n-1; j > i; j– )
    if( a[j] < a[j-1] ) {
    int tg = a[j];
    a[j] = a[j-1];
    a[j-1] = tg;
    }
    }
    }

    void xuat(int a[], int n ){
    int i;
    for(i = 0; i< n; i++ ) printf("%5d", a[i]);
    printf("n");
    }

    int main(){
    int a[20];
    int n;
    nhap(a,&n);
    xuat(a,n);
    bubbleSort(a,n);
    xuat(a,n);
    return 0;
    }

    Reply
  5. Cho em hỏi nếu có 2 lệnh for lồng nhau thì máy sẽ thực hiện hết lệnh for trong trước rồi mới đến lệnh for ngoài hay thực hiện lặp 1 lần for trong sau lại đến lặp 1 lần for ngoài rồi cứ thế thực hiện đến khi hết

    Reply

Post Comment