初级软考资料:面试系列3--冒泡算法(优化)
发布时间:2010/3/27 9:38:27 来源:城市学习网 编辑:admin
本段代码增加了一些优化:
增加b_exchange,若本轮冒泡没有交换数据,则表示排序成功,退出
增加n_exchange,n_head,记录最近的交换位置,下轮冒泡只要冒到该位置即可
/
created:2006/06/15
filename:C:“DocumentsandSettings“Administrator“桌面mmp“poposort.c
filepath:C:“DocumentsandSettings“Administrator“桌面mmp
filebase:poposort
fileext:c
author:A.TNG
version:0.0.1
purpose:冒泡排序的实现
增加b_exchange,若本轮冒泡没有交换数据,则表示排序成功,退出
增加n_exchange,n_head,记录最近的交换位置,下轮冒泡只要冒到该位置即可
#include<stdio.h>
#include<stdlib.h>
/
name:poposort
params:
polist[in/out]待排序的int数组
n[in]int数组的长度
return:
1-成功0-失败
notes:
对polist进行冒泡排序
author:A.TNG2006/06/159:00
/
intpoposort
{
inti,j;
intn_exchange;
if
return0;
n_exchange=0;
for
{
/最外层循环,冒泡排序需要比较n-1轮/
intb_exchange;
intn_head;
b_exchange=0;
n_head=n_exchange;
for
{
/第i轮比较,把最轻的泡冒置第i个位置/
if
{
intn_tmp_num;
n_tmp_num=polist[j];
polist[j]=polist[j-1];
polist[j-1]=n_tmp_num;
b_exchange=1;
n_exchange=j;