Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
独孤过
Pure C
提交
cc6059f9
P
Pure C
项目概览
独孤过
/
Pure C
9 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Pure C
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
cc6059f9
编写于
3月 16, 2020
作者:
独孤过
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善代码
上级
3adca89c
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
420 addition
and
144 deletion
+420
-144
工具库/CPU架构字节序.c
工具库/CPU架构字节序.c
+3
-3
工具库/不重复随机数.c
工具库/不重复随机数.c
+6
-6
工具库/文件/重定向.c
工具库/文件/重定向.c
+4
-4
工具库/文件/重定向2.c
工具库/文件/重定向2.c
+10
-9
工具库/日期/星期天数.c
工具库/日期/星期天数.c
+1
-1
工具库/隐藏密码/conio.c
工具库/隐藏密码/conio.c
+2
-1
工具库/隐藏密码/conio2.c
工具库/隐藏密码/conio2.c
+3
-1
工具库/隐藏密码/main.c
工具库/隐藏密码/main.c
+4
-3
算法/二分查找.c
算法/二分查找.c
+11
-5
算法/交换.c
算法/交换.c
+42
-19
算法/排序/bubble_sort.c
算法/排序/bubble_sort.c
+16
-10
算法/排序/gtsort.h
算法/排序/gtsort.h
+6
-3
算法/排序/insert_sort.c
算法/排序/insert_sort.c
+11
-6
算法/排序/quick_sort.c
算法/排序/quick_sort.c
+65
-32
算法/排序/quick_sort_parallel.c
算法/排序/quick_sort_parallel.c
+158
-0
算法/排序/select_sort.c
算法/排序/select_sort.c
+11
-6
算法/排序/swap.c
算法/排序/swap.c
+36
-20
算法/排序/test.c
算法/排序/test.c
+2
-2
算法/数学/最大公因数与最小公倍数.c
算法/数学/最大公因数与最小公倍数.c
+1
-1
算法/矩阵/转置.c
算法/矩阵/转置.c
+28
-12
未找到文件。
工具库/CPU架构字节序.c
浏览文件 @
cc6059f9
...
...
@@ -3,9 +3,9 @@
int8_t
endianness
()
{
//int8_t temp[] = { 0, 1 };
//return *(int
16
_t*)temp == 1;
int
16_t
temp
=
0x0100
;
//int8_t temp[] = { 0,
0, 0, 0, 0, 0, 0,
1 };
//return *(int
64
_t*)temp == 1;
int
64_t
temp
=
0x01
i64
<<
(
7
<<
3
)
;
return
*
(
int8_t
*
)
&
temp
;
}
...
...
工具库/不重复随机数.c
浏览文件 @
cc6059f9
#include <time.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
...
...
@@ -17,16 +17,16 @@ static inline void swap(int *restrict left, int *restrict right)
*
right
=
temp
;
}
void
random
(
int
*
restrict
ptr
,
int
num
)
void
random
(
int
*
restrict
base
,
int
num
)
{
if
(
ptr
==
NULL
||
num
<=
0
)
if
(
base
==
NULL
||
num
<=
0
)
return
;
for
(
int
i
=
0
;
i
<
num
;
++
i
)
ptr
[
i
]
=
i
+
1
;
base
[
i
]
=
i
+
1
;
for
(
int
i
=
0
;
i
<
num
;
++
i
)
swap
(
ptr
+
i
,
ptr
+
rand
()
%
num
);
swap
(
base
+
i
,
base
+
rand
()
%
num
);
}
#define N 10
...
...
工具库/文件/重定向.c
浏览文件 @
cc6059f9
...
...
@@ -74,12 +74,12 @@ int main()
//FILE *input = redirectInput(INPUT);
//if (input == NULL)
// return 1;
// return
-
1;
FILE
*
output
=
redirectOutput
(
OUTPUT
);
if
(
output
==
NULL
)
{
//fclose(input);
return
2
;
return
-
2
;
}
// 输出至文件
...
...
@@ -90,10 +90,10 @@ int main()
//if (resetInput() == NULL)
//{
// fclose(output);
// return 3;
// return
-
3;
//}
if
(
resetOutput
()
==
NULL
)
return
4
;
return
-
4
;
// 输出至窗口
puts
(
"freedom and justice"
);
...
...
工具库/文件/重定向2.c
浏览文件 @
cc6059f9
#include <stddef.h>
#include <stdio.h>
#if defined(_WIN32)
#include <io.h>
#elif defined(linux) || defined(__linux) || defined(__linux__)
...
...
@@ -8,6 +6,9 @@
#error "Currently only supports Windows and Linux!"
#endif
#include <stddef.h>
#include <stdio.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
...
...
@@ -31,7 +32,7 @@ FILE *redirectOutput(const char *restrict path)
}
const
char
*
const
INPUT
=
"test.in"
;
const
char
*
const
OUTPUT
=
"
E:/
test.out"
;
const
char
*
const
OUTPUT
=
"test.out"
;
int
main
()
{
...
...
@@ -41,25 +42,25 @@ int main()
//if (old_in < 0)
//{
// perror("输入文件描述符拷贝失败");
// return 1;
// return
-
1;
//}
int
std_out
=
fileno
(
stdout
);
int
old_out
=
dup
(
std_out
);
if
(
old_out
<
0
)
{
perror
(
"输出文件描述符拷贝失败"
);
return
2
;
return
-
2
;
}
// 输入输出重定向
//FILE *input = redirectInput(INPUT);
//if (input == NULL)
// return 3;
// return
-
3;
FILE
*
output
=
redirectOutput
(
OUTPUT
);
if
(
output
==
NULL
)
{
//fclose(input);
return
4
;
return
-
4
;
}
// 输出至文件
...
...
@@ -71,12 +72,12 @@ int main()
//{
// perror("默认输入恢复失败");
// fclose(output);
// return 5;
// return
-
5;
//}
if
(
dup2
(
old_out
,
std_out
)
<
0
)
{
perror
(
"默认输出恢复失败"
);
return
6
;
return
-
6
;
}
// 输出至窗口
...
...
工具库/日期/星期天数.c
浏览文件 @
cc6059f9
#include <time.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
char
*
weekday
(
unsigned
day
)
{
...
...
工具库/隐藏密码/conio.c
浏览文件 @
cc6059f9
#
if
!
defined
(
linux
)
&&
!
defined
(
__linux
)
&&
!
defined
(
__linux__
)
#error "Support for Linux!"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Usage: stty [-F DEVICE | --file=DEVICE] [SETTING]...
...
...
工具库/隐藏密码/conio2.c
浏览文件 @
cc6059f9
#
if
!
defined
(
linux
)
&&
!
defined
(
__linux
)
&&
!
defined
(
__linux__
)
#error "Support for Linux!"
#endif
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <unistd.h>
static
void
alterAttr
(
struct
termios
*
restrict
attribute
,
int
echo
)
{
...
...
工具库/隐藏密码/main.c
浏览文件 @
cc6059f9
#
include
<
string
.
h
>
#include <stdio.h>
#if defined(_WIN32)
#
if
defined
(
_WIN32
)
#include <conio.h>
#elif defined(linux) || defined(__linux) || defined(__linux__)
#include "conio.h"
#endif
#include <stdio.h>
#include <string.h>
const
char
*
const
PASSWORD
=
"universe"
;
int
main
()
...
...
算法/二分查找.c
浏览文件 @
cc6059f9
...
...
@@ -15,16 +15,18 @@ void *bsearch(
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
c
ontext
)
void
*
c
tx
)
{
if
(
key
==
NULL
||
base
==
NULL
||
num
*
size
==
0
||
comp
==
NULL
)
if
(
key
==
NULL
||
base
==
NULL
\
||
num
==
0
||
size
==
0
||
comp
==
NULL
)
return
NULL
;
size_t
low
=
0
,
high
=
num
-
1
,
mid
;
while
(
low
<=
high
)
{
mid
=
low
+
(
high
-
low
>>
1
);
int
ret
=
comp
(
key
,
address
(
base
,
mid
,
size
),
context
);
int
ret
=
comp
(
key
,
\
address
(
base
,
mid
,
size
),
ctx
);
if
(
ret
<
0
)
{
if
(
mid
==
0
)
...
...
@@ -39,7 +41,10 @@ void *bsearch(
return
NULL
;
}
int
compare
(
const
void
*
left
,
const
void
*
right
,
void
*
context
)
int
compare
(
const
void
*
left
,
\
const
void
*
right
,
\
void
*
context
)
{
int
key
=
*
(
const
int
*
)
left
;
int
value
=
*
(
const
int
*
)
right
;
...
...
@@ -63,7 +68,8 @@ int main()
printf
(
"输入查找数:"
);
scanf
(
"%d"
,
&
key
);
if
(
bsearch
(
&
key
,
array
,
N
,
sizeof
(
int
),
compare
,
NULL
))
if
(
bsearch
(
&
key
,
\
array
,
N
,
sizeof
(
int
),
compare
,
NULL
))
puts
(
"存在查找数"
);
else
puts
(
"不存在查找数"
);
...
...
算法/交换.c
浏览文件 @
cc6059f9
#include <
string
.h>
#include <
errno
.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void
*
swap
(
void
*
restrict
left
,
void
*
restrict
right
,
size_t
size
,
void
*
context
)
#define ERROR_MEM_ALLOC -1
errno_t
swap
(
void
*
restrict
left
,
\
void
*
restrict
right
,
\
size_t
size
,
\
void
**
context
)
{
if
(
left
==
NULL
||
right
==
NULL
||
size
==
0
)
if
(
left
==
NULL
\
||
right
==
NULL
||
size
==
0
)
{
free
(
context
);
return
NULL
;
if
(
context
)
{
free
(
*
context
);
*
context
=
NULL
;
}
return
0
;
}
if
(
left
==
right
)
return
context
;
return
0
;
if
(
context
&&
*
(
size_t
*
)
context
<
size
)
void
*
buffer
;
if
(
context
)
{
free
(
context
);
context
=
NULL
;
}
if
(
context
==
NULL
)
{
context
=
malloc
(
sizeof
(
size_t
)
+
size
);
*
(
size_t
*
)
context
=
size
;
void
*
temp
=
*
context
;
if
(
temp
&&
*
(
size_t
*
)
temp
<
size
)
{
free
(
temp
);
*
context
=
temp
=
NULL
;
}
if
(
temp
==
NULL
)
{
*
context
=
temp
=
malloc
(
sizeof
(
size_t
)
+
size
);
if
(
temp
==
NULL
)
return
ERROR_MEM_ALLOC
;
*
(
size_t
*
)
temp
=
size
;
}
buffer
=
(
void
*
)((
size_t
)
temp
+
sizeof
(
size_t
));
}
else
buffer
=
malloc
(
size
);
void
*
buffer
=
(
void
*
)((
size_t
)
context
+
sizeof
(
size_t
));
memcpy
(
buffer
,
left
,
size
);
memcpy
(
left
,
right
,
size
);
memcpy
(
right
,
buffer
,
size
);
return
context
;
if
(
context
==
NULL
)
free
(
buffer
);
return
0
;
}
int
main
()
{
int
a
=
9
,
b
=
10
;
printf
(
"֮ǰ
\n
a=%d,b=%d
\n
"
,
a
,
b
);
void
*
context
=
swap
(
&
a
,
&
b
,
sizeof
(
int
),
NULL
);
swap
(
&
a
,
&
b
,
sizeof
(
int
),
NULL
);
printf
(
"֮
\n
a=%d,b=%d
\n
"
,
a
,
b
);
swap
(
NULL
,
NULL
,
0
,
context
);
return
0
;
}
算法/排序/bubble_sort.c
浏览文件 @
cc6059f9
#include <stdbool.h>
#include "gtsort.h"
void
sort
(
#include <stdbool.h>
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
if
(
base
==
NULL
||
num
*
size
==
0
||
comp
==
NULL
)
return
;
if
(
base
==
NULL
||
num
==
0
\
||
size
==
0
||
comp
==
NULL
)
return
0
;
void
*
context
=
NULL
;
void
*
left
,
*
right
;
bool
swapped
=
true
;
// 内层循环交换元素标记
/*
* 如果内层循环未交换元素,表明元素已经有序,
* 结束外层循环,减少循环和比较次数
*/
bool
swapped
=
true
;
// 内层循环交换元素标记
void
*
left
,
*
right
;
void
*
context
=
NULL
;
errno_t
error
;
for
(
size_t
i
=
num
-
1
,
j
;
swapped
&&
i
>
0
;
--
i
)
{
swapped
=
false
;
...
...
@@ -26,9 +29,12 @@ void sort(
if
(
comp
(
left
=
address
(
base
,
j
,
size
),
\
right
=
address
(
base
,
j
+
1
,
size
),
ctx
)
>
0
)
{
context
=
swap
(
left
,
right
,
size
,
context
);
error
=
swap
(
left
,
right
,
size
,
&
context
);
if
(
error
!=
0
)
return
error
;
swapped
=
true
;
}
}
swap
(
NULL
,
NULL
,
0
,
context
);
swap
(
NULL
,
NULL
,
0
,
&
context
);
return
0
;
}
算法/排序/gtsort.h
浏览文件 @
cc6059f9
#ifndef GTSORT_H
#define GTSORT_H
#include <errno.h>
#include <stddef.h>
#define ERROR_MEM_ALLOC -1
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void
*
swap
(
errno_t
swap
(
void
*
restrict
left
,
\
void
*
restrict
right
,
\
size_t
size
,
\
void
*
context
);
void
*
*
context
);
static
inline
void
*
address
(
void
*
base
,
\
...
...
@@ -21,7 +24,7 @@ static inline void *address(
return
(
void
*
)((
size_t
)
base
+
num
*
size
);
}
void
sort
(
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
...
...
算法/排序/insert_sort.c
浏览文件 @
cc6059f9
#include <string.h>
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "gtsort.h"
void
sort
(
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
if
(
base
==
NULL
||
num
*
size
==
0
||
comp
==
NULL
)
return
;
if
(
base
==
NULL
||
num
==
0
\
||
size
==
0
||
comp
==
NULL
)
return
0
;
void
*
key
=
malloc
(
size
);
if
(
key
==
NULL
)
return
ERROR_MEM_ALLOC
;
for
(
size_t
i
,
j
=
1
;
j
<
num
;
++
j
)
{
memcpy
(
key
,
address
(
base
,
j
,
size
),
size
);
...
...
@@ -38,4 +42,5 @@ void sort(
}
}
free
(
key
);
return
0
;
}
算法/排序/quick_sort.c
浏览文件 @
cc6059f9
#include <string.h>
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include "gtsort.h"
#include <string.h>
static
size_t
middle
(
void
*
base
,
\
...
...
@@ -12,7 +12,7 @@ static size_t middle(
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
size_t
mid
=
(
low
+
high
)
/
2
,
min
,
max
[
2
];
size_t
mid
=
low
+
(
high
-
low
>>
1
)
,
min
,
max
[
2
];
if
(
comp
(
address
(
base
,
low
,
size
),
\
address
(
base
,
high
,
size
),
ctx
)
<
0
)
{
...
...
@@ -35,7 +35,7 @@ static size_t middle(
address
(
base
,
max
[
1
],
size
),
ctx
)
<
0
?
max
[
0
]
:
max
[
1
];
}
static
size
_t
partition
(
static
errno
_t
partition
(
void
*
base
,
\
size_t
low
,
\
size_t
high
,
\
...
...
@@ -43,7 +43,8 @@ static size_t partition(
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
,
\
void
*
key
,
\
void
**
context
)
void
**
sctx
,
\
size_t
*
axis
)
{
/*
* 于左中右三处选择次大的元素作为中轴,
...
...
@@ -51,12 +52,15 @@ static size_t partition(
*/
size_t
mid
=
middle
(
base
,
low
,
high
,
size
,
comp
,
ctx
);
memcpy
(
key
,
address
(
base
,
mid
,
size
),
size
);
memcpy
(
address
(
base
,
mid
,
size
),
\
address
(
base
,
low
,
size
),
size
);
memcpy
(
address
(
base
,
low
,
size
),
key
,
size
);
if
(
mid
!=
low
)
{
memcpy
(
address
(
base
,
mid
,
size
),
\
address
(
base
,
low
,
size
),
size
);
memcpy
(
address
(
base
,
low
,
size
),
key
,
size
);
}
size_t
left
=
low
;
size_t
right
=
high
;
size_t
left
=
low
,
right
=
high
;
errno_t
error
;
while
(
true
)
{
/*
...
...
@@ -78,16 +82,22 @@ static size_t partition(
if
(
left
>=
right
)
break
;
// 交换左右无序元素,使之成为有序元素
*
context
=
swap
(
address
(
base
,
left
,
size
),
\
address
(
base
,
right
--
,
size
),
size
,
*
context
);
error
=
swap
(
address
(
base
,
left
,
size
),
\
address
(
base
,
right
--
,
size
),
size
,
sctx
);
if
(
error
!=
0
)
return
error
;
}
// 一次划分之后,将中轴归位
*
context
=
swap
(
address
(
base
,
low
,
size
),
\
address
(
base
,
right
,
size
),
size
,
*
context
);
return
right
;
error
=
swap
(
address
(
base
,
low
,
size
),
\
address
(
base
,
right
,
size
),
size
,
sctx
);
if
(
error
!=
0
)
return
error
;
*
axis
=
right
;
return
0
;
}
static
void
recursion
(
static
errno_t
recursion
(
void
*
base
,
\
size_t
low
,
\
size_t
high
,
\
...
...
@@ -95,41 +105,64 @@ static void recursion(
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
,
\
void
*
key
,
\
void
**
context
)
void
**
sctx
)
{
/*
* 如果左边索引大于或者等于右边索引,
* 意味着已经完成一组划分排序,结束本次递归调用
*/
if
(
low
>=
high
)
return
;
return
0
;
// 进行一次划分,寻找中轴位置
size_t
axis
=
partition
(
base
,
low
,
high
,
\
size
,
comp
,
ctx
,
key
,
context
);
size_t
axis
;
errno_t
error
=
partition
(
base
,
low
,
high
,
\
size
,
comp
,
ctx
,
key
,
sctx
,
&
axis
);
if
(
error
!=
0
)
return
error
;
// 中轴左边仍有元素之时,进行递归调用,划分左边元素
if
(
axis
>
0
)
recursion
(
base
,
low
,
axis
-
1
,
\
size
,
comp
,
ctx
,
key
,
context
);
{
error
=
recursion
(
base
,
low
,
axis
-
1
,
\
size
,
comp
,
ctx
,
key
,
sctx
);
if
(
error
!=
0
)
return
error
;
}
// 进行递归调用,划分右边元素
recursion
(
base
,
axis
+
1
,
high
,
\
size
,
comp
,
ctx
,
key
,
context
);
error
=
recursion
(
base
,
axis
+
1
,
high
,
\
size
,
comp
,
ctx
,
key
,
sctx
);
if
(
error
!=
0
)
return
error
;
return
0
;
}
void
sort
(
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
if
(
base
==
NULL
||
num
*
size
==
0
||
comp
==
NULL
)
return
;
if
(
base
==
NULL
||
num
==
0
\
||
size
==
0
||
comp
==
NULL
)
return
0
;
void
*
key
=
malloc
(
size
);
void
*
context
=
NULL
;
recursion
(
base
,
0
,
num
-
1
,
\
size
,
comp
,
ctx
,
key
,
&
context
);
swap
(
NULL
,
NULL
,
0
,
context
);
if
(
key
==
NULL
)
return
ERROR_MEM_ALLOC
;
void
*
sctx
=
NULL
;
errno_t
error
=
recursion
(
base
,
0
,
num
-
1
,
\
size
,
comp
,
ctx
,
key
,
&
sctx
);
if
(
error
!=
0
)
{
free
(
key
);
return
error
;
}
swap
(
NULL
,
NULL
,
0
,
&
sctx
);
free
(
key
);
return
0
;
}
算法/排序/quick_sort_parallel.c
0 → 100644
浏览文件 @
cc6059f9
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static
size_t
middle
(
void
*
base
,
\
size_t
low
,
\
size_t
high
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
size_t
mid
=
low
+
(
high
-
low
>>
1
),
min
,
max
[
2
];
if
(
comp
(
address
(
base
,
low
,
size
),
\
address
(
base
,
high
,
size
),
ctx
)
<
0
)
{
min
=
low
;
max
[
0
]
=
high
;
}
else
{
min
=
high
;
max
[
0
]
=
low
;
}
if
(
mid
==
low
)
return
min
;
max
[
1
]
=
comp
(
address
(
base
,
min
,
size
),
\
address
(
base
,
mid
,
size
),
ctx
)
<
0
?
mid
:
min
;
return
comp
(
address
(
base
,
max
[
0
],
size
),
\
address
(
base
,
max
[
1
],
size
),
ctx
)
<
0
?
max
[
0
]
:
max
[
1
];
}
static
errno_t
partition
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
,
\
size_t
*
axis
)
{
size_t
low
=
0
,
high
=
num
-
1
;
void
*
key
=
malloc
(
size
);
if
(
key
==
NULL
)
return
ERROR_MEM_ALLOC
;
/*
* 于左中右三处选择次大的元素作为中轴,
* 并且与最左边元素交换位置
*/
size_t
mid
=
middle
(
base
,
low
,
high
,
size
,
comp
,
ctx
);
memcpy
(
key
,
address
(
base
,
mid
,
size
),
size
);
if
(
mid
!=
low
)
{
memcpy
(
address
(
base
,
mid
,
size
),
\
address
(
base
,
low
,
size
),
size
);
memcpy
(
address
(
base
,
low
,
size
),
key
,
size
);
}
size_t
left
=
low
,
right
=
high
;
void
*
sctx
=
NULL
;
errno_t
error
;
while
(
true
)
{
/*
* 从左往右遍历,在遍历未完的前提之下,
* 寻找一个不小于中轴的元素,即左无序元素
*/
while
(
left
<
high
\
&&
comp
(
address
(
base
,
++
left
,
size
),
key
,
ctx
)
<
0
);
/*
* 从右往左遍历,在遍历未完的前提之下,
* 寻找一个不大于中轴的元素,即右无序元素
*/
while
(
right
>
low
\
&&
comp
(
address
(
base
,
right
,
size
),
key
,
ctx
)
>
0
)
--
right
;
// 两边遍历重叠之时退出循环
if
(
left
>=
right
)
break
;
// 交换左右无序元素,使之成为有序元素
error
=
swap
(
address
(
base
,
left
,
size
),
\
address
(
base
,
right
--
,
size
),
size
,
&
sctx
);
if
(
error
!=
0
)
{
free
(
key
);
return
error
;
}
}
// 一次划分之后,将中轴归位
error
=
swap
(
address
(
base
,
low
,
size
),
\
address
(
base
,
right
,
size
),
size
,
&
sctx
);
if
(
error
!=
0
)
{
free
(
key
);
return
error
;
}
swap
(
NULL
,
NULL
,
0
,
&
sctx
);
free
(
key
);
*
axis
=
right
;
return
0
;
}
static
errno_t
recursion
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
/*
* 若元素数量不足两个,无需再次划分,结束本次递归调用
*/
if
(
num
<
2
)
return
0
;
// 进行一次划分,寻找中轴位置
size_t
axis
;
errno_t
error
=
partition
(
base
,
num
,
size
,
comp
,
ctx
,
&
axis
);
if
(
error
!=
0
)
return
error
;
// 中轴左边仍有元素之时,进行递归调用,划分左边元素
if
(
axis
>
0
)
{
error
=
recursion
(
base
,
axis
,
size
,
comp
,
ctx
);
if
(
error
!=
0
)
return
error
;
}
// 进行递归调用,划分右边元素
error
=
recursion
(
address
(
base
,
axis
+
1
,
size
),
\
num
-
axis
-
1
,
size
,
comp
,
ctx
);
if
(
error
!=
0
)
return
error
;
return
0
;
}
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
if
(
base
==
NULL
||
num
==
0
\
||
size
==
0
||
comp
==
NULL
)
return
0
;
return
recursion
(
base
,
num
,
size
,
comp
,
ctx
);
}
算法/排序/select_sort.c
浏览文件 @
cc6059f9
#include "gtsort.h"
void
sort
(
errno_t
sort
(
void
*
base
,
\
size_t
num
,
\
size_t
size
,
\
int
(
*
comp
)(
const
void
*
,
const
void
*
,
void
*
),
\
void
*
ctx
)
{
if
(
base
==
NULL
||
num
*
size
==
0
||
comp
==
NULL
)
return
;
if
(
base
==
NULL
||
num
==
0
\
||
size
==
0
||
comp
==
NULL
)
return
0
;
void
*
context
=
NULL
;
errno_t
error
;
for
(
size_t
i
=
0
,
j
,
k
,
counter
=
num
-
1
;
i
<
counter
;
++
i
)
{
for
(
j
=
i
+
1
,
k
=
i
;
j
<
num
;
++
j
)
...
...
@@ -18,8 +20,11 @@ void sort(
address
(
base
,
j
,
size
),
ctx
)
>
0
)
k
=
j
;
context
=
swap
(
address
(
base
,
i
,
size
),
\
address
(
base
,
k
,
size
),
size
,
context
);
error
=
swap
(
address
(
base
,
i
,
size
),
\
address
(
base
,
k
,
size
),
size
,
&
context
);
if
(
error
!=
0
)
return
error
;
}
swap
(
NULL
,
NULL
,
0
,
context
);
swap
(
NULL
,
NULL
,
0
,
&
context
);
return
0
;
}
算法/排序/swap.c
浏览文件 @
cc6059f9
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "gtsort.h"
void
*
swap
(
#include <stdlib.h>
#include <string.h>
errno_t
swap
(
void
*
restrict
left
,
\
void
*
restrict
right
,
\
size_t
size
,
\
void
*
context
)
void
*
*
context
)
{
if
(
left
==
NULL
||
right
==
NULL
||
size
==
0
)
if
(
left
==
NULL
\
||
right
==
NULL
||
size
==
0
)
{
free
(
context
);
return
NULL
;
if
(
context
)
{
free
(
*
context
);
*
context
=
NULL
;
}
return
0
;
}
if
(
left
==
right
)
return
context
;
return
0
;
if
(
context
&&
*
(
size_t
*
)
context
<
size
)
void
*
buffer
;
if
(
context
)
{
free
(
context
);
context
=
NULL
;
}
if
(
context
==
NULL
)
{
context
=
malloc
(
sizeof
(
size_t
)
+
size
);
*
(
size_t
*
)
context
=
size
;
void
*
temp
=
*
context
;
if
(
temp
&&
*
(
size_t
*
)
temp
<
size
)
{
free
(
temp
);
*
context
=
temp
=
NULL
;
}
if
(
temp
==
NULL
)
{
*
context
=
temp
=
malloc
(
sizeof
(
size_t
)
+
size
);
if
(
temp
==
NULL
)
return
ERROR_MEM_ALLOC
;
*
(
size_t
*
)
temp
=
size
;
}
buffer
=
(
void
*
)((
size_t
)
temp
+
sizeof
(
size_t
));
}
else
buffer
=
malloc
(
size
);
void
*
buffer
=
(
void
*
)((
size_t
)
context
+
sizeof
(
size_t
));
memcpy
(
buffer
,
left
,
size
);
memcpy
(
left
,
right
,
size
);
memcpy
(
right
,
buffer
,
size
);
return
context
;
if
(
context
==
NULL
)
free
(
buffer
);
return
0
;
}
算法/排序/test.c
浏览文件 @
cc6059f9
#
include
<
stdio
.
h
>
#
include
"gtsort.h"
#include
"gtsort.h"
#include
<stdio.h>
int
compare
(
const
void
*
left
,
const
void
*
right
,
void
*
context
)
{
...
...
算法/数学/最大公因数与最小公倍数.c
浏览文件 @
cc6059f9
...
...
@@ -16,7 +16,7 @@ unsigned greatestCommonDivisor(unsigned a, unsigned b)
// 最小公倍数
unsigned
leastCommonMultiple
(
unsigned
a
,
unsigned
b
)
{
return
a
*
b
/
greatestCommonDivisor
(
a
,
b
)
;
return
a
/
greatestCommonDivisor
(
a
,
b
)
*
b
;
}
int
main
()
...
...
算法/矩阵/转置.c
浏览文件 @
cc6059f9
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void
*
createMatrix
(
size_t
row
,
size_t
column
,
size_t
size
)
// 范围有限,溢出隐患
void
*
createMatrix
(
size_t
row
,
\
size_t
column
,
\
size_t
size
)
{
size_t
total
=
row
*
column
*
size
;
if
(
total
==
0
)
return
NULL
;
return
malloc
(
total
);
return
malloc
(
row
*
column
*
size
);
}
void
destroyMatrix
(
void
**
restrict
matrix
)
...
...
@@ -31,15 +32,24 @@ static inline void *address(
size_t
columns
,
\
size_t
size
)
{
return
(
void
*
)((
size_t
)
pointer
+
(
row
*
columns
+
column
)
*
size
);
return
(
void
*
)((
size_t
)
pointer
\
+
(
row
*
columns
+
column
)
*
size
);
}
void
*
transpose
(
const
void
*
restrict
matrix
,
size_t
row
,
size_t
column
,
size_t
size
)
void
*
transpose
(
const
void
*
restrict
matrix
,
\
size_t
row
,
\
size_t
column
,
\
size_t
size
)
{
if
(
matrix
==
NULL
||
row
*
column
*
size
==
0
)
if
(
matrix
==
NULL
||
row
==
0
\
||
column
==
0
||
size
==
0
)
return
NULL
;
void
*
pointer
=
createMatrix
(
column
,
row
,
size
);
if
(
pointer
==
NULL
)
return
pointer
;
for
(
size_t
i
=
0
,
j
;
i
<
column
;
++
i
)
for
(
j
=
0
;
j
<
row
;
++
j
)
memcpy
(
address
(
pointer
,
i
,
j
,
row
,
size
),
\
...
...
@@ -47,9 +57,15 @@ void* transpose(const void *restrict matrix, size_t row, size_t column, size_t s
return
pointer
;
}
void
foreach
(
void
*
matrix
,
size_t
row
,
size_t
column
,
size_t
size
,
void
(
*
functor
)(
void
*
))
void
foreach
(
void
*
matrix
,
\
size_t
row
,
\
size_t
column
,
\
size_t
size
,
\
void
(
*
functor
)(
void
*
))
{
if
(
matrix
==
NULL
||
size
==
0
||
functor
==
NULL
)
if
(
matrix
==
NULL
\
||
size
==
0
||
functor
==
NULL
)
return
;
for
(
size_t
i
=
0
,
j
;
i
<
row
;
++
i
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录