#include<stdio.h>
#include<assert.h> void display(int * a, int n)
{
for(int i = 0; i < n; i++)
{
printf("%d,",a[i]);
}
printf("\n");
} void swap(int * a, int * b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
} void bubble_sort(int * a, int n)
{
int i = 0;
int j ;
for(i = 0; i < n-1; i++ )
{
for( j = i; j <= n-1; j++ )
{
if(a[j] >= a[i])
{
swap(&(a[j]), &(a[i]));
}
}
} } int main()
{
int a[8] ={2, 1, 3, 4, 5, 7, 6, 8};
int num = sizeof(a)/sizeof(int);
printf("before_sort:");
display(a, num);
bubble_sort(a,num);
printf("after_sort:");
display(a, num);
return 0; }

改进后:

#include <stdio.h>
#include <time.h> //交换两个数据
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
} //显示交换后的数组
void display_array( int a[], int n )
{
int i;
for( i = 0; i < n; i++ )
printf( "%d ", a[i] );
} //冒泡排序
void bubble_sort( int a[], int n )
{ int i ,j;
int flag = 1; //判断比较中是否发生了交换 1代表进行了交换
for(i = 0; i<n-1 && flag; i++ ) //flag的作用:若上次排序没有发生交换,就说明数组已经是有序
{ //这样做可以减少不必要的后续比较
flag = 0;
for(j = n -1; j >=i; j-- ) //第一次比较时 j是 0 ~ 7 第二次是 1 ~ 7
{
if(a[j] > a[j + 1])
{
swap(&(a[j]), &(a[j+1]));
flag = 1;
}
}
} } int main()
{
clock_t start, finish;
start = clock();
int n = 8;
int a[] = { 2, 1, 3, 4, 5, 7, 6, 8 };
printf( "Before sorting: " );
display_array( a, n );
bubble_sort( a, n );
printf( "After sorting: " );
display_array( a, n );
finish = clock();
printf("\n本次计算一共耗时: %f秒\n\n", (double)(finish-start)/CLOCKS_PER_SEC);
return 0;
}

H面试程序(15): 冒泡排序法的更多相关文章

  1. H面试程序(10): 字符串包含问题

    题目描述:判断第二个字符串中的元素是否都能在第一个字符串中找到: 注意:和字符串的字串的问题有所区别,如第一个字符串为  abcdefg,第二个字符串为 aaabc,第二个字串还是包含于第一个字符串 ...

  2. H面试程序(11): 判断字符串是否包含子串问题

    题目描述:                        如字符串str1为''abcdef'''                       字符串str2为'' bc''; 则字符串str1中含有 ...

  3. H面试程序(12): 输出字符串中第一个只出现一次的字母

    题目描述: 若字符串str为'' sbdddsbfc'',则输出 f; 若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上 #include <stdio.h& ...

  4. H面试程序(29):求最大递增数

    要求:求最大递增数 如:1231123451 输出12345 #include<stdio.h> #include<assert.h> void find(char *s) { ...

  5. H面试程序(27):字串转换

    //1 字串转换 //问题描述: //将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a: //若输 ...

  6. H面试程序(16): 简单选择排序

    #include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...

  7. H面试程序(28):字符串处理转换

    //2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...

  8. H面试程序(4):翻转句子中单词的顺序 .

    题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stude ...

  9. H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒

    编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...

随机推荐

  1. WINFORM Tootip使用小结

    toolTip1.Active = true;   //激活工具提示,只有激活才会显示提示 toolTip1.IsBalloon = true;    //toolTip以气泡形式出现 toolTip ...

  2. BestCoder Round #47

    1001 Senior's Array 题目链接:1001 题意:给你一个长度为n的序列,你必须修改序列中的某个数为P,求修改后的最大连续子序列和. 思路:数据量比较小,可以直接暴力做, 枚举序列的每 ...

  3. Push segues can only be used when the.....

    刚刚遇到的两个错误,. 1, Terminating app due to uncaught exception'NSGenericException', reason: 'Push segues c ...

  4. jQuery validate和form插件配套使用

    参考 官网http://jqueryvalidation.org/documentation/ 博客http://www.cnblogs.com/buzzlight/archive/2010/06/3 ...

  5. perl5 第十一章 文件系统

    第十一章  文件系统 by flamephoenix 一.文件输入/输出函数  1.基本I/O函数    1)open函数    2)用open重定向输入    3)文件重定向    4)指定读写权限 ...

  6. HDOJ 2102 A计划(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 思路分析: <1>搜索方法分析:由于需要寻找最短的找到公主的路径,所以采用bfs搜索 ...

  7. 过河(bfs)

    Problem 2188 过河I Accept: 112    Submit: 277 Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  8. Cloud Foundry warden container 安全性探讨

    本文将从Cloud Foundry中warden container的几个方面探讨warden container的安全性. 1. warden container互訪 1.1.  互訪原理· 在Cl ...

  9. ASP.net体系

  10. java虚拟机内存溢出各种场景总结

    java堆溢出 java堆用于存储对象实例,只要不断地创建对象,并且保证gc roots到对象之间有可达路径来避免垃圾回收机制来清楚这些对象,那么在 对象到达最大堆的容量限制后就会产生内存溢出溢出. ...