5.#
点击查看考点
递归函数, 插入排序
1#include <iostream>
2
3void function(char* string, int index) {
4 if (string[index] == '\0') {
5 return;
6 }
7
8 char current = string[index];
9
10 int i = 0;
11 for (i = index - 1; i >= 0 && *(string + i) > current; --i) {
12 string[i + 1] = string[i];
13 }
14 string[i + 1] = current;
15
16 function(string, index + 1);
17}
18
19auto main() -> int {
20 char string[] = "HelloWorld";
21 function(string, 0);
22 std::cout << string;
23}