纪念逝去的岁月——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 ...
随机推荐
- Linux 下编译自己的 OpenJDK7 包括JVM和JDK API
1.首先去 这里 http://download.java.net/openjdk/jdk7/ 下载OpenJDK7的源码zip包 2. 简要介绍下OpenJDK7中的目录 hotspot: 放有Op ...
- 外观模式/facade模式/结构型模式
外观模式 为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式三要素(client-facade-subSystem) 外观角色 ...
- Win7系统怎么开启远程桌面?Win7远程桌面怎么用(转)
远程桌面服务开启之后,可以方便的远程管理服务器或计算机.为生活和工作带来不少便利呢,很多小伙伴还不知道怎么开启win7远程桌面吧(下面咗嚛以内网远程桌面为例) 工具/原料 Win7 Win7远程桌 ...
- 2016北京网络赛 hihocoder 1391 Countries 树状数组
Countries 描述 There are two antagonistic countries, country A and country B. They are in a war, and ...
- Parcelable和Serializable的区别
一.Android为什么要序列化?什么是序列化,怎么进行序列化 why 为什么要了解序列化?—— 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们 ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- hdu 5692 Snacks 线段树+dfs
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Laravel系列 目录结构
Where Is The Models Directory? app directory by default 其中 app:,core code of your application, almos ...
- CentOS下vm虚拟机桥接联网
CentOS下vm虚拟机桥接联网 vm虚拟机下的桥接联网相当于虚拟机是一个独立的主机,直接与外网相连,这是比较好的连接方式,这样外网的机子就可以直接访问到虚拟机了. 首先虚拟机的联网方式设置为 ...
- Visual Studio 设置 Inherited include Directories
在用Visual Studio进行开发的时候,避免不了要使用一些常用的第三方提供的库.如果是一次两次设置还能让人忍受,但是如果要写很多项目的话,设置这些库真的很让人头疼.不过Visual Studio ...