交换排序

代码

#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++交换排序的更多相关文章

  1. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  2. 纪念逝去的岁月——C++实现一个栈(使用类模板)

    这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...

  3. 纪念逝去的岁月——C++实现一个栈

    1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...

  4. 纪念逝去的岁月——C/C++排序二叉树

    1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...

  5. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  6. 纪念逝去的岁月——C/C++快速排序

    快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  7. 纪念逝去的岁月——C/C++选择排序

    选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  8. 纪念逝去的岁月——C/C++冒泡排序

    冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  9. 纪念逝去的岁月——C/C++字符串回文

    判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...

随机推荐

  1. Visual Studio 2015的Web扩展包

    过去几年,Visual Studio扩展功能生态系统得到了蓬勃发展,社区贡献出了大量优秀的扩展,其中也包括大量针对Web开发的扩展.但是很多时候,感觉寻找.安装.更新好 几个扩展,总显得比较麻烦.如果 ...

  2. php抓取网页信息

    index.php <?php include_once 'simple_html_dom.php'; //获取html数据转化为对象 $html = file_get_html('http:/ ...

  3. PHP json数据格式化方法

    php 的json_encode能把数组转换为json格式的字符串.字符串没有缩进,中文会转为unicode编码,例如\u975a\u4ed4.人阅读比较困难.现在这个方法在json_encode的基 ...

  4. zoj 3882 博弈 *

    看了半天约数居然包括1,水了 #include<cstdio> #include<iostream> #include<algorithm> #include< ...

  5. [Mobile] 手机浏览器输入框-数字输入框

    手机浏览器的输入框,一直都是以web的方式进行开发的,没有关注到用户体验,领导提出了输入框要弹出数字输入框,想来应该有这种技术能实现.   搜索之后发现可以使用type="number&qu ...

  6. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  7. loj1011 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 思路:最近的开始做dp了...很明显的一道状态压缩题,dp[n][state]表 ...

  8. Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    使用命令关闭占用80端口的程序 sudo fuser -k 80/tcp

  9. DSP using MATLAB 示例Example2.12

    代码: b = [1]; a = [1, -0.9]; n = [-5:50]; h = impz(b,a,n); set(gcf,'Color','white'); %subplot(2,1,1); ...

  10. mongodb学习04 操作详解(2)

    查找文档 筛选查找 db.collection.find(); 返回一个集合中文档的子集,子集的 范围从 0 个文档到整个集合; db.collection.findOne(); 返回筛选的一个文档; ...