纪念逝去的岁月——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 ...
随机推荐
- go sample - hello world
入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...
- [Outlook]设置邮件自动接收时间
[Outlook]设置邮件自动接收时间 找了好久,一直都没设置正常,导致老是收到邮件有延迟,今天头脑清晰,搜了一下,然后自己竟然给找到了,记下来当笔记,好记性不如烂笔头,呵呵 搜索百度&quo ...
- Java中synchronized详解
synchronized 原则: 尽量避免无谓的同步控制,同步需要系统开销,可能造成死锁 尽量减少锁的粒度 同步方法 public synchronized void printVal(int v) ...
- 驱动中获取PsActiveProcessHead变量地址的五种方法也可以获取KdpDebuggerDataListHead
PsActiveProcessHead的定义: 在windows系统中,所有的活动进程都是连在一起的,构成一个双链表,表头是全局变量PsActiveProcessHead,当一个进程被创建时,其Act ...
- cdoj1324暴力分块
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> ...
- supervisor(三)xml_rpc
supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,然后函数方法经过一番处理后,把结果返 ...
- eclipse文本域内只能输入繁体中文
背景:在文本编辑器下写纯文本时不知怎么回事儿,原来能输入简体字,但过了之后只能输入繁体中文了!我用的是sogou拼音输入法,我检查过,输入法设置的是简体中文,eclipse默认的编码方式是utf-8. ...
- hdu 1573 X问题 不互质的中国剩余定理
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 关闭Android/iPhone浏览器自动识别数字为电话号码
<meta name="format-detection" content="telephone=no"><meta http-equiv=& ...
- 数据库查询Database中的表
public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...