#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. 第八章 C#面向对象编程(Object-Oriented Programming,OOP)简介

    .NET OOP 一.面向对象编程的含义 1.函数(过程化)编程常常导致单一的应用程序,即所有的功能都包含在几个代码模块中(常常是一个代码模块) 而使用OOP技术,常常使用许多代码模块,每个模块提供特 ...

  2. ##DAY5 UIControl及其子类

    ##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...

  3. les nationalités et les pays

    masculin  féminin pays français  française  la France chinois  chinoise  la Chine   suisse  suisse  ...

  4. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  5. Vistual Studio 2010 调试无法进断点

    系统是2003出现的问题 win8就没事 打sp1 补丁就行

  6. what does Html.HiddenFor () for ?

    When I want to pass some value that won't be seen by users, I find it useful to use this. It can hel ...

  7. UIWebView的三种加载方式

    一.使用UIWebView 将web content 嵌入到应用上. API提供了三种方法: - (void)loadRequest:(NSURLRequest *)request; - (void) ...

  8. Oracle定时器调用存储过程

    1. 创建表 create table job_table(run_time date); 2. 创建存储过程 create or replace procedure job_proc is begi ...

  9. 使用Groovy进行依赖注入

    为什么选择Groovy? 传统的依赖注入是XML,对我而言,可读性太差,太不美观,强烈地想换一个方式进行依赖注入,Groovy作为XML的替代方案,在Spring4之后被引入,是基于JVM的一门方言, ...

  10. Cloudera Manager、CDH零基础入门、线路指导 http://www.aboutyun.com/thread-9219-1-1.html (出处: about云开发)

    Cloudera Manager.CDH零基础入门.线路指导http://www.aboutyun.com/thread-9219-1-1.html(出处: about云开发) 问题导读:1.什么是c ...