- void swap(float &a,float& b)
- {
- float c=a;
- a=b;
- b=c;
- }
- void swap(int &a,int &b)
- {
- int c=a;
- a=b;
- b=c;
- }
- void quick_sort(float* buf,const int begin,const int end) //buf from 'begin' to 'end -1'
- {
- if( (end-begin)<=1) return;
- int index=begin;
- int i=end-1;
- while(index!=i)
- {
- if(i>index)
- {
- if(buf[i]<buf[index])
- {
- swap(buf[i],buf[index]);
- swap(i,index);
- i++;
- }else
- i--;
- }
- else if(i<index)
- {
- if(buf[i]>buf[index])
- {
- swap(buf[i],buf[index]);
- swap(i,index);
- i--;
- }else
- i++;
- }
- }
- quick_sort(buf,begin,index-1);
- quick_sort(buf,index+1,end);
- }
- //该片段来自于http://www.codesnippet.cn/detail/0608201513365.html
来源: http://www.codesnippet.cn/detail/0608201513365.html