-
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.
- Loading branch information
Showing
123 changed files
with
21,105 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode | ||
*.txt | ||
*.bat |
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,30 @@ | ||
/* | ||
二叉搜索树的查找算法: | ||
在二叉搜索树b中查找x的过程为: | ||
1. 若b是空树,则搜索失败,否则: | ||
2. 若x等于b的根节点的数据域之值,则查找成功;否则: | ||
3. 若x小于b的根节点的数据域之值,则搜索左子树;否则: | ||
4. 查找右子树。 | ||
*/ | ||
|
||
// 在根指针T所指二叉查找树中递归地查找其关键字等于key的数据元素,若查找成功, | ||
// 则指针p指向該数据元素节点,并返回TRUE,否则指针指向查找路径上访问的最终 | ||
// 一个节点并返回FALSE,指针f指向T的双亲,其初始调用值为NULL | ||
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p){ | ||
|
||
if(!T) { //查找不成功 | ||
p=f; | ||
return false; | ||
} | ||
else if (key == T->data.key) { //查找成功 | ||
p=T; | ||
return true; | ||
} | ||
else if (key < T->data.key) //在左子树中继续查找 | ||
return SearchBST(T->lchild, key, T, p); | ||
else //在右子树中继续查找 | ||
return SearchBST(T->rchild, key, T, p); | ||
} |
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,37 @@ | ||
// 二分查找(折半查找):对于已排序,若无序,需要先排序 | ||
|
||
// 非递归 | ||
|
||
int BinarySearch(vector<int> v, int value , int low, int high) { | ||
if (v.size() <= 0) { | ||
return -1; | ||
} | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
if (v[mid] == value) { | ||
return mid; | ||
} | ||
else if (v[mid] > value) { | ||
high = mid - 1; | ||
} | ||
else { | ||
low = mid + 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
// 递归 | ||
int BinarySearch2(vector<int> v, int value, int low, int high) | ||
{ | ||
if (low > high) | ||
return -1; | ||
int mid = low + (high - low) / 2; | ||
if (v[mid] == value) | ||
return mid; | ||
else if (v[mid] > value) | ||
return BinarySearch2(v, value, low, mid - 1); | ||
else | ||
return BinarySearch2(v, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
(无序区,有序区)。从无序区通过交换找出最大元素放到有序区前端。 | ||
选择排序思路: | ||
1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 | ||
2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。 | ||
3. 针对所有的元素重复以上的步骤,除了最后一个。 | ||
4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 | ||
*/ | ||
|
||
// 冒泡排序 | ||
void BubbleSort(vector<int>& v) { | ||
int len = v.size(); | ||
for (int i = 0; i < len - 1; ++i) | ||
for (int j = 0; j < len - 1 - i; ++j) | ||
if (v[j] > v[j + 1]) | ||
swap(v[j], v[j + 1]); | ||
} | ||
|
||
// 模板实现冒泡排序 | ||
template<typename T> //整數或浮點數皆可使用,若要使用物件(class)時必須設定大於(>)的運算子功能 | ||
void bubble_sort(T arr[], int len) { | ||
for (int i = 0; i < len - 1; i++) | ||
for (int j = 0; j < len - 1 - i; j++) | ||
if (arr[j] > arr[j + 1]) | ||
swap(arr[j], arr[j + 1]); | ||
} | ||
|
||
// 冒泡排序(改进版) | ||
void BubbleSort_orderly(vector<int>& v) { | ||
int len = v.size(); | ||
bool orderly = false; | ||
for (int i = 0; i < len - 1 && !orderly; ++i) { | ||
orderly = true; | ||
for (int j = 0; j < len - 1 - i; ++j) { | ||
if (v[j] > v[j + 1]) { // 从小到大 | ||
orderly = false; // 发生交换则仍非有序 | ||
swap(v[j], v[j + 1]); | ||
} | ||
} | ||
} | ||
} |
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,80 @@ | ||
#include<iterator> | ||
#include<iostream> | ||
#include<vector> | ||
using std::vector; | ||
|
||
/***************** | ||
桶排序:将值为i的元素放入i号桶,最后依次把桶里的元素倒出来。 | ||
桶排序序思路: | ||
1. 设置一个定量的数组当作空桶子。 | ||
2. 寻访序列,并且把项目一个一个放到对应的桶子去。 | ||
3. 对每个不是空的桶子进行排序。 | ||
4. 从不是空的桶子里把项目再放回原来的序列中。 | ||
假设数据分布在[0,100)之间,每个桶内部用链表表示,在数据入桶的同时插入排序,然后把各个桶中的数据合并。 | ||
*****************/ | ||
|
||
|
||
const int BUCKET_NUM = 10; | ||
|
||
struct ListNode{ | ||
explicit ListNode(int i=0):mData(i),mNext(NULL){} | ||
ListNode* mNext; | ||
int mData; | ||
}; | ||
|
||
ListNode* insert(ListNode* head,int val){ | ||
ListNode dummyNode; | ||
ListNode *newNode = new ListNode(val); | ||
ListNode *pre,*curr; | ||
dummyNode.mNext = head; | ||
pre = &dummyNode; | ||
curr = head; | ||
while(NULL!=curr && curr->mData<=val){ | ||
pre = curr; | ||
curr = curr->mNext; | ||
} | ||
newNode->mNext = curr; | ||
pre->mNext = newNode; | ||
return dummyNode.mNext; | ||
} | ||
|
||
|
||
ListNode* Merge(ListNode *head1,ListNode *head2){ | ||
ListNode dummyNode; | ||
ListNode *dummy = &dummyNode; | ||
while(NULL!=head1 && NULL!=head2){ | ||
if(head1->mData <= head2->mData){ | ||
dummy->mNext = head1; | ||
head1 = head1->mNext; | ||
}else{ | ||
dummy->mNext = head2; | ||
head2 = head2->mNext; | ||
} | ||
dummy = dummy->mNext; | ||
} | ||
if(NULL!=head1) dummy->mNext = head1; | ||
if(NULL!=head2) dummy->mNext = head2; | ||
|
||
return dummyNode.mNext; | ||
} | ||
|
||
void BucketSort(int n,int arr[]){ | ||
vector<ListNode*> buckets(BUCKET_NUM,(ListNode*)(0)); | ||
for(int i=0;i<n;++i){ | ||
int index = arr[i]/BUCKET_NUM; | ||
ListNode *head = buckets.at(index); | ||
buckets.at(index) = insert(head,arr[i]); | ||
} | ||
ListNode *head = buckets.at(0); | ||
for(int i=1;i<BUCKET_NUM;++i){ | ||
head = Merge(head,buckets.at(i)); | ||
} | ||
for(int i=0;i<n;++i){ | ||
arr[i] = head->mData; | ||
head = head->mNext; | ||
} | ||
} |
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,62 @@ | ||
/***************** | ||
计数排序:统计小于等于该元素值的元素的个数i,于是该元素就放在目标数组的索引i位(i≥0)。 | ||
计数排序基于一个假设,待排序数列的所有数均为整数,且出现在(0,k)的区间之内。 | ||
如果 k(待排数组的最大值) 过大则会引起较大的空间复杂度,一般是用来排序 0 到 100 之间的数字的最好的算法,但是它不适合按字母顺序排序人名。 | ||
计数排序不是比较排序,排序的速度快于任何比较排序算法。 | ||
时间复杂度为 O(n+k),空间复杂度为 O(n+k) | ||
算法的步骤如下: | ||
1. 找出待排序的数组中最大和最小的元素 | ||
2. 统计数组中每个值为 i 的元素出现的次数,存入数组 C 的第 i 项 | ||
3. 对所有的计数累加(从 C 中的第一个元素开始,每一项和前一项相加) | ||
4. 反向填充目标数组:将每个元素 i 放在新数组的第 C[i] 项,每放一个元素就将 C[i] 减去 1 | ||
*****************/ | ||
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
// 计数排序 | ||
void CountSort(vector<int>& vecRaw, vector<int>& vecObj) | ||
{ | ||
// 确保待排序容器非空 | ||
if (vecRaw.size() == 0) | ||
return; | ||
|
||
// 使用 vecRaw 的最大值 + 1 作为计数容器 countVec 的大小 | ||
int vecCountLength = (*max_element(begin(vecRaw), end(vecRaw))) + 1; | ||
vector<int> vecCount(vecCountLength, 0); | ||
|
||
// 统计每个键值出现的次数 | ||
for (int i = 0; i < vecRaw.size(); i++) | ||
vecCount[vecRaw[i]]++; | ||
|
||
// 后面的键值出现的位置为前面所有键值出现的次数之和 | ||
for (int i = 1; i < vecCountLength; i++) | ||
vecCount[i] += vecCount[i - 1]; | ||
|
||
// 将键值放到目标位置 | ||
for (int i = vecRaw.size(); i > 0; i--) // 此处逆序是为了保持相同键值的稳定性 | ||
vecObj[--vecCount[vecRaw[i - 1]]] = vecRaw[i - 1]; | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> vecRaw = { 0,5,7,9,6,3,4,5,2,8,6,9,2,1 }; | ||
vector<int> vecObj(vecRaw.size(), 0); | ||
|
||
CountSort(vecRaw, vecObj); | ||
|
||
for (int i = 0; i < vecObj.size(); ++i) | ||
cout << vecObj[i] << " "; | ||
cout << endl; | ||
|
||
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,71 @@ | ||
// 斐波那契查找 | ||
|
||
#include "stdafx.h" | ||
#include <memory> | ||
#include <iostream> | ||
using namespace std; | ||
|
||
const int max_size=20;//斐波那契数组的长度 | ||
|
||
/*构造一个斐波那契数组*/ | ||
void Fibonacci(int * F) | ||
{ | ||
F[0]=0; | ||
F[1]=1; | ||
for(int i=2;i<max_size;++i) | ||
F[i]=F[i-1]+F[i-2]; | ||
} | ||
|
||
/*定义斐波那契查找法*/ | ||
int FibonacciSearch(int *a, int n, int key) //a为要查找的数组,n为要查找的数组长度,key为要查找的关键字 | ||
{ | ||
int low=0; | ||
int high=n-1; | ||
|
||
int F[max_size]; | ||
Fibonacci(F);//构造一个斐波那契数组F | ||
|
||
int k=0; | ||
while(n>F[k]-1)//计算n位于斐波那契数列的位置 | ||
++k; | ||
|
||
int * temp;//将数组a扩展到F[k]-1的长度 | ||
temp=new int [F[k]-1]; | ||
memcpy(temp,a,n*sizeof(int)); | ||
|
||
for(int i=n;i<F[k]-1;++i) | ||
temp[i]=a[n-1]; | ||
|
||
while(low<=high) | ||
{ | ||
int mid=low+F[k-1]-1; | ||
if(key<temp[mid]) | ||
{ | ||
high=mid-1; | ||
k-=1; | ||
} | ||
else if(key>temp[mid]) | ||
{ | ||
low=mid+1; | ||
k-=2; | ||
} | ||
else | ||
{ | ||
if(mid<n) | ||
return mid; //若相等则说明mid即为查找到的位置 | ||
else | ||
return n-1; //若mid>=n则说明是扩展的数值,返回n-1 | ||
} | ||
} | ||
delete [] temp; | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int a[] = {0,16,24,35,47,59,62,73,88,99}; | ||
int key=100; | ||
int index=FibonacciSearch(a,sizeof(a)/sizeof(int),key); | ||
cout<<key<<" is located at:"<<index; | ||
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,43 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
// 堆排序:(最大堆,有序区)。从堆顶把根卸出来放在有序区之前,再恢复堆。 | ||
|
||
void max_heapify(int arr[], int start, int end) { | ||
//建立父節點指標和子節點指標 | ||
int dad = start; | ||
int son = dad * 2 + 1; | ||
while (son <= end) { //若子節點指標在範圍內才做比較 | ||
if (son + 1 <= end && arr[son] < arr[son + 1]) //先比較兩個子節點大小,選擇最大的 | ||
son++; | ||
if (arr[dad] > arr[son]) //如果父節點大於子節點代表調整完畢,直接跳出函數 | ||
return; | ||
else { //否則交換父子內容再繼續子節點和孫節點比較 | ||
swap(arr[dad], arr[son]); | ||
dad = son; | ||
son = dad * 2 + 1; | ||
} | ||
} | ||
} | ||
|
||
void heap_sort(int arr[], int len) { | ||
//初始化,i從最後一個父節點開始調整 | ||
for (int i = len / 2 - 1; i >= 0; i--) | ||
max_heapify(arr, i, len - 1); | ||
//先將第一個元素和已经排好的元素前一位做交換,再從新調整(刚调整的元素之前的元素),直到排序完畢 | ||
for (int i = len - 1; i > 0; i--) { | ||
swap(arr[0], arr[i]); | ||
max_heapify(arr, 0, i - 1); | ||
} | ||
} | ||
|
||
int main() { | ||
int arr[] = { 3, 5, 3, 0, 8, 6, 1, 5, 8, 6, 2, 4, 9, 4, 7, 0, 1, 8, 9, 7, 3, 1, 2, 5, 9, 7, 4, 0, 2, 6 }; | ||
int len = (int) sizeof(arr) / sizeof(*arr); | ||
heap_sort(arr, len); | ||
for (int i = 0; i < len; i++) | ||
cout << arr[i] << ' '; | ||
cout << endl; | ||
return 0; | ||
} |
Oops, something went wrong.