Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
4b4931a0
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4b4931a0
编写于
2月 06, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成sort
上级
b1b65935
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
83 addition
and
2 deletion
+83
-2
TinySTL/Algorithm.h
TinySTL/Algorithm.h
+57
-2
TinySTL/Test/AlgorithmTest.cpp
TinySTL/Test/AlgorithmTest.cpp
+24
-0
TinySTL/Test/AlgorithmTest.h
TinySTL/Test/AlgorithmTest.h
+2
-0
未找到文件。
TinySTL/Algorithm.h
浏览文件 @
4b4931a0
...
...
@@ -457,8 +457,7 @@ namespace TinySTL{
while
(
n
++
){
--
it
;
}
}
else
{
}
else
{
while
(
n
--
){
++
it
;
}
...
...
@@ -478,6 +477,62 @@ namespace TinySTL{
typedef
iterator_traits
<
InputIterator
>::
iterator_category
iterator_category
;
_advance
(
it
,
n
,
iterator_category
());
}
//********** [sort] ******************************
//********* [Algorithm Complexity: O(NlogN)] ****************
namespace
{
template
<
class
RandomIterator
,
class
BinaryPredicate
>
typename
iterator_traits
<
RandomIterator
>::
value_type
mid3
(
RandomIterator
first
,
RandomIterator
last
,
BinaryPredicate
pred
){
//[first, last]
auto
mid
=
first
+
(
last
+
1
-
first
)
/
2
;
if
(
pred
(
*
mid
,
*
first
)){
swap
(
*
mid
,
*
first
);
}
if
(
pred
(
*
last
,
*
mid
)){
swap
(
*
last
,
*
mid
);
}
if
(
pred
(
*
last
,
*
first
)){
swap
(
*
last
,
*
first
);
}
auto
ret
=
*
mid
;
swap
(
*
mid
,
*
(
last
-
1
));
//将mid item换位作为哨兵
return
ret
;
}
template
<
class
RandomIterator
,
class
BinaryPredicate
>
void
bubble_sort
(
RandomIterator
first
,
RandomIterator
last
,
BinaryPredicate
pred
){
auto
len
=
last
-
first
;
for
(
auto
i
=
len
;
i
!=
0
;
--
i
){
for
(
auto
p
=
first
;
p
!=
(
first
+
i
-
1
);
++
p
){
if
(
pred
(
*
(
p
+
1
),
*
p
))
swap
(
*
(
p
+
1
),
*
p
);
}
}
}
}
template
<
class
RandomIterator
>
void
sort
(
RandomIterator
first
,
RandomIterator
last
){
return
sort
(
first
,
last
,
less
<
typename
iterator_traits
<
RandomIterator
>::
value_type
>
());
}
template
<
class
RandomIterator
,
class
BinaryPredicate
>
void
sort
(
RandomIterator
first
,
RandomIterator
last
,
BinaryPredicate
pred
){
static
int
n
=
1
;
if
(
first
>=
last
||
first
+
1
==
last
)
return
;
if
(
last
-
first
<=
20
)
//区间长度小于等于20的采用冒泡排序更快
return
bubble_sort
(
first
,
last
,
pred
);
auto
mid
=
mid3
(
first
,
last
-
1
,
pred
);
auto
p1
=
first
,
p2
=
last
-
2
;
while
(
p1
<
p2
){
while
(
pred
(
*
p1
,
mid
)
&&
p1
<
p2
)
++
p1
;
while
(
!
pred
(
*
p2
,
mid
)
&&
p1
<
p2
)
--
p2
;
if
(
p1
<
p2
){
swap
(
*
p1
,
*
p2
);
}
}
swap
(
*
p1
,
*
(
last
-
2
));
//将作为哨兵的mid item换回原来的位置
sort
(
first
,
p1
);
sort
(
p1
+
1
,
last
);
}
}
...
...
TinySTL/Test/AlgorithmTest.cpp
浏览文件 @
4b4931a0
...
...
@@ -189,6 +189,29 @@ namespace TinySTL{
TinySTL
::
advance
(
lit
,
-
5
);
assert
(
*
vit
==
0
&&
*
lit
==
0
);
}
void
testSort
(){
int
arr1
[
1
]
=
{
0
};
TinySTL
::
sort
(
std
::
begin
(
arr1
),
std
::
end
(
arr1
));
assert
(
std
::
is_sorted
(
std
::
begin
(
arr1
),
std
::
end
(
arr1
)));
int
arr2
[
2
]
=
{
1
,
0
};
TinySTL
::
sort
(
std
::
begin
(
arr2
),
std
::
end
(
arr2
));
assert
(
std
::
is_sorted
(
std
::
begin
(
arr2
),
std
::
end
(
arr2
)));
int
arr3
[
3
]
=
{
2
,
1
,
3
};
TinySTL
::
sort
(
std
::
begin
(
arr3
),
std
::
end
(
arr3
));
assert
(
std
::
is_sorted
(
std
::
begin
(
arr3
),
std
::
end
(
arr3
)));
int
arr4
[
10000
];
std
::
random_device
rd
;
for
(
auto
i
=
0
;
i
!=
100
;
++
i
){
for
(
auto
&
n
:
arr4
){
n
=
rd
()
%
65536
;
}
TinySTL
::
sort
(
std
::
begin
(
arr4
),
std
::
end
(
arr4
));
assert
(
std
::
is_sorted
(
std
::
begin
(
arr4
),
std
::
end
(
arr4
)));
}
}
void
testAllCases
(){
...
...
@@ -211,6 +234,7 @@ namespace TinySTL{
testIsPermutation
();
testSearch
();
testAdvance
();
testSort
();
}
}
}
\ No newline at end of file
TinySTL/Test/AlgorithmTest.h
浏览文件 @
4b4931a0
...
...
@@ -11,6 +11,7 @@
#include <cstring>
#include <cassert>
#include <list>
#include <random>
#include <vector>
#include "../BinarySearchTree.h"
...
...
@@ -39,6 +40,7 @@ namespace TinySTL{
void
testIsPermutation
();
void
testSearch
();
void
testAdvance
();
void
testSort
();
void
testAllCases
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录