![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构和算法
微躺青年
这个作者很懒,什么都没留下…
展开
-
找第k小的数
/** * 找第 k 小的的数 * */#include<bits/stdc++.h>using namespace std;// Partion函数使得a[left]左边的数都小于它,右边的数都大于它int Partion(int a[], int left, int right) { int temp = a[left]; while(left <...原创 2020-05-05 16:28:35 · 123 阅读 · 0 评论 -
堆和堆排序
数据结构——堆的c++实现(这里将使用最大堆,即任意结点的父亲结点都大于它本身,满足完全二叉树的性质)template<typename T>class MaxHeap{private: T* data; int count; //当前结点数量 int capacity; //最大容量 void shiftUp(int k){ while ( k>1 &...原创 2019-08-12 16:41:15 · 107 阅读 · 0 评论 -
排序算法(nlogn级)
归并排序//merge()函数是合并两个有序序列,使用了two pointers的思想,可以看看算法笔记上的记录//将数组a的[l1,r1]与[l2,r2]区间合并成有序区间(此处l2即为 r1+1)const int max=100;void merge(T a[],int l1,int r1,int l2,int r2){ int i=l1,j=l2; //i指向a[l1],j...原创 2019-08-09 21:51:13 · 438 阅读 · 0 评论 -
排序算法(n^2级)
选择排序//简单选择排序void selectSort(T arr[],int n){ for(int i=0;i<n;i++){//进行n趟操作 int minIndex=i; for(int j=i+1;j<n;j++){//记录arr[i,n]中最小元素的索引 if(arr[j]<a[minIndex]) minIndex=j; } ...原创 2019-08-09 20:56:35 · 355 阅读 · 0 评论 -
二分查找法
//二分查找法(必须适用于有序数组,这里按从小到大排序)//返回要寻找元素 target的索引int binarySearch(T arr[], int n, T target){ //在 arr[l,r]之间查找 target int l = 0, r = n - 1; while( l<=r ){ //int mid = (l + r)/2; int mid =...原创 2019-08-13 23:01:49 · 425 阅读 · 0 评论 -
图的广度优先遍历
邻接矩阵版 const int MAXV=1000;//最大顶点数 const int INF=1000000000;//G[u][v]=INF表示u不能到达v int n,G[MAXV][MAXV]; //n为顶点数,MAXV为最大顶点数 bool inq[MAXV]={false};//若顶点i曾入队过,则inq[i]=true,初始化全为fal...原创 2019-08-05 17:33:29 · 87 阅读 · 0 评论 -
图的深度优先遍历
邻接矩阵版 const int MAXV=1000;//最大顶点数 const int INF=1000000000;//G[u][v]=INF表示u不能到达v int n,G[MAXV][MAXV]; bool v[MAXV]={false};//如果顶点已经被访问过,设为true,初始化全为false void DFS(int u,i...原创 2019-08-05 17:04:51 · 81 阅读 · 0 评论 -
使用c++从底层编写数据结构栈
注:本文章基于慕课网bobo老师的课程以及houpengfei的github的相关代码,并加以自我总结。类Stack的代码#ifndef DATASTRUCTURE_STACK_H #define DATASTRUCTURE_STACK_H template<class T> class Stack { public: in...原创 2019-07-18 14:56:26 · 119 阅读 · 0 评论 -
使用c++从底层编写动态数组
任务:新建工程使用c++从底层编写动态数组Array类Array.h 要求:元素承载泛型T私有成员(函数):data,size,resize(扩容)公有成员:Array():已经初始化好数组大小Array(int capacity):用户自定义capacity的大小getCapacity()getSize()isEmpty()add()addFirst()addLast(...原创 2019-07-13 13:41:35 · 293 阅读 · 0 评论 -
二叉树已知后序与中序输出前序
这是已知二叉树后序遍历数组和中序遍历数组的情况下,采用递归算法实现前序遍历数组的输出 #include<bits/stdc++.h> using namespace std; int post[999],in[999]; void pre(int root,int start,int end){//root是整棵树在后...原创 2019-07-13 12:37:14 · 693 阅读 · 0 评论