mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Format the C code in Clang-Format Style: Microsoft
This commit is contained in:
@@ -17,7 +17,7 @@ void constant(int n) {
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
int nums[1000];
|
||||
ListNode* node = newListNode(0);
|
||||
ListNode *node = newListNode(0);
|
||||
free(node);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -38,11 +38,11 @@ typedef struct {
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int *nums = malloc (sizeof(int) * n);
|
||||
int *nums = malloc(sizeof(int) * n);
|
||||
free(nums);
|
||||
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
ListNode** nodes = malloc(sizeof(ListNode *) * n);
|
||||
ListNode **nodes = malloc(sizeof(ListNode *) * n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes[i] = newListNode(i);
|
||||
}
|
||||
@@ -56,7 +56,7 @@ void linear(int n) {
|
||||
hashTable *h = NULL;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
hashTable *tmp = malloc(sizeof (hashTable));
|
||||
hashTable *tmp = malloc(sizeof(hashTable));
|
||||
tmp->key = i;
|
||||
sprintf(tmp->val, "%d", i);
|
||||
HASH_ADD_INT(h, key, tmp);
|
||||
@@ -81,7 +81,7 @@ void linearRecur(int n) {
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
int** numMatrix = malloc(sizeof(int *) * n);
|
||||
int **numMatrix = malloc(sizeof(int *) * n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
int *tmp = malloc(sizeof(int) * n);
|
||||
for (int j = 0; j < n; j++) {
|
||||
@@ -100,8 +100,8 @@ void quadratic(int n) {
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
int* nums = malloc(sizeof(int) * n);
|
||||
return 0;
|
||||
int *nums = malloc(sizeof(int) * n);
|
||||
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
|
||||
free(nums);
|
||||
return quadraticRecur(n - 1);
|
||||
|
||||
@@ -12,7 +12,7 @@ int constant(int n) {
|
||||
int size = 100000;
|
||||
int i = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -21,7 +21,7 @@ int constant(int n) {
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -31,19 +31,18 @@ int arrayTraversal(int *nums, int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n)
|
||||
{
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
@@ -51,7 +50,7 @@ int quadratic(int n)
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int *nums, int n) {
|
||||
int count = 0; // 计数器
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@@ -61,7 +60,7 @@ int bubbleSort(int *nums, int n) {
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +84,8 @@ int exponential(int n) {
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
@@ -101,24 +101,26 @@ int logarithmic(float n) {
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count ++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
@@ -145,7 +147,7 @@ int main(int argc, char *argv[]) {
|
||||
count = quadratic(n);
|
||||
printf("平方阶的计算操作数量 = %d\n", count);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(nums, n);
|
||||
printf("平方阶(冒泡排序)的计算操作数量 = %d\n", count);
|
||||
@@ -172,5 +174,6 @@ int main(int argc, char *argv[]) {
|
||||
nums = NULL;
|
||||
}
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ int *randomNumbers(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
// 随机打乱数组元素
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
int j = rand() % (i + 1);
|
||||
int temp = nums[i];
|
||||
@@ -29,7 +29,8 @@ int findOne(int *nums, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1) return i;
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -51,5 +52,6 @@ int main(int argc, char *argv[]) {
|
||||
nums = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user