由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 我写的quick sort
相关主题
Partitioning (转载)这两个地方是否需要typename?
两行quicksort,不难些吧请教一个初级算法问题 (转载)
underlying sort algorithm for SET in STL?算法之极弱问
嵌入式系统用什么sorting算法比较好?问一个严肃的实用问题
私有成员不能用类成员函数修改?typedef 的一个问题
这段 C++ 怎么改才能编译?哪位大侠给说说 何时用 merge sort, 何时用 quick sort, 何时
C++ linking 弱问 (one file)如何 define/initialize static data member of a class templ
C++里get array size的问题 (转载)我也来一个, quick sort 只要一行。
相关话题的讨论汇总
话题: down话题: up话题: pivot话题: int话题: quicksort
进入Programming版参与讨论
1 (共1页)
O*******d
发帖数: 20343
1
贴在这里,面试时也许用得上
void quickSort(int * a, int len)
{
if(len > 1)
{
int pivot;
int *up, *down;
int c;
/* choose the first element as pivot */
pivot = *a;
/* set up and down */
up = a + 1;
down = a + len - 1;
while(up < down)
{
/* partition */
while(up < down && *up <= pivot)
++up;
while(up < down && *down > pivot)
--down;
if(up < down)
{
c = *up;
*up = *down;
p*u
发帖数: 2454
2
#include
#include
#include
// Change this to change the number of elements to be stored and sorted
#define ARRAY_LENGTH 128
template
void quickSort(Type* array, int low, int high)
{
int pivotPosition = -1;
if ( low < high )
{
pivotPosition = partition(array, low, high);
quickSort(array, low, pivotPosition-1);
quickSort(array, pivotPosition+1, high);
b********n
发帖数: 609
3
这个好,more generic

【在 p*u 的大作中提到】
: #include
: #include
: #include
: // Change this to change the number of elements to be stored and sorted
: #define ARRAY_LENGTH 128
: template
: void quickSort(Type* array, int low, int high)
: {
: int pivotPosition = -1;
: if ( low < high )

O*******d
发帖数: 20343
4
赞。 你的思路是用两个index来作partition起点和终点, 而array的起始点不变。 我
的思路是把每个partition的起点和长度传给下一个recursive call。
1 (共1页)
进入Programming版参与讨论
相关主题
我也来一个, quick sort 只要一行。私有成员不能用类成员函数修改?
新手学JAVA,遇到一个难题,有大侠愿意帮忙吗?这段 C++ 怎么改才能编译?
[合集] 答案. 未排序的100个数字,如何最快地找出最大的5个C++ linking 弱问 (one file)
Is it safe to use unique_ptr with STL container?C++里get array size的问题 (转载)
Partitioning (转载)这两个地方是否需要typename?
两行quicksort,不难些吧请教一个初级算法问题 (转载)
underlying sort algorithm for SET in STL?算法之极弱问
嵌入式系统用什么sorting算法比较好?问一个严肃的实用问题
相关话题的讨论汇总
话题: down话题: up话题: pivot话题: int话题: quicksort