欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > C++ 算法教程

C++ 算法教程

2024/10/26 0:28:10 来源:https://blog.csdn.net/qq_30220519/article/details/139724486  浏览:    关键词:C++ 算法教程
 归并排序
#include<iostream>
using namespace std;
template <class T>
void Merge(T data[],int start,int mid,int end)
{int len1 = mid - start + 1, len2 = end - mid;int i, j, k;T* left = new int[len1];T* right = new int[len2];for (i = 0; i < len1; i++)left[i] = data[i + start];for (i = 0; i < len2; i++)right[i] = data[i + mid + 1];i = 0, j = 0;for (k = start; k < end; k++){if (i == len1 || j == len2)break;if (left[i] <= right[j])data[k] = left[i++];elsedata[k] = right[j++];}while (i < len1)data[k++] = left[i++];while (j < len2)data[k++] = right[j++];delete[] left;delete[] right;
}
template <class T>
void MergeSort(T data[], int start, int end)
{if (start < end){int mid = (start + end) / 2;MergeSort(data, start,mid);MergeSort(data, mid + 1, end);Merge(data, start, mid, end);}
}
void show(int*temp,int n)
{for (int i = 0; i < n; i++)cout << temp[i] << "  ";
}
void main()
{int temp[8];for (int i = 0; i < 8; i++)cin >> temp[i];MergeSort<int>(temp, 0,7);show(temp, 8);
}

冒泡排序_相邻交换

#include <stdio.h>  int main()  
{  int i,p,temp;  int array[10] = {2,6,1,9,4,7,5,8,3,0};  printf("Display this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  for(i = 1;i < 10; ++i)
{for(p = 0; p < 10; ++p){if(array[i]>array[p]){array[i] = 	array[i]^array[p];array[p] = array[i]^array[p];	array[i] = array[i]^array[p];}}
}printf("\n");  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }     printf("\n");  return 0;  
}  
判断循环链表

1.建立set集合,每次遍历存储元素,当集合大小不变,但循环仍在继续时说明存在循环,并得出该位置
2.定义双指针遍历:一个指针每次移动一个节点,一个指针每次移动2个节点,当2个节点指针在一个节点指针后面时,此链表存在循环。
3.链表反向
4.构造双向链表

swap

a:1001
b:1100
a=a^b;  a:0101
b=a^b;  b:1001
a=a^b;  a:1100

a = a + b;
b = a - b;
a = a - b;

直插排序
#include <stdio.h>  int main()  
{  int i,p,temp;  int array[10] = {2,6,1,9,4,7,5,8,3,0};  printf("Display this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  //选择第一个数做为起始排序for(i = 1; i < 10; ++i){temp = array[i];	//待插入排序数for(p = i-1; p >=0 && array[p]>temp; --p){//遍历已排序数列表{//如果当前数大小在已排序范围中,开始向右位移一个数让出空间array[p+1] = array[p];	}}//找到待排序数的位置,在让出的空间直接插入		array[p+1]=temp;			}printf("\n");  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }     printf("\n");  return 0;  
}  

#include <stdio.h>  int main()  
{  int i,j,t;  int array[10]={2,7,1,8,5,9,3,4,0,6};  printf("\nDisplay this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  printf("\n");  for(i=1;i<=9;i++)  {  //遍历int t = i-1;  //假设当前数为最小数for(j=i;j<10;j++)  {  if(array[j]<array[t])  {  //遍历找到最小的数,保存最小数索引t=j;  }  }  if(t!=(i-1))  {  //交换最小数与假设最小数int temp = 0;  temp=array[i-1];  array[i-1]=array[t];  array[t]=temp;  }  }  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  printf("\n");  return 0;  
}  

https://github.com/sashafierce/100-days-of-Algorithm-Challenge

GitHub - hackerkid/Awesome-Data-Structures: C++ implementation of basic data structures and algorithms

GitHub - hackerkid/LightOJ-Solutions: :sparkles: LightOJ Solutions with hints

GitHub - mmc-maodun/Data-Structure-And-Algorithm: Data Structure And Algorithm(常用数据结构与算法C/C++实现)

https://leetcode.com/

Codewars - Achieve mastery through coding practice and developer mentorship

Khan Academy | Free Online Courses, Lessons & Practice

https://github.com/sashafierce/Algo_Ds_Notes

位操作基础篇之位操作全面总结_c++位操作-CSDN博客

https://www.cnblogs.com/findumars/p/5180528.html


创作不易,小小的支持一下吧!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com