交换排序

代码

#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. sublime text 全局搜索

    Ctrl+Shift+F Mac下是commadn+Shift+F 在下面Find中填入需要搜索的关键字 点击find

  2. Win10 UAP 绑定

    Compiled DataBinding in Windows Universal Applications (UAP) http://nicksnettravels.builttoroam.com/ ...

  3. PowerDesigner(PowerDesigner15.1.0.2850)下载、安装以及破解

    转自:http://www.cnblogs.com/Fonkie/articles/1600662.html 一.先安装PowerDesigner15(PowerDesigner15.1.0.2850 ...

  4. JMockit

    [TOC] 简介 JMockit是基于JavaSE5中的java.lang.instrument包开发,内部使用ASM库来动态修改java的字节码,使得java这种静态语言可以想动态脚本语言一样动态设 ...

  5. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  6. ios摇一摇截屏代码

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  7. ubuntu 安装fcitx输入法

    ubuntu 14 的环境 我用的ibus输入法和firefox 36.0.4 版本相互冲突,有bug.在输入栏无法选中,以及复制.查其原因是ibus输入法有问题,需要重新换个输入法. 我先卸载了ib ...

  8. sql篇,动态合并数据

    背景: 为何说是一雪前耻呢,想当年,我还小,我出去面试远洋,远远地看着浩哥在那里坐着,然后下班去吃饭,我和东辉却在那里静静地等待着第二轮的技术面试(结果是没有面上,一个是学历问题),终于一个小个子姐姐 ...

  9. 用js 向h5 中的table 动态添加数据 (简单实现)

    //向 表格传值 function setTextareaValue(items,pp){ console.log(" 进入函数 items=="+items); var tb = ...

  10. Uva 699The Falling Leaves

    0.唔.这道题 首先要明确根节点在哪儿 初始化成pos=maxn/2; 1.因为是先序的输入方法,所以这个建树的方法很重要 void build(int p) { int v; cin>> ...