Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7538917
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-06-17 22:45:44

  1. /*
  2.  * 内存映射实现快速排序
  3.  * Lzy 2011-6-17
  4.  */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <sys/mman.h>

  9. #define DATA_NUM 100    //要进行排序的数据为1000个

  10. /*
  11.  * 一趟快速排序
  12.  * 取第一个为基准值
  13.  * 先从右向左扫描,找取第一个小于基准值的元素,交换位置
  14.  * 再从左向右扫描,找取第一个大于基准值的元素,交换位置
  15.  */
  16. int partion(int num[], int left, int right)
  17. {
  18.     int temp;
  19.     int pos = num[left];        //定义基准值

  20.     while(left < right)
  21.     {
  22.         while(left < right && num[right] >= pos)        //从右向左进行扫描,查找第一个小于基准值的数据元素
  23.         {
  24.             right--;
  25.         }
  26.         temp = num[left];            //数据交换
  27.         num[left] = num[right];
  28.         num[right] = temp;
  29.     
  30.         while(left < right && num[left] <= pos)    //从左向右进行扫描,查找第一个大于基准值的数据元素
  31.         {
  32.             left++;
  33.         }
  34.         temp = num[left];                        //数据交换
  35.         num[left] = num[right];
  36.         num[right] = temp;
  37.     }
  38.     return left;
  39. }

  40. /*
  41.  * 递归调用实现快速排序
  42.  */
  43. void qu_sort(int num[], int left, int right)
  44. {
  45.     int pos;

  46.     if(left < right)
  47.     {
  48.         pos = partion(num, left, right);        //划分左右子树
  49.         qu_sort(num, left, pos-1);                //递归调用,对左子树进行排序
  50.         qu_sort(num, pos+1, right);                //递归调用,对右子树进行排序
  51.     }
  52. }

  53. int main(void)
  54. {
  55.     int i, data;
  56.     int fd, length;    
  57.     
  58.     int *ptr;

  59.     fd = open("data", O_RDWR | O_CREAT, 0776);    //打开文件
  60.     if(fd < 0)
  61.     {
  62.         perror("open");
  63.         exit(0);
  64.     }

  65.     for(i = 0; i < DATA_NUM; i++)    //往文件中写入DATA_NUM个数据
  66.     {
  67.         data = rand() % 1000;            //产生随机数
  68.         write(fd, &data, sizeof(data));        //数据写入文件
  69.     }    
  70.     
  71.     ptr = mmap(NULL, DATA_NUM*sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);    //内存映射
  72.     if((int)ptr == -1)
  73.     {
  74.         perror("mmap");
  75.         exit(0);
  76.     }
  77.         
  78.     qu_sort(ptr, 0, DATA_NUM-1);                    //数据排序
  79.     
  80.     munmap(ptr, DATA_NUM*sizeof(int));                //取消映射    
  81.     
  82.     lseek(fd, 0, SEEK_SET);                            //读写位置指向文件头
  83.     for(i = 0; i < DATA_NUM; i++)                    //往文件中写入DATA_NUM个数据
  84.     {        
  85.         read(fd, &data, sizeof(data));                //数据写入文件
  86.         printf("%d ",data);
  87.     }
  88.     printf("\n");
  89.     
  90.     close(fd);
  91.     return 0;
  92. }
阅读(1837) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~