桑海

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

随笔分类 -  算法导论

摘要:三类随机问题 1. 已有n条记录,从中选取m条记录,选取出来的记录前后顺序不管。 实现思路:按行遍历所有记录,约隔n/m条取一个数据即可 2. 在1类情况下,还要求选取出来的m条记录是随机排序的 实现思路: 给n条记录,分别增加一列标记,值为随机选取的1至n之间的不重复数据, 实现参考博文 将文件内 阅读全文
posted @ 2017-01-23 11:21 桑海 阅读(885) 评论(0) 推荐(0)

摘要:#include<iostream>#include<cstdlib>#include<ctime>using namespace std;const int maxn = 5000;int main(){ void Randomized_QuickSort(int A[], int p, int r); void out(int A[], int n); int n, A[ma... 阅读全文
posted @ 2012-12-11 19:56 桑海 阅读(935) 评论(0) 推荐(0)

摘要:描述: 快速排序是一种排序算法,对包含n个数的输入数组,最坏情况运行时间为Θ(你……2).虽然这个最坏情况运行时间比较差,但快速排序通常是用于排序的最佳选择,,这时因为其平均性能相当好:期望的运行时间为Θ(n^2),且Θ(nlgn)记号中的常数因子很小。另外,它还能够进行就地排序,在虚拟环境中也能很好的工作。 #include<iostream>using namespace std;const... 阅读全文
posted @ 2012-12-11 13:58 桑海 阅读(241) 评论(0) 推荐(0)

摘要:#include<iostream>using namespace std;int power(int x, int n){//分治法,时间复杂度:lgn if(x == 0) { if(n == 0) cout << "WRONG INPUT: SIGNIGICANT!!!" << endl; return 0; } if... 阅读全文
posted @ 2012-12-09 14:02 桑海 阅读(291) 评论(0) 推荐(0)

摘要:问题描述:在给定结合S中,是否存在之和与给定数x相等的两数,有则输出。#include<iostream>using namespace std;const int maxn = 5000;int main(){ void merge_sort(int a[], int beg, int end); void put(int a[], int n); int binary_sort(int A[], int y, int beg, int end); int a[maxn], x, n; cout << "Enter two numbers: "; 阅读全文
posted @ 2012-12-08 22:44 桑海 阅读(232) 评论(0) 推荐(0)

摘要:#include<iostream>using namespace std;const int maxn = 5000;int main(){ void insertion_sort(int A[], int n); void put(int A[], int n); int n, A[maxn]; cin >> n; for(int i = 0; i < n; ++i) cin >> A[i]; insertion_sort(A, n); put(A, n); return 0;}void insertion_sort(int A... 阅读全文
posted @ 2012-12-08 22:41 桑海 阅读(143) 评论(0) 推荐(0)

摘要:#include<iostream>using namespace std;const int maxn = 5000;int main(){ void selection_sort(int A[], int n); void put(int A[], int n); int n, A[maxn]; cin >> n; for(int i = 0; i < n; ++i) cin >> A[i]; selection_sort(A, n); put(A, n); return 0;}int min(int A[], int beg, int... 阅读全文
posted @ 2012-12-08 22:38 桑海 阅读(150) 评论(0) 推荐(0)

摘要:#include<iostream>using namespace std;const int maxn = 5000;int main(){ void merge_sort(int a[], int beg, int end); void put(int a[], int n); int a[maxn], n; cout << "Enter a number: "; cin >> n; cout << "Enter some numbers:" << endl; for(int i = 0; 阅读全文
posted @ 2012-12-08 22:30 桑海 阅读(201) 评论(0) 推荐(0)

摘要:插入排序:数组或容器中vec[0....n]中,包含了n个待排序的数。输入的各个数字是原地排序的,意即这些数字就是在数组或容器vec在中进行重新排序的,在任何饿时候,至多只有其中的常数个数字是存储在数组或容器之外的。 #include<iostream>#include<vector>using namespace std;int main(){ vector<int> ivec; i... 阅读全文
posted @ 2012-11-11 18:51 桑海 阅读(190) 评论(0) 推荐(0)