纪念逝去的岁月——C/C++交换排序
交换排序
代码
#include <stdio.h> void printList(int iList[], int iLen)
{
int i = ;
for(i = ; i < iLen; i++)
{
printf("%d ", iList[i]);
}
printf("\n");
} int exchangeSort(int iList[], int iNum)
{
int i = , j = ;
for(i = ; i < iNum - ; i++)
{
int k = ;
for(j = i + ; j < iNum; j++)
{
if(iList[j] < iList[i])
{
int iX = iList[j];
iList[j] = iList[i];
iList[i] = iX;
}
k++;
printf(" .%d: ", k);
printList(iList, iNum);
}
printf("%d : ", i + );
printList(iList, iNum);
}
} int main()
{
int iNum = ;
int iList[] = {, , , , , , , , , };
printf("src : ");
printList(iList, iNum);
putchar('\n');
exchangeSort(iList, iNum);
putchar('\n');
printf("dst : ");
printList(iList, iNum); return ;
}
编译
$ g++ -o exchangeSort exchangeSort.cpp
运行
$ ./exchangeSort
src : .: 7 9
.: 5 7
.: 3 5
.: 0 3
.: //此时已经没有比 iList[0] 元素更小的了,所以后面几次都没有进行任何元素交换
.:
.:
.:
.:
: //此时 iList[0]元素已经稳定,下面开始寻找第二小的元素,并不断与 iList[1]进行交换
.: 7 9
.: 5 7
.: 3 5
.: 1 3
.: //此时已经没有比 iList[1] 元素更小的了,所以后面几次都没有进行任何元素交换
.:
.:
.:
: //此时 iList[0]元素和iList[1]元素已经稳定,下面开始寻找第三小的元素,并不断与 iList[2]进行交换
.: 7 9
.: 5 7
.: 3 5
.: 2 3
.: //
.:
.:
: // iList[2]稳定
.: 7 9
.: 5 7
.: 3 5
.:
.:
.:
: //iList[3]稳定
.: 7 9
.: 5 7
.: 4 5
.:
.:
: //iList[4]稳定
.: 7 9
.: 5 7
.:
.:
: //iList[5]稳定
.: 7 9
.: 6 7
.:
: //iList[6]稳定
.: 7 9
.:
: //iList[7]稳定
.: 8 9
: //iList[8]稳定
//根据抽屉原理,iList[9]只能是稳定的了 ^_^
dst :
再见……
纪念逝去的岁月——C/C++交换排序的更多相关文章
- 纪念逝去的岁月——C++实现一个队列(使用类模板)
1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...
- 纪念逝去的岁月——C++实现一个栈(使用类模板)
这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...
- 纪念逝去的岁月——C++实现一个栈
1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...
- 纪念逝去的岁月——C/C++排序二叉树
1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...
- 纪念逝去的岁月——C/C++二分查找
代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...
- 纪念逝去的岁月——C/C++快速排序
快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++选择排序
选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++冒泡排序
冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++字符串回文
判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...
随机推荐
- js判断当前的访问是手机还是电脑
<script type="text/javascript"> //平台.设备和操作系统 var system ={ win : false, mac : false, ...
- HR外包系统 - 薪资项目分类
序号 薪资项目编码规则 6到9开头1 普通工资项目加项 7开头三位,7XX,不够时,从71XX开始2 普通工资项目减项 8开头三位,8XX,不够时,从81XX开始3 ...
- the last lecture
2008.07.25,CMU教授Randy Pausch教授因癌症去世,仅47岁. 几年之前,当我看到Pausch先生最后一课的视频时,让我震撼. 转眼之间,7年过去了,这7年,让我成长了许多. 7年 ...
- Loadrunner中参数化实战(7)-Unique+Each iteration
参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Unique+Each iteration 设置迭代4次Action 结果如下:
- 廖雪峰js教程笔记7 基本类型和包装类型
在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...
- Android 编程下 DP、SP 以及屏幕像素密度
有时需为视图属性指定大小尺寸值(通常以像素为单位,但有时也用点.毫米或英寸).最常见的属性有: 文字大小(Text Size),指设备上显示的文字像素高度: 边距(Margin),指定视图组件间的距离 ...
- AOP动态代理解析4-jdk代理的实现
JDKProxy的使用关键是创建自定义的InvocationHandler,而InvocationHandler中包含了需要覆盖的函数getProxy,而当前的方法正是完成了这个操作.在此确认一下JD ...
- loopback 03
使用微信开发前准备 微信公众开发者平台 注册开发者账号获取权限: 安装包: wechat, wechat-oauth 微信公众平台操作 登录之后,得到appID和appsecret 根据appID和a ...
- Tactical Multiple Defense System 二分图
This problem is about a war game between two countries. To protect a base, your country built a defe ...
- [转]Tangram框架应用开发的一般模式
//转的,怕想百度博客一样搬家,赶紧先复制下来. 框架其实就是一种开发模式,用tangram框架开发应用程序意味着选择一种面向接口.模块化的开发方式.这和传统的Delphi应用程序开发方式有一定区别, ...