Chinaunix首页 | 论坛 | 博客
  • 博客访问: 174570
  • 博文数量: 39
  • 博客积分: 929
  • 博客等级: 准尉
  • 技术积分: 500
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-06 11:16
个人简介

文章分类

全部博文(39)

文章存档

2013年(3)

2012年(28)

2011年(8)

我的朋友

分类: C/C++

2012-11-07 18:33:54



点击(此处)折叠或打开

  1. 1 #include <stdio.h>
  2.       2 #include <stdlib.h>
  3.       3 #include <unistd.h>
  4.       4
  5.       5 int a[10] = {49,38,65,97,76,13,27,22,15,87};
  6.       6
  7.       7 void qs(begin,end){
  8.       8 int key = a[begin],l_hold = begin,r_hold = end;
  9.       9 while(begin<end){
  10.      10 //first from right,find one that < key
  11.      11 while(begin < end && a[end] >= key){
  12.      12 end--;
  13.      13 }
  14.      14 //if they do not point to the same
  15.      15 if(begin != end){
  16.      16 a[begin++] = a[end];
  17.      17 }
  18.      18
  19.      19 //the next step we from left,find one that > key
  20.      20 while(begin < end && a[begin] <= key){
  21.      21 begin++;
  22.      22 }
  23.      23 //if they do not point to the same
  24.      24 if(begin != end){
  25.      25 a[end--] = a[begin];
  26.      26 }
  27.      27 }
  28.      28 //we get begin == end now,put the key in this place
  29.      29 a[begin] = key;
  30.      30 //now position to middle
  31.      31 int middle = begin;
  32.      32 //1rst sort over,a is splited into [section1(l_hold,P-1),(P+1,r_hold)seciont2()]
  33.      33 //for left
  34.      34
  35.      35 int i = 0;
  36.      36 for(;i<10;i++){
  37.      37 printf("\t%d",a[i]);
  38.      38 }
  39.      39 printf("\n");
  40.      40
  41.      41 if(l_hold < middle)
  42.      42 qs(l_hold,middle-1);
  43.      43 //for right
  44.      44 if(r_hold > middle)
  45.      45 qs(middle+1,r_hold);
  46.      46 }
  47.      47
  48.      48 int
  49.      49 main(){
  50.      50 int i = 0;
  51.      51 for(;i<10;i++){
  52.      52 printf("\t%d",a[i]);
  53.      53 }
  54.      54 printf("\n");
  55.      55
  56.      56 qs(0,9);
  57.      57 }

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