代码是用c-free5.0编译器测试的,只要改测试程序中的排序算法函数就可以看到测试结果,代码如下:
#include <stdio.h> int arr1[24][25]={ 10,11,12,13,14,0,1,2,3,4,9,8,7,6,5,19,18,17,16,15,20,21,22,23,24, 10,11,12,13,14,0,1,2,3,4,9,8,7,6,5,20,21,22,23,24,19,18,17,16,15, 10,11,12,13,14,0,1,2,3,4,19,18,17,16,15,9,8,7,6,5,20,21,22,23,24, 10,11,12,13,14,0,1,2,3,4,19,18,17,16,15,20,21,22,23,24,9,8,7,6,5, 10,11,12,13,14,0,1,2,3,4,20,21,22,23,24,9,8,7,6,5,19,18,17,16,15, 10,11,12,13,14,0,1,2,3,4,20,21,22,23,24,19,18,17,16,15,9,8,7,6,5, 10,11,12,13,14,9,8,7,6,5,0,1,2,3,4,19,18,17,16,15,20,21,22,23,24, 10,11,12,13,14,9,8,7,6,5,0,1,2,3,4,20,21,22,23,24,19,18,17,16,15, 10,11,12,13,14,19,18,17,16,15,0,1,2,3,4,9,8,7,6,5,20,21,22,23,24, 10,11,12,13,14,19,18,17,16,15,0,1,2,3,4,20,21,22,23,24,9,8,7,6,5, 10,11,12,13,14,20,21,22,23,24,0,1,2,3,4,9,8,7,6,5,19,18,17,16,15, 10,11,12,13,14,20,21,22,23,24,0,1,2,3,4,19,18,17,16,15,9,8,7,6,5, 10,11,12,13,14,9,8,7,6,5,19,18,17,16,15,0,1,2,3,4,20,21,22,23,24, 10,11,12,13,14,9,8,7,6,5,20,21,22,23,24,0,1,2,3,4,19,18,17,16,15, 10,11,12,13,14,19,18,17,16,15,9,8,7,6,5,0,1,2,3,4,20,21,22,23,24, 10,11,12,13,14,19,18,17,16,15,20,21,22,23,24,0,1,2,3,4,9,8,7,6,5, 10,11,12,13,14,20,21,22,23,24,9,8,7,6,5,0,1,2,3,4,19,18,17,16,15, 10,11,12,13,14,20,21,22,23,24,19,18,17,16,15,0,1,2,3,4,9,8,7,6,5, 10,11,12,13,14,9,8,7,6,5,19,18,17,16,15,20,21,22,23,24,0,1,2,3,4, 10,11,12,13,14,9,8,7,6,5,20,21,22,23,24,19,18,17,16,15,0,1,2,3,4, 10,11,12,13,14,19,18,17,16,15,9,8,7,6,5,20,21,22,23,24,0,1,2,3,4, 10,11,12,13,14,19,18,17,16,15,20,21,22,23,24,9,8,7,6,5,0,1,2,3,4, 10,11,12,13,14,20,21,22,23,24,9,8,7,6,5,19,18,17,16,15,0,1,2,3,4, 10,11,12,13,14,20,21,22,23,24,19,18,17,16,15,9,8,7,6,5,0,1,2,3,4 }; void vbubblesort(int arr[], int len){ int i, j, temp; for (j = 0; j < len - 1; j++){ //每次最大元素就像气泡一样"浮"到数组的最后 for (i = 0; i < len - 1 - j; i++){ //依次比较相邻的两个元素,使较大的那个向后移 if(arr[i] > arr[i + 1]){ //交换两个数 temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } } void vbubblesortchange(int arr[], int len){ int i,j,temp; int swapped = 1; for (j = 0; swapped; j++){ //每次最大元素就像气泡一样"浮"到数组的最后 swapped = 0; for (i = 0; i < len - 1 - j; i++){ //依次比较相邻的两个元素,使较大的那个向后移 if(arr[i] > arr[i + 1]){ //交换两个数 temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = 1; } } // if( swapped == 0) {j = len-1;}//如果没有元素交换,说明序列是顺序的,退出循环 } } void vcocktailsort(int arr[],int len){ int tmp,i,left=0,right = len-1; while(left < right){ for(i=left;i<right;i++){//正向冒泡,确定最大值 if(arr[i]>arr[i+1]){ tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; } } right--; for(i=right;i>left;i--){//反向冒泡,确定最小值 if(arr[i]<arr[i-1]){ tmp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = tmp; } } left++; } } void vcocktailsortchange(int arr[],int len){ int tmp,i,left=0,right = len-1; int swapped = 1; int bound = 0;//记录某趟遍历的最后一次交换元素的位置,优化减少循环次数 while(swapped){//如果没有元素交换,说明序列是顺序的 swapped = 0; for(i=left;i<right;i++){//正向冒泡,确定最大值 if(arr[i]>arr[i+1]){ tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; swapped = 1; bound = i; } } right=bound;//缩小遍历边界 for(i=right;i>left;i--){//反向冒泡,确定最小值 if(arr[i]<arr[i-1]){ tmp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = tmp; swapped = 1; bound = i; } } left=bound;//缩小遍历边界 } } void vprintarr(int a1[],int n){ int i=0; for(i=0;i<n;i++) { printf("%d,",a1[i]); } printf("n"); } int main() { int i=0; //打印原数组 for(i=0;i<24;i++){ vprintarr(&arr1[i][0],25); } printf("打印原数组n"); //排序 for(i=0;i<24;i++){ vcocktailsortchange(&arr1[i][0],25); } //打印排序数组 for(i=0;i<24;i++){ vprintarr(&arr1[i][0],25); } printf("hello world!n"); return 0; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/607490.html