Skip to content

Commit 2e344a0

Browse files
committed
Snap
1 parent 762fea8 commit 2e344a0

6 files changed

Lines changed: 41 additions & 13 deletions

File tree

105 KB
Loading

c/pancakeSort/pancakeSort.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define NO_REC_IMP
77
#endif
88

9+
// #define DEBUG
910
#if defined DEBUG
1011
static int reverse_times = 0;
1112
#endif

c/pancakeSort/pancakeSort_generic.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define length_of_array(array) sizeof(array) / sizeof(array[0])
44

5+
// #define DEBUG
56
#if defined DEBUG
67
static int reverse_times = 0;
78
#endif
105 KB
Loading

c/pancakeSort/pancake_sort/slides.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
---
2-
author: 测试
3-
date: 12 01, 2023
4-
paging: Slide %d / %d
2+
title: 煎饼排序(pancake sort)
53
---
64

75
# 煎饼排序(pancake sort)
@@ -16,7 +14,7 @@ paging: Slide %d / %d
1614

1715
---
1816

19-
## 题目:
17+
## 题目要求
2018

2119
给你一个整数数组  `arr` ,请使用 _煎饼翻转_ 完成对数组的排序。
2220

@@ -39,7 +37,9 @@ paging: Slide %d / %d
3937
4038
---
4139

42-
## 尝试解决子问题:将最大元素移动到无序部分的底部
40+
## 尝试解决子问题:
41+
42+
> 将最大元素移动到无序部分的底部
4343
4444
### 考虑[3,2,1,4]的一般解法
4545

@@ -140,7 +140,7 @@ int find_max_elem(int *array, int start, int end) {
140140
141141
## 纯 C 语言实现(非泛型)(续)
142142
143-
```c
143+
```c {all|6-8|14,18-20|all}
144144
// 非递归煎饼排序
145145
int pancakeSort_no_rec(int *unsorted_array, int sort_start, int sort_end) {
146146
int max_elem;
@@ -200,7 +200,7 @@ int find_max_elem(void *array, size_t len, size_t elem_byte_size,
200200
201201
## 纯 C 语言实现(泛型)(续)
202202
203-
```c
203+
```c {all|8-11|all}
204204
// 非递归煎饼排序
205205
int pancakeSort_no_rec(void *unsorted_array, size_t len, size_t elem_byte_size,
206206
int (*compare_function)(const void *a, const void *b),
@@ -221,7 +221,7 @@ int pancakeSort_no_rec(void *unsorted_array, size_t len, size_t elem_byte_size,
221221

222222
## 纯 C 语言实现(泛型)(续)
223223

224-
```c
224+
```c {all|8-13|all}
225225
// 递归煎饼排序
226226
int pancakeSort_rec(void *unsorted_array, size_t len, size_t elem_byte_size,
227227
int (*compare_function)(const void *a, const void *b),
@@ -246,3 +246,15 @@ int pancakeSort_rec(void *unsorted_array, size_t len, size_t elem_byte_size,
246246
247247
- 当最大元素本来就在无序部分底部时无需进行煎饼翻转, 可直接进行扩展有序部分
248248
- 当最大元素本来就在无序部分顶部时只需翻转一次
249+
250+
---
251+
252+
## 代码运行截图
253+
254+
![替代文本](Screenshot_2023-12-03-18-49-20_5200.png)
255+
256+
---
257+
258+
# That's all
259+
260+
Thanks

c/pancakeSort/煎饼排序.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
#define length_of_array(array) sizeof(array) / sizeof(array[0])
44

5-
#if defined DEBUG
65
static int reverse_times = 0;
7-
#endif
86

97
// #define REC_IMP
108
#ifndef REC_IMP
119
#define NO_REC_IMP
1210
#endif
1311

12+
static size_t *result_p;
13+
1414
// generic
1515
int reverse_array(void *array, size_t len, size_t elem_byte_size,
1616
int (*swap_function)(const void *a, const void *b)) {
@@ -56,7 +56,11 @@ int pancakeSort(void *unsorted_array, size_t len, size_t elem_byte_size,
5656
for (; unsorted_len > 0; unsorted_len--) {
5757
max_elem = find_max_elem(unsorted_array, unsorted_len, elem_byte_size,
5858
compare_int);
59+
result_p[reverse_times] = max_elem + 1;
60+
reverse_times++;
5961
reverse_array(unsorted_array, max_elem + 1, elem_byte_size, swap_int);
62+
result_p[reverse_times] = unsorted_len;
63+
reverse_times++;
6064
reverse_array(unsorted_array, unsorted_len, elem_byte_size, swap_int);
6165
}
6266
return 0;
@@ -86,8 +90,18 @@ int pancakeSort(void *unsorted_array, size_t len, size_t elem_byte_size,
8690
#endif
8791

8892
int main() {
89-
int array1[] = {5, 2, 1, 0, 3, 2, 9}; // {5,2,1,0,3,2*,9} => {0,1,2,2*,3,5,9}
93+
int array1[] = {5, 2, 1, 0,
94+
3, 2, 9}; // want: {5,2,1,0,3,2*,9} => {0,1,2,2*,3,5,9}
9095
size_t i;
91-
size_t result[length_of_array(array1)*2];
92-
96+
size_t result[length_of_array(array1) * 2];
97+
result_p = result;
98+
pancakeSort(array1, length_of_array(array1), sizeof(int) / sizeof(char),
99+
compare_int, swap_int);
100+
printf("k值序列:");
101+
for (i = 0; i < reverse_times; i++) {
102+
printf("%zu ", result[i]);
103+
}
104+
printf("\n排序结果:");
105+
for (i = 0; i < length_of_array(array1); i++)
106+
printf("%d ", array1[i]);
93107
}

0 commit comments

Comments
 (0)