c++数据结构与算法
bladeandmaster88
好好奋斗
展开
-
海量数据求中位数
题目如下:只有2G内存的pc机,在一个存有10G个整数的文件,从中找到中位数,写一个算法。给出了四种方法来解决算法:1.利用外排序的方法,进行排序 ,然后再去找中位数注释:外部排序基本上由两个相对独立的阶段组成。首先,按可用内存大小,将外存上含n个记录的文件分成若干长度为h的子文件,依次读入内存并利用有效的内部排序方法对它们进行排序,并将排序后得到的有序子文件重新写入外存,通常称这些有序子文件为归并段或顺串;然后,对这些归并段进行逐趟归并,使归并段(有序的子文件)逐渐由小到大,直至得到整个有序文转载 2020-05-20 00:09:59 · 1047 阅读 · 0 评论 -
找出整型数组中重复次数最多的元素
#include #include #include using namespace std;struct number{ int value; int count;};int find(const vector v,int value){ for (int i = 0; i < v.size(); i++) { if (v[i].value == value)原创 2016-09-14 22:13:14 · 2666 阅读 · 0 评论 -
求孪生素数的对数
//数学上把相差为2的两个质数叫做“孪生质数”//在 100 以内的孪生素数有(3,5),(5, 7), (11, 13), (17, 19), //(29, 31), (41, 43), (59, 61) 和(71, 73),总计有 8 组。#include using namespace std;bool isPrime(int n){ for (int i = 2;原创 2016-09-12 09:03:27 · 3873 阅读 · 0 评论 -
求任意数的质数对数
//给定一个数,比如10= 3+7 = 5+5//如上两队质数3-7和5-5,相加为10//则10的质数对的个数为2//设计一个算法求任意数的质数对数。#include using namespace std;int main(){int num;cin >> num;int result = 0;/*请在些完成代码根原创 2016-09-11 21:38:38 · 818 阅读 · 0 评论 -
删除链表中重复元素
//删除链表中重复的元素//比如1 2 3 3 4 4//删除重复元素变为1 2#include using namespace std;struct Node{ Node(int x) { value = x; next = 0; } int value; Node *next;};void deleteDuplication(Node **head){原创 2016-09-18 09:39:11 · 1262 阅读 · 0 评论 -
合并两个已排序的链表
#include using namespace std;struct Node{ Node(int x) { value = x; next = 0; } int value; Node *next;};Node *Merge(Node *head1, Node *head2){ if (head1 == NULL) return head2; if原创 2016-09-16 11:11:10 · 405 阅读 · 0 评论 -
求单链表的长度(可能有环)
单向链表环问题的全面解析(判断环,环的长度,环的入口)参照博客https://my.oschina.net/u/2360415/blog/741253#include using namespace std;struct Node{ Node(int x) { value = x; next = 0; } int value; Node *next;};in原创 2016-09-15 21:40:20 · 1518 阅读 · 0 评论 -
链表操作面试题
一、寻找单链表的中间结点//方法1、遍历求长度length 然后再遍历输出//方法2、双路齐下: p1走两步 p2走一步; p1走到终点时, p2正好到达中点Node* findMiddle(Node *head){ Node *p1 = head; Node *p2 = head; while (p1->next->next) { p1 = p1->next->next;原创 2016-09-06 20:40:14 · 661 阅读 · 0 评论 -
从一个链表channelHead中删除和channelToDeleteHead中相同的元素
#include using namespace std;typedef struct Channel{//int channelIndex;char *channelName;struct Channel *nextChannel;}sChannel;//从一个链表channelHead中删除和channelToDeleteHead中相同的原创 2016-09-06 10:31:53 · 436 阅读 · 0 评论 -
c++反转整个链表
链表反转在面试中经常容易被问及,比如//输入1 2 3 4 5//输出5 4 3 2 1#include #include using namespace std;class Node{public:Node(int data){value = data;next = 0;}int value;Node *next;原创 2016-09-01 10:20:31 · 4087 阅读 · 0 评论 -
c++反转单链表后半部分
给出一个链表,将给定链表的后半个反转。如果链表中元素个数为奇数,则中间元素会成为反转的一部分;如果为偶数,中间元素不成为反转的一部分。中间的元素是第(N+1)/2个元素,N是总元素的个数。示例:输入:2 3 6 1 4 8 9 7输出:2 3 6 1 7 9 8 4链表结构体:struct lnode{ char value; struct ln原创 2016-08-31 20:23:51 · 2069 阅读 · 0 评论 -
顺时针打印二维数组
如果看不懂,可参照剑指offer128页#include #include using namespace std;//方法一:传vector >需要传引用//方法二:传二维指针//按圈给数组赋值void printMatrixInCircle(/*int***/ vector >&numbers, int rows, int columns, int start, i原创 2016-09-16 19:54:23 · 1380 阅读 · 0 评论 -
矩阵相乘
#include using namespace std;int **getTranspose(int **list, int rows, int columns){ //创建一个newRow-newCol的转置矩阵 int newRow = columns, newCol = rows; int **trans = new int*[newRow]; for (int i =原创 2016-09-14 10:51:28 · 644 阅读 · 0 评论 -
矩阵的转置
#include using namespace std;int **getTranspose(int **list, int rows, int columns){ //创建一个newRow-newCol的转置矩阵 int newRow = columns, newCol = rows; int **trans = new int*[newRow]; for (int i =原创 2016-09-14 10:06:45 · 519 阅读 · 0 评论 -
rectanglePatternPrint
//打印如下的矩阵//55555//55155//55255//55355//55455//55555#include using namespace std;void rectanglePatternPrint(int n){ int row = n + 1;//行数 int col = 0; if (n % 2 == 0)//n是偶数 col = n + 1原创 2016-09-13 18:39:39 · 442 阅读 · 0 评论 -
squarePatternPrint
给定一个整数n,以下列方式打印n行:如果n=4,生成的阵列为1*2*3*49*10*11*1213*14*15*165*6*7*8每行只能输出数字和*,不能有空格void squarePatternPrint(int n){ }#include #include #include using namespace std;int mai原创 2016-09-01 11:17:33 · 543 阅读 · 0 评论 -
利用快排思想找出数组中第k大的元素
#include #include using namespace std;//用快排的思想:例如找49个元素里面第24大的元素,那么按如下步骤://1.进行一次快排(将大的元素放在前半段,小的元素放在后半段), 假设得到的中轴为p//2.判断 k -1==p - low,如果成立,直接输出a[p],(因为前半段有k - 1个大于a[p]的元素,故a[p]为第K大的元素)//3.如果原创 2016-09-05 11:08:42 · 3924 阅读 · 3 评论 -
选择排序算法
#includeusing namespace std;//选择排序法的第一层循环从下标1开始遍历到下标n-1,//主要是在每次进入的第二层循环之前,将外层循环的下标赋值给临时变量,//接下来的第二层循环中,如果发现有比这个最小位置处的元素更小的元素,则将那个更小的元素的下标赋给临时变量,//最后,在二层循环退出后,如果临时变量改变,则说明有比当前外层循环位置更小的元素,需要将这两个元原创 2016-09-04 17:04:29 · 324 阅读 · 0 评论 -
二分法查找
//二分查找的基本思想是将n个元素分成大致相等的两部分,取a[n / 2]与x做比较,如果x = a[n / 2], 则找到x,//算法中止;如果xa[n / 2], 则只要在数组a的右半部搜索x//时间复杂度为O()=O(logn)//二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;//其缺点是要求待查表为有序表,且插入删除困难。#includeusing name原创 2016-09-04 22:03:40 · 536 阅读 · 0 评论 -
插入排序算法
假设我们输入的是 "5,1,4,2,3" 我们从第二个数字开始,这个数字是1,我们的任务只要看看1有没有正确的位置,我们的做法是和这个数字左边的数字来比,因此我们比较1和5,1比5小,所以我们就交换1和5,原来的排列就变成了"1,5,4,2,3 "接下来,我们看第3个数字有没有在正确的位置。这个数字是4,它的左边数字是5,4比5小,所以我们将4和5交换,排列变成了 "1,4,5,2,3 "我们必须原创 2016-09-04 15:51:16 · 548 阅读 · 0 评论 -
冒泡排序
#includeusing namespace std;// 冒泡排序算法//list为一个int[]数组,存放有n个元素,下面的算法对数组中元素按从小到大的顺序排列void bubbleSort(int list[], int n){ for (int i = 1; i <= n - 1; i++)//外层循环,第一次循环挑出最大的,第二次循环挑出次大的元素,,那么10个元素,循原创 2016-09-04 21:38:02 · 324 阅读 · 0 评论 -
csdn如何高亮显示代码和插入图片
转载链接:http://blog.csdn.net/fengkuangshixisheng/article/details/27691179转载 2016-09-04 16:10:55 · 360 阅读 · 0 评论 -
快速排序
快速排序的算法介绍和算法演示见百度百科http://baike.baidu.com/view/19016.htm#include <iostream>#include <vector>#include <iterator>using namespace std;//快排的思路:是通过不断的对数组进行二分,直到数组不能再分解为止(只有一个数据),步骤如下:...原创 2016-09-05 11:06:47 · 405 阅读 · 0 评论 -
几种排序以及其时间复杂度
1.选择排序:不稳定,时间复杂度 O(n^2)选择排序的基本思想是对待排序的记录序列进行n-1遍的处理,第i遍处理是将L[i..n]中最小者与L[i]交换位置。 这样,经过i遍处理之后,前i个记录的位置已经是正确的了。2.插入排序:稳定,时间复杂度 O(n^2)插入排序的基本思想是,经过i-1遍处理后,L[1..i-1]己排好序。第i遍处理仅将L[i]插入L[1..i-1]的适当位置,原创 2016-09-04 19:13:48 · 473 阅读 · 0 评论 -
全排列思想 实现_ _ _ + _ _ _ = _ _ _解法
#include using namespace std;int total = 0;//利用递归实现数的全排列的思想 实现_ _ _ + _ _ _ = _ _ _解法void Permutations(int *a, const int start, const int end)//start和end分别是a的起始位置和终点位置{ if (start == end) {原创 2016-09-05 16:34:28 · 478 阅读 · 0 评论 -
递归实现数的全排列
#include using namespace std;//递归实现数的全排列void Permutations(int *a,const int start,const int end )//start和end分别是a的起始位置和终点位置{ if (start == end) { for (int j = 0; j <= end; j++) cout << a[j] <原创 2016-09-05 14:20:07 · 490 阅读 · 0 评论