Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
fed7dd1c
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
fed7dd1c
编写于
7月 01, 2020
作者:
K
Krishna Vedala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improved self-tests
上级
69b5c839
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
31 addition
and
0 deletion
+31
-0
sorting/insertion_sort.cpp
sorting/insertion_sort.cpp
+31
-0
未找到文件。
sorting/insertion_sort.cpp
浏览文件 @
fed7dd1c
...
...
@@ -51,6 +51,7 @@ namespace sorting {
/** \brief
* Insertion Sort Function
*
* @tparam T type of array
* @param [in,out] arr Array to be sorted
* @param n Size of Array
*/
...
...
@@ -69,6 +70,7 @@ void insertionSort(T *arr, int n) {
/** Insertion Sort Function
*
* @tparam T type of array
* @param [in,out] arr pointer to array to be sorted
*/
template
<
typename
T
>
...
...
@@ -88,6 +90,21 @@ void insertionSort(std::vector<T> *arr) {
}
// namespace sorting
/**
* @brief Create a random array objecthelper function to create a random array
*
* @tparam T type of array
* @param arr array to fill (must be pre-allocated)
* @param N number of array elements
*/
template
<
typename
T
>
static
void
create_random_array
(
T
*
arr
,
int
N
)
{
while
(
N
--
)
{
double
r
=
(
std
::
rand
()
%
10000
-
5000
)
/
100.
f
;
arr
[
N
]
=
static_cast
<
T
>
(
r
);
}
}
/** Test Cases to test algorithm */
void
tests
()
{
int
arr1
[
10
]
=
{
78
,
34
,
35
,
6
,
34
,
56
,
3
,
56
,
2
,
4
};
...
...
@@ -113,6 +130,20 @@ void tests() {
sorting
::
insertionSort
(
&
arr4
);
assert
(
std
::
is_sorted
(
std
::
begin
(
arr4
),
std
::
end
(
arr4
)));
std
::
cout
<<
"passed"
<<
std
::
endl
;
int
arr5
[
50
];
std
::
cout
<<
"Test 5... "
;
create_random_array
(
arr5
,
50
);
sorting
::
insertionSort
(
arr5
,
50
);
assert
(
std
::
is_sorted
(
arr5
,
arr5
+
50
));
std
::
cout
<<
"passed"
<<
std
::
endl
;
float
arr6
[
50
];
std
::
cout
<<
"Test 6... "
;
create_random_array
(
arr6
,
50
);
sorting
::
insertionSort
(
arr6
,
50
);
assert
(
std
::
is_sorted
(
arr6
,
arr6
+
50
));
std
::
cout
<<
"passed"
<<
std
::
endl
;
}
/** Main Function */
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录