改进的冒泡排序
2015-10-26 15:24
211 查看
用一个标记来记录上次交换的最后一个位置,位置后面的序列就是有序的了。有序的就不用再排了,节省了时间,提高了效率。
head.h
function.cpp
main.cpp
head.h
#define MAXSIZE 100 typedef int KeyType; typedef int InfoType; typedef struct{ KeyType key; InfoType otherinfo; }RedType; typedef struct{ RedType r[MAXSIZE+1]; int length; }SqList; void GBubbletSort(SqList &L); void ShowList(SqList &L);
function.cpp
#include"head.h" #include<iostream> using namespace std; void GBubbletSort(SqList &L) { RedType chan; int lastswap=L.length-1; int flag=L.length-1; for(int i=0;i<L.length;i++) { lastswap=flag; for(int j=0;j<lastswap;j++) { if(L.r[j].key>L.r[j+1].key) { chan=L.r[j]; L.r[j]=L.r[j+1]; L.r[j+1]=chan; flag=j; } } if(flag==lastswap) break; } } void ShowList(SqList &L) { if(L.r==NULL) { cout<<"the array is not exist!"<<endl; exit(0); } for(int i=0;i<L.length;i++) cout<<L.r[i].key<<" "; cout<<endl; }
main.cpp
#include"head.h" #include<iostream> using namespace std; void main() { cout<<"请输入数组长度length:"; int length; cin>>length; cout<<"请输入"<<length<<"个数:"<<endl; SqList arr; arr.length=0; for(int i=0;i<length;i++) { cin>>arr.r[i].key; arr.length++; } GBubbletSort(arr); cout<<"冒泡排序后的数组:"<<endl; ShowList(arr); }
相关文章推荐
- Uicc之UiccController(转)
- Java实现两个矩阵相乘
- WAS8.5安装
- 【图论训练】优惠
- HDU 4781 Assignment For Princess 构造
- ajax 和jsonp 不是一码事 细读详解
- Generating 2D Navmeshes
- Android 横竖屏处理
- SAP CO
- 手动发送广播
- LOG使用问题总结
- mysql 的类型转换函数cast的用法
- 3GPP TS 23.122 V12.7.0 中文版---1
- 浅谈SQL Server中统计对于查询的影响
- Handler sendMessage 与 obtainMessage (sendToTarget)比较
- swift:高级运算符(位运算符、溢出运算符、优先级和结合性、运算符重载函数)
- Codeforces 549G. Happy Line 馋
- 常用排序算法的JS实现
- Android编程实现小说阅读器滑动效果的方法
- POJ POJ Cash Machine 多重背包