Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112741
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-07-26 10:12:32

1. 选择排序
由小到大排序的过程
第一趟相当于找到最小值并放在首位   
第二趟相当于找到次小值,并放在第2的位置上    
以此类推
b. 与冒泡排序的区别
在找最小数的同时冒泡排序会交换数据,而选技排序不会交换只是查找最小数,找到了才交换
下面是冒泡的理解:
 第1个数与剩下的n-1个数比较,若有比第1个数小的就交换,让第1个数始终存本趟最小的数
  第2个数与剩下的n-2个数比较,若有比第2个数小的就交换,让第2个数始终存本趟最小的数

1.2 代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
  4. #define SWAP(x,y) (x=(x)+(y),y=(x)-(y),x=(x)-(y))

  5. int dum_array(int* arr, int len)
  6. {
  7.     int i;
  8.     for(i=0; i<len; i++)
  9.     {
  10.         //printf("%d=%d ", i, arr[i]);
  11.         printf("%d ", arr[i]);
  12.     }
  13.     printf("\n");
  14.     return 0;
  15. }

  16. int select_min_index(int*arr, int start, int end)
  17. {
  18.    int i,j;
  19.    int index = start;
  20.    dbmsg("s=%d,e=%d", start, end);
  21.    for(i=start+1; i<end; i++)
  22.        if(arr[i] < arr[index])
  23.            index = i;
  24.    return index;
  25. }

  26. int select_sort(int* arr, int len)
  27. {
  28.     int i,j;
  29.     int key;
  30.     if(len <= 0)
  31.         return 0;
  32.     for(i=0; i<len; i++)
  33.     {
  34.         j = select_min_index(arr, i, len);  //在剩下的数据中选出最小
  35.         dbmsg("j=%d", j);                   //与剩下的数据中的第一个相比较,不同则交换
  36.         if(i != j)
  37.             SWAP(arr[i], arr[j]);
  38.         dum_array(arr, len);
  39.     }
  40.     return 0;
  41. }
  42. int main ( int argc, char *argv[] )
  43. {
  44.     //int arr[] = {49, 38, 65, 97, 76, 13, 27, 49};
  45.     int arr[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
  46.     int len = sizeof(arr)/sizeof(int);
  47.     dbmsg("len=%d", len);
  48.     dbmsg("before sort:");
  49.     dum_array(arr, len);
  50.     select_sort(arr, len);
  51.     dbmsg("after sort:");
  52.     dum_array(arr, len);
  53.     return EXIT_SUCCESS;
  54. }
1.3 运行结果
  1. select.c:main[51]: before sort:
    49  38  65  97  76  13  27  49  55  4  
    select.c:select_min_index[22]: s=0,e=10
    select.c:select_sort[38]: j=9
    4  38  65  97  76  13  27  49  55  49  
    select.c:select_min_index[22]: s=1,e=10
    select.c:select_sort[38]: j=5
    4  13  65  97  76  38  27  49  55  49  
    select.c:select_min_index[22]: s=2,e=10
    select.c:select_sort[38]: j=6
    4  13  27  97  76  38  65  49  55  49  
    select.c:select_min_index[22]: s=3,e=10
    select.c:select_sort[38]: j=5
    4  13  27  38  76  97  65  49  55  49  
    select.c:select_min_index[22]: s=4,e=10
    select.c:select_sort[38]: j=7
    4  13  27  38  49  97  65  76  55  49  
    select.c:select_min_index[22]: s=5,e=10
    select.c:select_sort[38]: j=9
    4  13  27  38  49  49  65  76  55  97  
    select.c:select_min_index[22]: s=6,e=10
    select.c:select_sort[38]: j=8
    4  13  27  38  49  49  55  76  65  97  
    select.c:select_min_index[22]: s=7,e=10
    select.c:select_sort[38]: j=8
    4  13  27  38  49  49  55  65  76  97  
    select.c:select_min_index[22]: s=8,e=10
    select.c:select_sort[38]: j=8
    4  13  27  38  49  49  55  65  76  97  
    select.c:select_min_index[22]: s=9,e=10
    select.c:select_sort[38]: j=9
    4  13  27  38  49  49  55  65  76  97  
    select.c:main[54]: after sort:
    4  13  27  38  49  49  55  65  76  97
1.4 性能
O(n2)
1.5 代码打包
select.rar    (下载后改名为select.tar.gz)

阅读(1131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~