免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3798 | 回复: 4
打印 上一主题 下一主题

[C] 我的C语言快速排序方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-24 20:51 |只看该作者 |倒序浏览
// test0.cpp : 定义控制台应用程序的入口点。

//此教程来源于ChinaUnix論壇(http://bbs.chinaunix.net/

//查看完整的教程请点:http://bbs.chinaunix.net/thread-1296108-1-1.html

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iterator>
//随机选取中间数

template<typename T>
int select(T data[], int low, int high)
{
int middle=low+rand()%(high-low);
std::swap(data[middle], data[low]);
int i=low+1;
int j=high;
while ( i<=j )
{
&nbsp;&nbsp;while ( data[i]<data[low] )
&nbsp;&nbsp;&nbsp;++i;
&nbsp;&nbsp;while ( data[low]<data[j] )
&nbsp;&nbsp;&nbsp;--j;
&nbsp;&nbsp;if (i<j)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;std::swap(data[i], data[j]);
&nbsp;&nbsp;&nbsp;++i;
&nbsp;&nbsp;&nbsp;--j;
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;++i;
}
std::swap(data[low], data[j]);
return j;
}
//快速排序函数

template <typename T>
void QuickSorter (T data[], int low, int high)
{
if ( low<high )
{
&nbsp;&nbsp;int n=select(data, low, high);
&nbsp;&nbsp;QuickSorter(data, low, n-1);
&nbsp;&nbsp;QuickSorter(data, n+1, high);
}
}
//快速排序包装器

template <typename T>
void QuickSort(T data[], int size)
{
srand(time(NULL));
QuickSorter(data, 0, size-1);
}
//测试例程

int _tmain(int argc, _TCHAR* argv[])
{
int data[10000];
for ( int i=0; i<10000; ++i )
{
&nbsp;&nbsp;data[i]=rand()%1000;
}
QuickSort(data, 10000);
std::copy(data, data+10000, std::ostream_iterator<int>(std::cout, " "));
return 0;
}


[ 本帖最后由 langue 于 2008-10-25 08:02 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-10-25 10:43 |只看该作者
cxx吧,不是c语言

论坛徽章:
0
3 [报告]
发表于 2008-10-27 14:51 |只看该作者
顶了再说

论坛徽章:
0
4 [报告]
发表于 2008-10-27 15:22 |只看该作者
抄来的c++快速排序方法(stl_algo.h):


  1.   template<typename _RandomAccessIter>
  2.     inline void
  3.     sort(_RandomAccessIter __first, _RandomAccessIter __last)
  4.     {
  5.       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;

  6.       // concept requirements
  7.       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
  8.             _RandomAccessIter>)
  9.       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)

  10.       if (__first != __last) {
  11.         __introsort_loop(__first, __last, __lg(__last - __first) * 2);
  12.         __final_insertion_sort(__first, __last);
  13.       }
  14.     }

  15.   template<typename _RandomAccessIter, typename _Size>
  16.     void
  17.     __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last,
  18.                      _Size __depth_limit)
  19.     {
  20.       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;

  21.       while (__last - __first > _M_threshold) {
  22.         if (__depth_limit == 0) {
  23.           partial_sort(__first, __last, __last);
  24.           return;
  25.         }
  26.         --__depth_limit;
  27.         _RandomAccessIter __cut =
  28.           __unguarded_partition(__first, __last,
  29.                                 _ValueType(__median(*__first,
  30.                                                     *(__first + (__last - __first)/2),
  31.                                                     *(__last - 1))));
  32.         __introsort_loop(__cut, __last, __depth_limit);
  33.         __last = __cut;
  34.       }
  35.     }

  36.   template<typename _RandomAccessIter>
  37.     void
  38.     __final_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last)
  39.     {
  40.       if (__last - __first > _M_threshold) {
  41.         __insertion_sort(__first, __first + _M_threshold);
  42.         __unguarded_insertion_sort(__first + _M_threshold, __last);
  43.       }
  44.       else
  45.         __insertion_sort(__first, __last);
  46.     }

  47.   template<typename _RandomAccessIter>
  48.     void
  49.     partial_sort(_RandomAccessIter __first,
  50.                  _RandomAccessIter __middle,
  51.                  _RandomAccessIter __last)
  52.     {
  53.       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;

  54.       // concept requirements
  55.       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
  56.             _RandomAccessIter>)
  57.       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)

  58.       make_heap(__first, __middle);
  59.       for (_RandomAccessIter __i = __middle; __i < __last; ++__i)
  60.         if (*__i < *__first)
  61.           __pop_heap(__first, __middle, __i, _ValueType(*__i));
  62.       sort_heap(__first, __middle);
  63.     }

  64.   template<typename _RandomAccessIter, typename _Tp>
  65.     _RandomAccessIter
  66.     __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last,
  67.                           _Tp __pivot)
  68.     {
  69.       while (true) {
  70.         while (*__first < __pivot)
  71.           ++__first;
  72.         --__last;
  73.         while (__pivot < *__last)
  74.           --__last;
  75.         if (!(__first < __last))
  76.           return __first;
  77.         iter_swap(__first, __last);
  78.         ++__first;
  79.       }
  80.     }
复制代码

[ 本帖最后由 lgfang 于 2008-10-27 15:30 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-10-27 17:12 |只看该作者
连c和c++都分不清。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP