sort &&qsort

来源:互联网 发布:小粉红 知乎 编辑:程序博客网 时间:2024/05/29 04:15

两个关于快排的函数,但是刚开始不会用,因此记录一下。

当在pascal里埋头苦干的前辈们还在为快排和堆排哪个效率更高争吵不休时,我们拥有了qsort。

当我还在为qsort里最后一个参数愁眉苦脸时,STL的工程师们发明了sort。

——Crux.D

qosrt:

 源自于C,但C标准未对其作出规定。有时算法会发生退化,效率较sort可能差一些。其包含在stdlib.h

函数原型:

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

Sort elements of array
Sorts the num elements of the array pointed by base, each element size bytes long, using the compar function to determine the order.

The sorting algorithm used by this function compares pairs of elements by calling the specified compar function with pointers to them as argument.

The function does not return any value, but modifies the content of the array pointed by base reordering its elements as defined by compar.

The order of equivalent elements is undefined.

给基底指向的数组进行排序,每个元素长度为1byte,用compare函数决定排列顺序。该函数无返回值,但会以compare确定的顺序修改base指向的数组的元素的顺序。

Parameters

base
Pointer to the first object of the array to be sorted, converted to a void*.
num
Number of elements in the array pointed by base.
size_t is an unsigned integral type.
size
Size in bytes of each element in the array.
size_t is an unsigned integral type.
compar
Pointer to a function that compares two elements.
This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:
 
int compar (const void* p1, const void* p2);
 

Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):
return valuemeaning<0The element pointed by p1 goes before the element pointed by p20The element pointed by p1 is equivalent to the element pointed by p2>0The element pointed by p1 goes after the element pointed by p2
For types that can be compared using regular relational operators, a general compar function may look like:

123456
int compareMyType (const void * a, const void * b){  if ( *(MyType*)a <  *(MyType*)b ) return -1;  if ( *(MyType*)a == *(MyType*)b ) return 0;  if ( *(MyType*)a >  *(MyType*)b ) return 1;}
 其实大可以改为 
int compareMyType (const void * a, const void * b){  return  *(MyType*)a <  *(MyType*)b ;}
qsort定义compare比较麻烦-_-#

int cmp(const void* x,const void* y){if((*(Mytype*)x).s==(*(Mytype*)y).s)return (*(Mytype*)x).b<(*(Mytype*)y).b;else return (*(Mytype*)x).s<(*(Mytype*)y).s;}
sort:

源自于stl模板,灵活,算法较为稳定。同qsort平均排序复杂度为n*log(n)。

函数原型:

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Sort elements in range

Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).

排列指定范围内的元素,第一个默认为运算符 "<" 排序,当你排序结构体时,你可以重载运算符进行排序,但是qsort'不行+_+。

或者你可以继续用自定义的compare。

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Parameters

first, last
Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.
首地址,末地址;注意这是半开半闭区间排序。
comp
Binary function that accepts two elements in the range as arguments, and returns a value convertible tobool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
自定义compare

0 0
原创粉丝点击