H面试程序(15): 冒泡排序法
#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): 冒泡排序法的更多相关文章
- H面试程序(10): 字符串包含问题
题目描述:判断第二个字符串中的元素是否都能在第一个字符串中找到: 注意:和字符串的字串的问题有所区别,如第一个字符串为 abcdefg,第二个字符串为 aaabc,第二个字串还是包含于第一个字符串 ...
- H面试程序(11): 判断字符串是否包含子串问题
题目描述: 如字符串str1为''abcdef''' 字符串str2为'' bc''; 则字符串str1中含有 ...
- H面试程序(12): 输出字符串中第一个只出现一次的字母
题目描述: 若字符串str为'' sbdddsbfc'',则输出 f; 若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上 #include <stdio.h& ...
- H面试程序(29):求最大递增数
要求:求最大递增数 如:1231123451 输出12345 #include<stdio.h> #include<assert.h> void find(char *s) { ...
- H面试程序(27):字串转换
//1 字串转换 //问题描述: //将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a: //若输 ...
- H面试程序(16): 简单选择排序
#include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...
- H面试程序(28):字符串处理转换
//2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...
- H面试程序(4):翻转句子中单词的顺序 .
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stude ...
- H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...
随机推荐
- 一般处理程序在VS2012中打开问题
问题:如果你用vs2012建立的一个一般处理程序,运行查看是,出现这样的界面 原因:VS2012默认使用IIS Web服务器,而不是Visual Studio开发服务器,基于安全考虑IIS默认不允许浏 ...
- 【转】Linq实现DataTable行列转换
出处:http://www.cnblogs.com/li-peng/ 转换前的table: 转换后的table: 代码里有详细的说明, 还有一些参数我都截图了下面有 using System;usin ...
- C#WebService 客户端通过Http调用请求(转)
1.webservice帮助类 public class WebServiceHelper { public static string CallServiceByG ...
- servlet三种实现方式之二继承GenericServlet开发
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第二种示例代码如下(已去掉包名): //这是第二 ...
- C——货物管理系统
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> ...
- wsdl透明解析
1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...
- 转: 深入理解 AngularJS 的 Scope
查看 DEMO.参考 StackOverflow. ng-switch ng-switch 的原型继承和 ng-include 一样.所以如果你需要对基本类型数据进行双向绑定,使用 $parent ...
- 禁用Java DNS缓存-Disable DNS caching
Once an application has performed network access (i.e. urlconnection, parsing of xml document with e ...
- VARCHAR2(N CHAR)与VARCHAR2(N)的区别[Oracle基础]
转载: http://blog.itpub.net/24930246/viewspace-1064982 在数据库开发的时候,经常需要考虑存储空间的问题,当然很多时候我们并不需要去考虑一些细小的差别, ...
- C/C++中的内存管理
一.内存的分配方式 1. 程序代码区 2. 静态数据区 3. 动态数据区 二.动态内存 1. 在栈上创建的内存 2. 从堆上分配的内存 3. 小结 三.指针与内存 1. 操作内存 2. 指针与数组 3 ...