mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
Format the C code in Clang-Format Style: Microsoft
This commit is contained in:
@@ -1 +1 @@
|
||||
add_executable(linear_search linear_search.c)
|
||||
add_executable(binary_search binary_search.c)
|
||||
@@ -15,9 +15,9 @@ int binarySearch(int *nums, int len, int target) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right] 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中
|
||||
right = mid - 1;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
@@ -33,9 +33,9 @@ int binarySearch1(int *nums, int len, int target) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right) 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中
|
||||
right = mid;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
@@ -45,8 +45,8 @@ int binarySearch1(int *nums, int len, int target) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 6;
|
||||
int nums[10] = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
|
||||
|
||||
int nums[10] = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92};
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int index = binarySearch(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
@@ -54,6 +54,6 @@ int main() {
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user