-
-
Save tangzhongliang/f8cdad7990ea132d295fdfef6c9a6faf to your computer and use it in GitHub Desktop.
查找 排序
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
排序算法 |
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
//顺序查找 | |
int SequenceSearch(int a[], int value, int n) | |
{ | |
int i; | |
for(i=0; i<n; i++) | |
if(a[i]==value) | |
return i; | |
return -1; | |
} | |
//二分查找(折半查找),版本1 | |
int BinarySearch1(int a[], int value, int n) | |
{ | |
int low, high, mid; | |
low = 0; | |
high = n-1; | |
while(low<=high) | |
{ | |
mid = (low+high)/2; | |
if(a[mid]==value) | |
return mid; | |
if(a[mid]>value) | |
high = mid-1; | |
if(a[mid]<value) | |
low = mid+1; | |
} | |
return -1; | |
} | |
//二分查找,递归版本 | |
int BinarySearch2(int a[], int value, int low, int high) | |
{ | |
int mid = low+(high-low)/2; | |
if(a[mid]==value) | |
return mid; | |
if(a[mid]>value) | |
return BinarySearch2(a, value, low, mid-1); | |
if(a[mid]<value) | |
return BinarySearch2(a, value, mid+1, high); | |
} | |
复制代码 | |
//插值查找 | |
int InsertionSearch(int a[], int value, int low, int high) | |
{ | |
int mid = low+(value-a[low])/(a[high]-a[low])*(high-low); | |
if(a[mid]==value) | |
return mid; | |
if(a[mid]>value) | |
return InsertionSearch(a, value, low, mid-1); | |
if(a[mid]<value) | |
return InsertionSearch(a, value, mid+1, high); | |
} |
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
//前序遍历 | |
int forward(Node *n){ | |
return n->left->value | |
return n->value | |
return n->right->right | |
return n->left->value | |
return n->value | |
return n->right->right | |
} | |
int forward_node(Node *n){ | |
return [n->left->value,n->value,n->right->value] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment