用C语言写一个冒泡排序

//
//  main.c
//  P01_hello
//
//  Created by i++ on 2019/3/23.
//  Copyright © 2019年 i++. All rights reserved.
//

#include <stdio.h>

void BubblesSort(int a[],int n);
void BubblesSort2(int a[],int n);
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    printf("hello C on Mac\n");
    int n;
    scanf("%d",&n);
    int array[n];
    int i =0;
    while (i<n) {
         scanf("%d",&array[i]);
         i++;
    }

    BubblesSort2(array,n);
    
    i =0;
    while (i<n) {
        printf("%d  ",array[i]);
        i++;
    }
     printf("\n ");
    
    return 0;
}

void BubblesSort(int a[],int n)
{
    int i =0,temp=0;
    int j=0;
    while (i<n-1) {
        j=0;
        while (j<n-i) {
            if (a[j]>a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1]=temp;
            }
            j++;
        }
        i++;
    }
}


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

猜你喜欢

转载自blog.csdn.net/Johnisjohn/article/details/88769072
今日推荐