# 关于C++各类排序算法与std::sort性能的比较

2104人阅读 评论(0)

talk is cheap.直接放代码（C++）。

class Time_count//时间计时类，需要#include< ctime>
{
private:
clock_t start_,end_;
double duration;//记录时间
public:
Time_count()
{
start_=clock();
}
void show_time()
{
end_=clock();
duration = (double)(end_-start_)/CLOCKS_PER_SEC;
cout< < “spend time “< < duration< < ” seconds”< < endl< < endl;
}
};

# define LENGTH 10000//排序长度为10000的数组

int a[LENGTH+10];
srand(time(NULL));
for(int i=0;i< LENGTH;i++)
{
a[i]=rand()%10000+1;//随机生成1-10000之间的数字填充进数组
}

/*1.以下为插入排序/
Time_count t1;
insertion_sort(a);
cout< < “1.after insertion sort:”< < endl;
t1.show_time();
//print(a);
/*2.以下为冒泡排序/
Time_count t2;
bubble_sort(a);
cout< < “2.after bubble sort:”< < endl;
t2.show_time();
//print(a);
/*3.以下为选择排序/
Time_count t3;
selection_sort(a);
cout< < “3.after selection sort:”< < endl;
t3.show_time();
//print(a);
/*4.以下为归并排序/
Time_count t4;
merge_sort(a,0,LENGTH-1);
cout< < “4.after merge sort:”< < endl;
t4.show_time();
//print(a);
/*5.以下为快速排序/
Time_count t5;
quick_sort(a,0,LENGTH-1);
cout< < “5.after quick sort:”< < endl;
t5.show_time();
//print(a);
/*6.以下为STLsort排序/
Time_count t6;
STLsort(a);
cout< < “6.after STLsort:”< < endl;
t6.show_time();
//print(a);
system(“pause”);

//插入排序
void insertion_sort(int *a)
{
int key;
int i,j;
for(j=1;j< LENGTH;j++)
{
key=a[j];
i=j-1;
while(i>0&&a[i]>key)
{
a[i+1]=a[i];
i–;
}
a[i+1]=key;
}
}
//归并排序(注意！last应为a数组长度-1)
void merge_sort(int *a,int first,int last)
{
if (first< last)
{
int mid = (first + last) / 2;
merge_sort(a, first, mid); //左边有序
merge_sort(a, mid + 1, last); //右边有序
//再将二个有序数列合并
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
int *temp;
if((temp=(int*)malloc(LENGTH*sizeof(int)))==0)
{
cout< < “fuck you”< < endl;
exit(1);
}
while (i < = m && j < = n)
{
if (a[i] < = a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i < = m)
{
temp[k++] = a[i++];
}
while (j < = n)
{
temp[k++] = a[j++];
}
for (i = 0; i < k; i++)
{
a[first + i] = temp[i];
}
}
}

//快速排序
void quick_sort(int *a, int l, int r)
{
if (l < r)
{//Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1
int i = l, j = r, x = a[l];
while (i < j)
{
while(i < j && a[j] >= x) // 从右向左找第一个小于x的数
{
j–;
}
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x) // 从左向右找第一个大于等于x的数
{
i++;
}
if(i < j)
a[j–] = a[i];
}
a[i] = x;
quick_sort(a, l, i - 1); // 递归调用
quick_sort(a, i + 1, r);
}
}

STL的sort函数还是傲视群雄。根本没用多少时间。

template< class _RanIt,
class _Diff,
class _Pr> inline
void _Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal, _Pr _Pred)
{ // order [_First, _Last), using _Pred
_Diff _Count;
for (; _ISORT_MAX < (_Count = _Last - _First) && 0 < _Ideal; )
{ // divide and conquer by quicksort
pair< _RanIt, _RanIt> _Mid =
_Unguarded_partition(_First, _Last, _Pred);
_Ideal /= 2, _Ideal += _Ideal / 2; // allow 1.5 log2(N) divisions

    if (_Mid.first - _First <  _Last - _Mid.second)
{   // loop on second half
_Sort(_First, _Mid.first, _Ideal, _Pred);
_First = _Mid.second;
}
else
{   // loop on first half
_Sort(_Mid.second, _Last, _Ideal, _Pred);
_Last = _Mid.first;
}
}

if (_ISORT_MAX <  _Count)
{   // heap sort if too many divisions
_STD make_heap(_First, _Last, _Pred);
_STD sort_heap(_First, _Last, _Pred);
}
else if (2 < = _Count)
_Insertion_sort(_First, _Last, _Pred);  // small
}


template< class _RanIt,
class _Pr> inline
void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
{ // order [_First, _Last), using _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER_IF(2 < = _Last - _First, _Pred);
_Sort(_Unchecked(_First), _Unchecked(_Last), _Last - _First, _Pred);
}

    // TEMPLATE FUNCTION sort


template< class _RanIt> inline
void sort(_RanIt _First, _RanIt _Last)
{ // order [_First, _Last), using operator<
_STD sort(_First, _Last, less< >());
}

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2357次
• 积分：63
• 等级：
• 排名：千里之外
• 原创：3篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
评论排行