交换排序

代码

#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. apache lucene solr 官网历史版本下载地址

    官网上一般只提供最新版本的下载,下面两个链接为所有历史版本的下载地址: lucene地址:archive.apache.org/dist/lucene/java/ solr地址:archive.apa ...

  2. Java Hour 67 Java Collection API

    本文不是一个大而全的讲述Java Coleection 相关的APi, 而是笔者认为哪些是一个初学者所能够而且必须确切知道的知识点. Collection 一脉 这里有我们比较常用的List<E ...

  3. Win10 启动模拟器

    不知道怎么解决 答:去bios里设置了.开启了虚拟化hyper,重启.

  4. hdu 4044 2011北京赛区网络赛E 树形dp ****

    专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...

  5. Zero Copy 简介

    转自:http://blog.csdn.net/zzz_781111/article/details/7534649 许多web应用都会向用户提供大量的静态内容,这意味着有很多data从硬盘读出之后, ...

  6. eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法

    eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...

  7. MathType 常用快捷键

    MathType 数学公式编辑器是广大理科生电脑上必不可少的软件!然而在大量公式时,不会巧妙使用快捷键真的是心累身累.巧妙使用MathType数学公式编辑器可加快数学符号的录入速度和效率,这将节约大量 ...

  8. 注解:【有连接表的】Hibernate单向1->N关联

    Person与Address关联:单向1->N,[有连接表的] Person.java package org.crazyit.app.domain; import java.util.Hash ...

  9. barabasilab-networkScience学习笔记6-evolving networks

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  10. pylab模式

    启动IPython时ipython --pylab就可以进入pylab模式,这种模式下画图时图片不会直接蹦出来,而是嵌在交互环境中,当然sypder里自动是pylab模式了