快速排序算法的实现

#include<iostream>
#include<math.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
//产生(begin,end)区间的随机数
int myrand(int begin,int end)

 srand(unsigned(time(NULL)));
    int i=(rand() % (end-begin+1))+begin;
 
 
 return i;

}

//交换两个数
void swap(int &a,int& b)
{
 int temp=a;
 a=b;
 b=temp;

}
 
//找到分割点
int Partition(int *a,int begin,int end)
{ void print(int *a,int n);
 int i=begin-1;
 int x=a[end];
 for(int j=begin;j<end;j++)
 {
  if(a[j]<=x)
  {
   i++;
   swap(a[i],a[j]);
  }
 
 }
 swap(a[i+1],a[end]);
 return i+1;
}

 


//随机找到分割点

int Randomized_Partition(int *a,int begin,int end)
{  void print(int *a,int n);
   int i=myrand(begin,end);
    swap(a[i],a[end]);
 
   return Partition(a,begin,end);

}
//快速排序随机算法
void Randomized_QuickSort(int *a,int begin,int end)
{
 if (begin<end)
 {
 int q=Randomized_Partition(a,begin,end);
     //int q=Partition(a,begin,end);

  Randomized_QuickSort(a,begin,q-1);
  Randomized_QuickSort(a,q+1,end);
 }

}
void print(int *a ,int n)
{
 for(int k=0;k<n;k++)
 {
  cout<<a[k]<<" ";
 }
 cout<<"\n";
}

int  main()
{  
 int a[7]={6,4,2,9,1,10,0};
 Randomized_QuickSort(a,0,6);
 /*int j= Randomized_Partition(a,0,6);
 print(a,7);
 cout<<j<<endl;*/
 print(a,7);
 int k;
 cin>>k;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值