提高篇第26-27课第三题

来源:互联网 发布:匿名网络举报网站 编辑:程序博客网 时间:2024/05/22 15:52
#include<stdio.h>#include<stdlib.h>//第三体,整型,字符型冒泡排序,选择排序void bubble_sort(int array[],int len);void output_array(int array[], int len);void char_bubble_sort(char c[], int len);//字符数组排序void char_output(char c[], int len);//字符数组输出void select_sort(int array[], int len);//选择排序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 };char c[20] = { 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e', 'l', 'e', 'c', 't', 'o', 'k', 'o', 'k' };char d[15] = { 'a', 'b', 'a', 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e' };  //自己补足bubble_sort(a, 20);   //用冒泡法按降序排序a中元素output_array(a, 20);   //输出排序后的数组bubble_sort(b, 15);   //用冒泡法按降序排序b中元素output_array(b, 15);   //输出排序后的数组char_bubble_sort(c, 20);char_output(c,20);char_bubble_sort(d, 15);char_output(d, 15);select_sort(a, 20);output_array(a, 20);return 0;}void bubble_sort(int array[], int len){int i,j,temp;for (i = 0; i < len - 1; i++)for (j = 0; j<len-i-1; j++){if (array[j] < array[j+1]){temp = array[j];array[j] = array[j+1];array[j+1] = temp;}}}void output_array(int array[], int len){int i;for (i = 0; i < len; i++)printf("%d ", array[i]);printf("\n");}void char_bubble_sort(char c[], int len){int  i;int *array;array = (int*)malloc(len*sizeof(int));//动态定义len大小的整型数组for (i = 0; i < len; i++)array[i] = c[i];bubble_sort(array, len);for (i = 0; i < len; i++)c[i] = array[i];free(array);}void char_output(char c[], int len){int i;for (i = 0; i < len; i++)printf("%c ", c[i]);printf("\n");}void select_sort(int array[],int len){int i,j,temp;for (i = 0; i < len; i++)for (j = i; j < len;j++)if (array[j] < array[i]){temp = array[j];array[j] = array[i];array[i] = temp;}}

0 0
原创粉丝点击