Chinaunix首页 | 论坛 | 博客
  • 博客访问: 584684
  • 博文数量: 92
  • 博客积分: 5026
  • 博客等级: 大校
  • 技术积分: 1321
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-28 11:04
文章分类

全部博文(92)

文章存档

2011年(9)

2010年(17)

2009年(12)

2008年(54)

我的朋友

分类: C/C++

2008-03-28 16:35:25

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void quick_sort(int* buf, int len) {
 int  base  = buf[0];
 int* free_space = buf;
 int* start = buf+1;
 int* end   = buf+len-1;
 int bigger_num = 0;
 int less_num = 0;
 bool compare_from_end = true;
 for(int i=0; i  if(compare_from_end) {
   if(*end < base) {
    *free_space = *end;
    free_space = end;
    compare_from_end = false;
    less_num++;
   } else {
    bigger_num++;
   }
   end--;
  } else {
   if(*start > base) {
    *free_space = *start;
    free_space = start;
    compare_from_end = true;
    bigger_num++;
   } else {
    less_num++;
   }
   start++;
  }
 }
 *free_space = base;
 if(less_num > 1)quick_sort(buf, less_num);
 if(bigger_num > 1)quick_sort(buf+less_num+1, bigger_num);
}
int main() {
 int arr[100000];
 int len = sizeof(arr)/4;
 quick_sort(arr, len);
 for(int i=0; i  printf("%d\t", arr[i]);
 }
 printf("\n");
 getchar();
 return 0;
}
阅读(814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~