设为首页 加入收藏

TOP

冒泡排序算法C语言代码测试总结(一)
2018-05-26 14:14:00 】 浏览:75
Tags:冒泡 排序 算法 语言 代码 测试 总结

代码是用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;//缩小遍历边界  
        fo
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言线性单链表相关函数和算法的.. 下一篇C语言实现定时关机的代码教程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目