快速排序转冒泡排序的临界值

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include <memory.h>

inline __int64 GetCpuCounter(){
   __asm _emit 0x0F
   __asm _emit 0x31
}

template <class Elem>
void swap(Elem &e1,Elem &e2){
   Elem temp = e2;
   e2 = e1;
   e1 = temp;
}

template <class Elem>
void bubSort(Elem A[], int n){
   for(int i =0; i <n-1; i++)
      for(int j = n-1; j > i; j--) //bubble up i'th element

         if(A[j] < A[j-1])
            swap<Elem>(A[j],A[j-1]);
}


template<class Elem>
int partition(Elem A[],int left, int right) //right一定大于left

{
   Elem temp = A[left];
   do{
      // 循环找出右边起第一个比temp小的数

      while((right > left)&&(A[--right] > temp));
      if(left < right)
         swap<Elem>(A[right], A[left]); // 将此数和A[left]交换

      // 循环找出左边起第一个比temp大的数

      while((left < right)&&(A[++left] < temp));
      if(left < right)
         swap<Elem>(A[right], A[left]);

   }
   while(left < right);
   return left;
}

template<class Elem, int THRESHOLD>
void qSort(Elem A[],int left,int right){
   if(left >= right)
      return;
   int size = right - left + 1;
   if(size <= THRESHOLD){
      bubSort<Elem>(&A[left], size);
      return;
   }
   if(A[left] > A[right]) //让A[left]和A[right-1]以前的元素比较

      swap<Elem>(A[right], A[left]);
   int k = partition<Elem>(A,left,right);
   qSort<Elem, THRESHOLD>(A,left,k-1);
   qSort<Elem, THRESHOLD>(A,k+1,right);
}

#define TestThreshold(a, size, startThreshold, conter1, counter2, passed) \
conter1 = GetCpuCounter(); \
qSort<int, startThreshold>(a, 0, size - 1); \
counter2 = GetCpuCounter(); \
passed = counter2 - conter1; \
printf("Threshold:%2d cpu Counter: %ld\n", startThreshold, passed);


int main(){
   const int arraySize = 100000;
   int origin[arraySize];
   srand(time(0));
   for(int n = 0; n < arraySize; n++){
      origin[n] = rand() % 10000000;
   }
   int a[arraySize];
  
   //

   __int64 counter1;
   __int64 counter2;
   __int64 passed;

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold4 = 4;
   TestThreshold(a, arraySize, threshold4, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold5 = 5;
   TestThreshold(a, arraySize, threshold5, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold6 = 6;
   TestThreshold(a, arraySize, threshold6, counter1, counter2, passed);
   
   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold7 = 7;
   TestThreshold(a, arraySize, threshold7, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold8 =8;
   TestThreshold(a, arraySize, threshold8, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold9 = 9;
   TestThreshold(a, arraySize, threshold9, counter1, counter2, passed);
 
   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold10 = 10;
   TestThreshold(a, arraySize, threshold10, counter1, counter2, passed);
   
   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold11 = 11;
   TestThreshold(a, arraySize, threshold11, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold12 = 12;
   TestThreshold(a, arraySize, threshold12, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold13 = 13;
   TestThreshold(a, arraySize, threshold13, counter1, counter2, passed);
  
   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold14 = 14;
   TestThreshold(a, arraySize, threshold14, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold15 = 15;
   TestThreshold(a, arraySize, threshold15, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold16 = 16;
   TestThreshold(a, arraySize, threshold16, counter1, counter2, passed);

   memcpy(a, origin, sizeof(int) * arraySize);
   const int threshold17 = 17;
   TestThreshold(a, arraySize, threshold17, counter1, counter2, passed);

   
  getchar();
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值