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 value meaning <0
The element pointed by p1 goes before the element pointed by p20
The element pointed by p1 is equivalent to the element pointed by p2>0
The 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;}
qsort定义compare比较麻烦-_-#int compareMyType (const void * a, const void * b){ return *(MyType*)a < *(MyType*)b ;}
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;}
源自于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 to
bool
. 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
- qsort sort
- sort &&qsort
- SORT QSORT
- sort---qsort
- qsort sort
- 关于sort 和 qsort
- qsort() 和 sort()
- qsort和sort
- [排序]SORT QSORT
- sort与qsort函数
- sort与qsort
- sort qsort的区别
- sort qsort 小结
- sort()和qsort()
- sort和qsort排序
- qsort与sort
- qsort与sort
- (转)[排序]SORT QSORT
- IIS重装asp.net程序无法运行
- ExtJS4.2 文本框中多个验证的提示信息显示问题
- Overlay网络和VXLAN
- 代理模式
- BloomFilter–大规模数据处理利器(解决空查问题)(包含BloomFilter算法思想的演变:实例4)
- sort &&qsort
- 13. Java类集 Part 2(Map接口、SortedMap接口、集合工具类Collection、Stack类、属性类Properties) ----- 学习笔记
- Menu控件在Chrome,Safari,360浏览器显示不正常的解决方案
- F12 点开的童话——chrome开发者工具使用基础
- Swoole源码学习记录(二)——三种MemoryPool(上)
- linux内存管理浅析
- hdu 4911 Inversion ( 2014 Multi-University Training Contest 5)
- Hdu 2196 Computer (树形dp)
- JAVA注释格式说明