冒泡排序

#include <stdio.h>

#include <stdlib.h>

void bubble_sort(int a[], int size);

void print_array(int a[], int size);

int main()

{

int size;

int i;

int *a;

printf("input the size of array: size = \n");

scanf("%d",&size);

a = (int *)malloc(size * sizeof(int));

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

{

printf("input the %dth element of array's value : a[%d] = ",i+1,i);

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

printf("\n");

}

    print_array(a, size);

bubble_sort(a, size);

print_array(a, size);

return 0;

}

   

void print_array(int a[], int size)

{

int i;

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

{

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

}

printf("\n");

}

void bubble_sort(int a[], int size)

{

int i;

int temp;

int exchange = size -1;

  int bound;

    

while(exchange)

{

bound = exchange;

exchange = 0;

isend = 0;

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

{

if(a[i] > a[i+1])

{

temp = a[i];

a[i] = a[i+1];

a[i+1] = temp;

exchange = i;

}

}

print_array(a,size);

}

}

   

posted on 2013-08-02 18:23  零风腾飞  阅读(147)  评论(0编辑  收藏  举报

导航