1. 快速排序算法
https://blog.csdn.net/sunlanchang/article/details/60478814
2. 堆排序算法(不稳定算法)
https://www.cnblogs.com/liuqiyun/p/9415003.html
https://blog.csdn.net/zihonggege/article/details/81909047
3.归并排序算法
算法介绍:
https://www.cnblogs.com/chengxiao/p/6194356.html
c code:
https://www.cnblogs.com/942267027wzmblog/p/6882267.html
4. 二分查找算法
算法介绍:(代码看着有问题)
https://blog.csdn.net/peng_apple/article/details/79612718
code可以参考后面我提供的整体code。
1 2 3 4代码:
#include <stdio.h>
#define TEST_QUCICK_SORT 0 //test quick sort when TEST_QUCICK_SORT is 1
#define TEST_HEAP_SORT 0 //test heap sort when TEST_HEAP_SORT is 1
#define TEST_MERGE_SORT 1 //test merge sort when TEST_merge_SORT is 1
void _print(int s[], int len){
int i=0;
for(i=0; i<len; i++)
{
printf("%d=%d \n", i, s[i]);
}
}
//quick sort alg
void quick_sort(int s[], int l, int r){
int i = l, j = r, x = s[l];
if ( l < r){
while ( i < j ){
while( i<j && s[j] >= x)
j--;
if( i< j ){
s[i]=s[j];
i++;
}
while( i<j && s[i] < x)
i++;
if( i < j ){
s[j]=s[i];
j--;
}
s[i]=x;
quick_sort(s, l, i-1);
quick_sort(s, i+1, r);
}
}
}
//heap sort
void heap_adjust(int a[], int s, int m){
int rc, j;
rc = a[s];
for( j=2*s; j<=m; j=j*2){
if(j<m && a[j]<a[j+1])
j++;
if( rc>a[j])
break;
a[s]=a[j];
s=j;
}
a[s]=rc;
}
void heap_sort(int a[], int n){
int temp,i,j;
for(i=n/2;i>0;i--)//通过循环初始化顶堆
{
heap_adjust(a,i,n);
printf("Ajust start\n");
_print(a, n+1);
printf("Ajust end\n");
}
for(i=n;i>0;i--)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;//将堆顶记录与未排序的最后一个记录交换
printf("Ajust start\n");
heap_adjust(a,1,i-1);//重新调整为顶堆
_print(a, n+1);
printf("Ajust end\n");
}
}
//merge sort
void merge(int arr[], int low, int mid, int high){
int i, k;
int *tmp = (int *)malloc((high-low+1)*sizeof(int)); //申请空间,使其大小为两个
int left_low = low;
int left_high = mid;
int right_low = mid + 1;
int right_high = high;
for(k=0; left_low<=left_high && right_low<=right_high; k++){ // 比较两个指针所指向的元素
if(arr[left_low]<=arr[right_low]){
tmp[k] = arr[left_low++];
}else{
tmp[k] = arr[right_low++];
}
}
if(left_low <= left_high){ //若第一个序列有剩余,直接复制出来粘到合并序列尾
for(i=left_low;i<=left_high;i++)//memcpy(tmp+k, arr+left_low, (left_high-left_low+l)*sizeof(int));
tmp[k++] = arr[i];
}
if(right_low <= right_high){//若第二个序列有剩余,直接复制出来粘到合并序列尾
for(i=right_low; i<=right_high; i++)//memcpy(tmp+k, arr+right_low, (right_high-right_low+1)*sizeof(int));
tmp[k++] = arr[i];
}
for(i=0; i<high-low+1; i++)
arr[low+i] = tmp[i];
free(tmp);
return;
}
void merge_sort(int arr[], unsigned int first, unsigned int last){
int mid = 0;
if(first<last){
mid = (first+last)/2; /* 注意防止溢出 *//*mid = first/2 + last/2;*/ //mid = (first & last) + ((first ^ last) >> 1);
merge_sort(arr, first, mid);
merge_sort(arr, mid+1,last);
merge(arr,first,mid,last);
}
return;
}
//binary search
int binary_search(int src[], int len, int value){
int low=0;
int high=len-1;
int mid;
while(low < high)
{
mid=(low+high)/2;
if(value == src[mid])
return mid;
else if(value > src[mid])
low=mid+1;
else
high=mid-1;
if( (high-low) == 2 || (high-low) == 3)
break;
}
if(src[low] == value)
return low;
else if (src[high] == value)
return high;
return -1;
}
int main()
{
int num[]={24,1,4,9,3,6,7,8,9,12,23,5};
int len=0;
len = sizeof(num)/sizeof(num[0]);
printf("num's len is :%d \n",len);
printf("src num start\n");
_print(num, len);
printf("src num end\n");
#if TEST_QUCICK_SORT
printf("quick sort result:\n");
quick_sort(num, 0, len-1 );
#endif
#if TEST_HEAP_SORT
printf("heap sort result:\n");
heap_sort(num, len-1 );
#endif
#if TEST_MERGE_SORT
printf("merge sort result:\n");
merge_sort(num, 0,len-1);
#endif
printf("num:%d, in num:%d\n", 24, binary_search(num, len, 24));
printf("num:%d, in num:%d\n", 1, binary_search(num, len, 1));
_print(num, len);
return 0;
}
对应的Makefile
all : al_test al_dfs
al_test:main.o
gcc main.o -o al_test
al_dfs:dfs.o
gcc dfs.o -o al_dfs
%.o:%.c
gcc -Wall -Wunused -g -O2 -c $<
clean:
rm -f *.o al_test al_dfs
5. DFS(深度优先搜索)
算法介绍:
https://www.cnblogs.com/DWVictor/p/10048554.html
24点 DFS解法:
https://blog.csdn.net/specter11235/article/details/69183488
#define MAX_NUM 4
int num[MAX_NUM]={6,7,8,9};
int num_state[MAX_NUM]={0};
int dfs(int x){
int i,j;
double nu1,nu2;
if( x >= 3){
for(i=0; i<4; i++){
if(!num_state[i] && abs(num[i]-24.0) < 0.000001 )
return 1;
}
return 0;
}
for( i=0; i<4; i++){
if(!num_state[i]){
for(j=i+1; j<4l; j++){
if(!num_state[j]){
num_state[j]=1;
nu1 = num[i];
nu2 = num[j];
num[i] = nu1+nu2; if(dfs(x+1)) return 1;
num[i] = nu1-nu2; if(dfs(x+1)) return 1;
num[i] = nu2-nu1; if(dfs(x+1)) return 1;
num[i] = nu1*nu2; if(dfs(x+1)) return 1;
if( nu2 != 0)
num[i] = nu1/nu2; if(dfs(x+1)) return 1;
if( nu1 != 0)
num[i] = nu2/nu1; if(dfs(x+1)) return 1;
num[i] = nu1;
num_state[j]=0;
}
}
}
}
return 0;
}
int main()
{
if(dfs(0))
printf("found the result success\n");
else
printf("found the result fail\n");
return 1;
}
6. BFS(广度优先搜索)
算法介绍
https://blog.csdn.net/weixin_40953222/article/details/80544928
7. Dijkstra算法
https://blog.csdn.net/lbperfect123/article/details/84281300
附:
无穷大:
https://blog.csdn.net/qq_34886018/article/details/77159597
8. BFPRT(线性查找算法)(无序数据中查找)
https://blog.csdn.net/maijia0754/article/details/77947167
9. 动态规划算法
https://blog.csdn.net/ailaojie/article/details/83014821
10. 朴素贝叶斯分类算法(分类算法)