提高第27课时,实践3,项目3-数组的排序

来源:互联网 发布:sqlserver 默认值约束 编辑:程序博客网 时间:2024/06/04 17:56
/**Copyright(c) 2015/5/18 CSDN博客*All rights reserved.*文件名称:main.c*作    者:金叶*完成日期:2015/5/18*版本号:V1.0*问题描述:项目3 - 数组的排序d(1)编写函数,完成冒泡排序要求不能改变下面的main函数。*/#include <stdio.h>//两个函数bubble_sort和output_array的声明void bubble_sort(int a[],int m);void output_array(int a[],int m);int main( ){    int a[20]={86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};    int b[15]={27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};    bubble_sort(a,20);   //用冒泡法按降序排序a中元素    output_array(a,20);   //输出排序后的数组    bubble_sort(b,15);   //用冒泡法按降序排序b中元素    output_array(b,15);   //输出排序后的数组    return 0;}//请在下面定义bubble_sort和output_array函数void bubble_sort(int a[],int m){    int i,j;    for (i=0;i<m-1;i++){        for (j=0;j<m-i-1;j++){            if (a[i]>a[j]){                int t;                t=a[i];                a[i]=a[j];                a[j]=t;            }        }    }}void output_array(int a[],int m){    int i;    for (i=0;i<m;i++){        printf("%d\t",a[i]);        if ((i+1)%5==0)            printf("\n");    }    printf("\n");}




/**Copyright(c) 2015/5/18 CSDN博客*All rights reserved.*文件名称:main.c*作    者:金叶*完成日期:2015/5/18*版本号:V1.0*问题描述:(2)字符数组排序:改造(1)的程序,使其能对字符数组进行排序,其中:*/#include <stdio.h>//两个函数bubble_sort和output_array的声明void bubble_sort(char a[],int m);void output_array(char a[],int m);int main( ){    char a[20] = {'s','o','r','t','b','u','b','b','l','e','s','e','l','e','c','t','o','k','o','k'};    char b[15] = {'u','b','b','l','e','s','e','l','e','c','t','o','k','o','k'};    bubble_sort(a,20);   //用冒泡法按降序排序a中元素    output_array(a,20);   //输出排序后的数组    bubble_sort(b,15);   //用冒泡法按降序排序b中元素    output_array(b,15);   //输出排序后的数组    return 0;}//请在下面定义bubble_sort和output_array函数void bubble_sort(char a[],int m){    int i,j;    for (i=0;i<m-1;i++){        for (j=0;j<m-i-1;j++){            if (a[j]<a[j+1]){                char t;                t=a[j];                a[j]=a[j+1];                a[j+1]=t;            }        }    }}void output_array(char a[],int m){    int i;    for (i=0;i<m;i++){        printf("%c\t",a[i]);        if ((i+1)%5==0)            printf("\n");    }    printf("\n");}



/**Copyright(c) 2015/5/18 CSDN博客*All rights reserved.*文件名称:main.c*作    者:金叶*完成日期:2015/5/18*版本号:V1.0*体验选择排序:改造(1)的程序,将bubble_sort(...)改为select_sort(...),排序算法由冒泡排序换作为选择排序,排序结果由降序变为升序,完成类似的任务。*/#include <stdio.h>//两个函数bubble_sort和output_array的声明void select_sort(int a[],int m);void output_array(int a[],int m);int main( ){    int a[20]= {86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};    int b[15]= {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};    select_sort(a,20);   //用冒泡法按降序排序a中元素    output_array(a,20);   //输出排序后的数组    select_sort(b,15);   //用冒泡法按降序排序b中元素    output_array(b,15);   //输出排序后的数组    return 0;}//请在下面定义bubble_sort和output_array函数void select_sort(int a[],int m){   int i,j,k,t;   for (i=0;i<m-1;i++){    k=i;    for (j=i+1;j<m;j++){        if (a[j]<a[k]){            k=j;            }    }    t=a[k];    a[k]=a[i];    a[i]=t;   }}void output_array(int a[],int m){    int i;    for (i=0;i<m;i++){        printf("%d\t",a[i]);        if ((i+1)%5==0)            printf("\n");    }    printf("\n");}



0 0