交换排序

代码

  1. #include <stdio.h>
  2.  
  3. void printList(int iList[], int iLen)
  4. {
  5. int i = ;
  6. for(i = ; i < iLen; i++)
  7. {
  8. printf("%d ", iList[i]);
  9. }
  10. printf("\n");
  11. }
  12.  
  13. int exchangeSort(int iList[], int iNum)
  14. {
  15. int i = , j = ;
  16. for(i = ; i < iNum - ; i++)
  17. {
  18. int k = ;
  19. for(j = i + ; j < iNum; j++)
  20. {
  21. if(iList[j] < iList[i])
  22. {
  23. int iX = iList[j];
  24. iList[j] = iList[i];
  25. iList[i] = iX;
  26. }
  27. k++;
  28. printf(" .%d: ", k);
  29. printList(iList, iNum);
  30. }
  31. printf("%d : ", i + );
  32. printList(iList, iNum);
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38. int iNum = ;
  39. int iList[] = {, , , , , , , , , };
  40. printf("src : ");
  41. printList(iList, iNum);
  42. putchar('\n');
  43. exchangeSort(iList, iNum);
  44. putchar('\n');
  45. printf("dst : ");
  46. printList(iList, iNum);
  47.  
  48. return ;
  49. }

编译

  1. $ g++ -o exchangeSort exchangeSort.cpp

运行

  1. $ ./exchangeSort
  2. src :
  3.  
  4. .: 7 9
  5. .: 5 7
  6. .: 3 5
  7. .: 0 3
  8. .: //此时已经没有比 iList[0] 元素更小的了,所以后面几次都没有进行任何元素交换
  9. .:
  10. .:
  11. .:
  12. .:
  13. : //此时 iList[0]元素已经稳定,下面开始寻找第二小的元素,并不断与 iList[1]进行交换
  14. .: 7 9
  15. .: 5 7
  16. .: 3 5
  17. .: 1 3
  18. .: //此时已经没有比 iList[1] 元素更小的了,所以后面几次都没有进行任何元素交换
  19. .:
  20. .:
  21. .:
  22. : //此时 iList[0]元素和iList[1]元素已经稳定,下面开始寻找第三小的元素,并不断与 iList[2]进行交换
  23. .: 7 9
  24. .: 5 7
  25. .: 3 5
  26. .: 2 3
  27. .: //
  28. .:
  29. .:
  30. : // iList[2]稳定
  31. .: 7 9
  32. .: 5 7
  33. .: 3 5
  34. .:
  35. .:
  36. .:
  37. : //iList[3]稳定
  38. .: 7 9
  39. .: 5 7
  40. .: 4 5
  41. .:
  42. .:
  43. : //iList[4]稳定
  44. .: 7 9
  45. .: 5 7
  46. .:
  47. .:
  48. : //iList[5]稳定
  49. .: 7 9
  50. .: 6 7
  51. .:
  52. : //iList[6]稳定
  53. .: 7 9
  54. .:
  55. : //iList[7]稳定
  56. .: 8 9
  57. : //iList[8]稳定
  58. //根据抽屉原理,iList[9]只能是稳定的了 ^_^
  59. 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. go sample - hello world

    入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...

  2. [Outlook]设置邮件自动接收时间

    [Outlook]设置邮件自动接收时间   找了好久,一直都没设置正常,导致老是收到邮件有延迟,今天头脑清晰,搜了一下,然后自己竟然给找到了,记下来当笔记,好记性不如烂笔头,呵呵   搜索百度&quo ...

  3. Java中synchronized详解

    synchronized 原则: 尽量避免无谓的同步控制,同步需要系统开销,可能造成死锁 尽量减少锁的粒度 同步方法 public synchronized void printVal(int v) ...

  4. 驱动中获取PsActiveProcessHead变量地址的五种方法也可以获取KdpDebuggerDataListHead

    PsActiveProcessHead的定义: 在windows系统中,所有的活动进程都是连在一起的,构成一个双链表,表头是全局变量PsActiveProcessHead,当一个进程被创建时,其Act ...

  5. cdoj1324暴力分块

    #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> ...

  6. supervisor(三)xml_rpc

    supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,然后函数方法经过一番处理后,把结果返 ...

  7. eclipse文本域内只能输入繁体中文

    背景:在文本编辑器下写纯文本时不知怎么回事儿,原来能输入简体字,但过了之后只能输入繁体中文了!我用的是sogou拼音输入法,我检查过,输入法设置的是简体中文,eclipse默认的编码方式是utf-8. ...

  8. hdu 1573 X问题 不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. 关闭Android/iPhone浏览器自动识别数字为电话号码

    <meta name="format-detection" content="telephone=no"><meta http-equiv=& ...

  10. 数据库查询Database中的表

    public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...