C++快速排序

来源:互联网 发布:腾讯云阿里云市场份额 编辑:程序博客网 时间:2024/06/06 19:34

转载自:http://baike.baidu.com/view/19016.htm


C++,递归快排(非随机)

  #include <iostream> 

 using namespace std;  

int a[50];  

void swap(int *pLeft,int *pRight) 

 {  int temp;  temp = *pLeft;  *pLeft= *pRight;  *pRight = temp;  } 

 void qs(int begin,int end) 

 { 

 int compare=a[begin], left =begin,right = end;  if(left >right)  return;  

while (left <right)  {  while ((left <right) && a[right]>=compare)  right--;

  //a[left] = a[right]; 

 swap(a[left],a[right]);  while ((left <right) &&(a[left] <compare))  left++;  

//a[right] = a[left];  

swap(a[right],a[left]);  }  a[right] = a[left];  qs(begin,right-1);  qs(right+1,end); 

 }  


int main()  {  int i;  for (i =0;i< 50;i++)  {  a[i] =50-i;  }  cout<<"排序前"<<endl;  for (i= 0;i<50;i++)  {  cout<<"a["<<i<<"] = "<<a[i]<<endl;  }  cout<<"排序后"<<endl;  qs(0,49);  for (i=0;i<50;i++)  {  cout<<"a["<<i<<"] = "<<a[i]<<endl;  }  system("pause");  return 0;  }

C++,快排(函数)

  在c++中可以用函数qsort()可以直接为数组进行排序。  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));   参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序  qsort 的函数原型是void __cdecl qsort ( void *base, size_t num, size_t width, int (__cdecl *comp)(const void *, const void* ) )   其中base是排序的一个集合数组,num是这个数组元素的个数,width是一个元素的大小,comp是一个比较函数。   比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。   qsort(a,1000,sizeof(int ),comp);   其中comp函数应写为:   int comp(const void *a,const void *b)   {   return *(int *)a-*(int *)b;   }   上面是由小到大排序,return *(int *)b-*(int *)a; 为由大到小排序。  很显然快速排序在对于大的、乱数列表一般相信是最快的已知排序。所以在c++的函数库里,已经为我们写好此函数。  在c++中我们也可以自己写快速排序的函数源代码:  void sort(int *a,int x,int y)  {  int xx=x,yy=y;  int k=a[x];  if(x>=y) return ;  while(xx!=yy)  {  while(xx<yy&& a[yy]>=k)yy--;  a[xx]=a[yy];  while(xx<yy&& a[xx]<=k) xx++;  a[yy]=a[xx];  }  a[xx]=k;  sort(a,x,xx-1);  sort(a,xx+1,y);  }

C

  //参照《数据结构》(C语言版)  //调用:quicksort-->qsort-->partitions  int partitions(int a[],int low,int high)  {  int pivotkey=a[low];  //a[0]=a[low];  while(low<high)  {  while(low<high && a[high]>=pivotkey)  --high;  a[low]=a[high];  while(low<high && a[low]<=pivotkey)  ++low;  a[high]=a[low];  }  //a[low]=a[0];  a[low]=pivotkey;  return low;  }  void qsort(int a[],int low,int high)  {  int pivottag;  if(low<high)  { //递归调用  pivottag=partitions(a,low,high);  qsort(a,low,pivottag-1);  qsort(a,pivottag+1,high);  }  }  void quicksort(int a[],int n)  {  qsort(a,0,n);  }  //简单示例  #include <stdio.h>   //#include <math.h>  #include "myfunc.h" //存放于个人函数库中  main()  {  int i,a[11]={0,11,12,5,6,13,8,9,14,7,10};  for(i=0;i<11;printf("%3d",a[i]),++i);  printf("\n");  quicksort(a,10);  for(i=0;i<11;printf("%3d",a[i]),++i);  printf("\n");  }

C++,递归快排(随机)

  #include<iostream>  using namespace std;  void QuickSort(int *pData,int left,int right)  {  int i(left),j(right),middle(0),iTemp(0);  middle=pData[(left+right)/2];//求中间值  middle=pData[(rand()%(right-left+1))+left]; //生成大于等于left小于等于right的随机数  do{  while((pData[i]<middle)&&(i<right))//从左扫描大于中值的数  i++;  while((pData[j]>middle) && (j>left))//从右扫描小于中值的数  j--;  //找到了一对值,交换  if(i<=j){  iTemp=pData[j];  pData[j]=pData[i];  pData[i]=iTemp;  i++;  j--;  }  }while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)  //当左边部分有值(left<j),递归左半边  if(left<j){  QuickSort(pData,left,j);  }  //当右边部分有值(right>i),递归右半边  if(right>i){  QuickSort(pData,i,right);  }  }  int main()  {  int data[]={10,9,8,7,6,5,4};  const int count(6);  QuickSort(data,0,count);  for(int i(0);i!=7;++i){  cout<<data[i]<<;“ ”<<flush;  }  cout<<endl;  return 0;  }



随机快排推荐程序

C++语言   #include <iostream>  #include <ctime>  using namespace std;  int partion(int a[],int p,int r)  {  //rand  srand( (unsigned)time( NULL ) );  int e=rand()%(r-p+1)+p;  int tem;  tem=a[e];  a[e]=a[r];  a[r]=tem;  int x=a[r];  int i=p-1;  for (int j=p;j<r-1;j++)  {  if (a[j]<=x)  {  tem=a[i+1];  a[i+1]=a[j];  a[j]=tem;  i++;  }   }  tem=a[r];  a[r]=a[i+1];  a[i+1]=tem;  return i+1;  }  void QuickSort(int a[],int p,int r)  {  if (p<r)  {  int q=partion(a,p,r);  QuickSort(a,p,q-1);  QuickSort(a,q+1,r);  }   }  int main()  {  int array[]={0,-2,11,-4,13,-5,14,-43};  QuickSort(array,0,7);  for(int i=0;i<7;i++)  cout<<array[i]<<" ";  cout<<endl;  return 0;   }