forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 18_hashtable
- Loading branch information
Showing
84 changed files
with
9,252 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,6 @@ hs_err_pid* | |
# editor files | ||
.vscode | ||
.*.swp | ||
|
||
# WebStorm | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/************************************************************************* | ||
> File Name: sqrt.c | ||
> Author: jinshaohui | ||
> Mail: [email protected] | ||
> Time: 18-10-31 | ||
> Desc: | ||
************************************************************************/ | ||
#include<stdio.h> | ||
#include<string.h> | ||
#include<stdlib.h> | ||
#include<assert.h> | ||
|
||
|
||
/*求解精度设置*/ | ||
#define E 0.000001 | ||
double mybsearch(double num) | ||
{ | ||
double start = 1.0; | ||
double end = num; | ||
double mid = 0.0; | ||
while(1) | ||
{ | ||
mid = (start + end)/2; | ||
if(((mid*mid - num) <= E) && ((mid*mid - num) >= -E)) | ||
{ | ||
return mid; | ||
} | ||
|
||
if ((mid*mid - num) > E) | ||
{ | ||
end = mid; | ||
} | ||
else | ||
{ | ||
start = mid; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
double num = 0.0; | ||
|
||
/*这里需要注意:double的输入方式*/ | ||
scanf("%lf",&num); | ||
printf("\r\n num %lf的平方根是%lf",num,mybsearch(num)); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/************************************************************************* | ||
> File Name: bsearch.c | ||
> Author: jinshaohui | ||
> Mail: [email protected] | ||
> Time: 18-10-21 | ||
> Desc: | ||
************************************************************************/ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<assert.h> | ||
|
||
/*二分查找算法的变形问题 | ||
*1、查找第一个等于给定数值的元素 | ||
*2、查找最后一个等于给定数值的元素 | ||
*3、查找第一个大于等于给定数值的元素 | ||
*4、查找第一个小于等于给定数值的元素 | ||
* */ | ||
|
||
|
||
/*1、查找第一个等于给定数值的元素*/ | ||
int mybsearch_1(int a[],int size,int value) | ||
{ | ||
int mid = 0; | ||
int left = 0; | ||
int right = size - 1; | ||
|
||
while(left <= right) | ||
{ | ||
/*防止size数量太大是,(left + right)数据翻转,导致问题*/ | ||
mid = left + ((right - left)>>1); | ||
|
||
if (a[mid] < value) | ||
{ | ||
left = mid + 1; | ||
} | ||
else if (a[mid] > value) | ||
{ | ||
right = mid - 1; | ||
} | ||
else | ||
{ | ||
if ((mid == 0) || (a[mid - 1] != value)) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
right = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/*2、查找最后一个等于给定数值的元素*/ | ||
int mybsearch_2(int a[],int size,int value) | ||
{ | ||
int mid = 0; | ||
int left = 0; | ||
int right = size - 1; | ||
|
||
while(left <= right) | ||
{ | ||
/*防止size数量太大是,(left + right)数据翻转,导致问题*/ | ||
mid = left + ((right - left)>>1); | ||
|
||
if (a[mid] < value) | ||
{ | ||
left = mid + 1; | ||
} | ||
else if (a[mid] > value) | ||
{ | ||
right = mid - 1; | ||
} | ||
else | ||
{ | ||
if ((mid == (size - 1)) || (a[mid + 1] != value)) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
left = mid + 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
/*3、查找第一个大于等于给定数值的元素*/ | ||
int mybsearch_3(int a[],int size,int value) | ||
{ | ||
int mid = 0; | ||
int left = 0; | ||
int right = size - 1; | ||
|
||
while(left <= right) | ||
{ | ||
/*防止size数量太大是,(left + right)数据翻转,导致问题*/ | ||
mid = left + ((right - left)>>1); | ||
|
||
if (a[mid] < value) | ||
{ | ||
left = mid + 1; | ||
} | ||
else | ||
{ | ||
/*a[mid] >= value 当mid==0 或者a[mid-1] > value 说明是第一个大于等于value*/ | ||
if ((mid == 0) || (a[mid - 1] < value)) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
right = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/*4、查找第一个小于等于给定数值的元素*/ | ||
int mybsearch_4(int a[],int size,int value) | ||
{ | ||
int mid = 0; | ||
int left = 0; | ||
int right = size - 1; | ||
|
||
while(left <= right) | ||
{ | ||
/*防止size数量太大是,(left + right)数据翻转,导致问题*/ | ||
mid = left + ((right - left)>>1); | ||
|
||
if (a[mid] > value) | ||
{ | ||
right = mid - 1; | ||
} | ||
else | ||
{ | ||
/*a[mid] <= value 时,当前mid == size -1 数组中最大的数值; | ||
* 或者a[mid + 1] 大于vlaue,就是mid就第一个小于等于value*/ | ||
if ((mid == (size - 1)) || (a[mid + 1] > value)) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
left = mid + 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
int main() | ||
{ | ||
int a[10] = {5,6,6,9,10,11,11,22,33,33}; | ||
int data = 0; | ||
int i = 0; | ||
int res =0; | ||
|
||
printf("\r\n"); | ||
for(i = 0; i < 10 ; i++) | ||
{ | ||
printf("%d ",a[i]); | ||
} | ||
printf("\r\n"); | ||
printf("\r\n输入一个整数"); | ||
scanf("%d",&data); | ||
res = mybsearch_1(a,10,data); | ||
printf("第一个等于data[%d],下标是%d",data,res); | ||
|
||
printf("\r\n输入一个整数"); | ||
scanf("%d",&data); | ||
res = mybsearch_2(a,10,data); | ||
printf("最后一个等于data[%d],下标是%d",data,res); | ||
|
||
printf("\r\n输入一个整数"); | ||
scanf("%d",&data); | ||
res = mybsearch_2(a,10,data); | ||
printf("第一个大于等于data[%d],下标是%d",data,res); | ||
|
||
printf("\r\n输入一个整数"); | ||
scanf("%d",&data); | ||
res = mybsearch_2(a,10,data); | ||
printf("第一个小等于data[%d],下标是%d",data,res); | ||
return; | ||
} |
Oops, something went wrong.