posts - 189,comments - 1,views - 10万

随笔分类 -  算法基础

同时找出最大数和最小数
摘要:这个算法的计算次数: 1.当n是偶数,一共是n/2组,每组进行3次比较,但第一组只要一次比较,所以一共需要(3n-4)/2次比较; 2.当n是奇数,一共是(n+1)/2组,每组进行3次比较,但第一组是1次而最好一组是2次,所以一共需要(3n-3)/2次比较。 可以证明这个算法是最优的,证明可在其他书 阅读全文
posted @ 2017-03-02 10:02 郑哲 阅读(247) 评论(0) 推荐(0) 编辑
桶排序
摘要:桶排序先把要排的小数乘一个整数,按得到整数的整数部分,放入list[整数部分]。 在用成熟的排序算法把list中的小数进行排序,因为数列较少,降低了比较的次数,从而提高效率。 缺点减少了时间,但却消耗了较多的内存。 阅读全文
posted @ 2017-03-02 09:40 郑哲 阅读(159) 评论(0) 推荐(0) 编辑
计数排序(不基于比较的排序算法)
摘要:1 #include 2 using namespace std; 3 4 5 void Counting_Sort(int a[], int b[], int n, int k) 6 { 7 int *c = new int[k]; 8 for (int i = 0; i = 1; j--) 15 { 16 int u = a[j];... 阅读全文
posted @ 2017-02-28 18:19 郑哲 阅读(246) 评论(0) 推荐(0) 编辑
快排序
摘要:1 #include 2 using namespace std; 3 4 5 6 int Partition(int a[],int p,int r,int q)//实现划分 7 { 8 int x = a[r]; 9 int i = p - 1; 10 for (int j = p; j <= r - 1; j++) 11 if... 阅读全文
posted @ 2017-02-28 16:10 郑哲 阅读(121) 评论(0) 推荐(0) 编辑
堆用作优先队列
摘要:1 #include 2 using namespace std; 3 4 //把a[i]中的数值增加到新的值key 5 void Heap_Increase_Key(int a[], int i, int key) 6 { 7 if (key 1&&a[i/2]<a[i]) 11 { 12 int t = a[i]; 13 a[i... 阅读全文
posted @ 2017-02-28 15:58 郑哲 阅读(118) 评论(0) 推荐(0) 编辑
堆排序
摘要:1 #include 2 using namespace std; 3 4 5 void Max_Heapify(int a[],int heap_size, int i)//修复堆 6 { 7 int largest; 8 int l = 2 * i; 9 int r = 2 * i + 1; 10 if (l a[i]) 11 ... 阅读全文
posted @ 2017-02-28 15:32 郑哲 阅读(102) 评论(0) 推荐(0) 编辑
合并算法
摘要:1 #include 2 using namespace std; 3 4 5 void Merge(int a[],int a1,int n1, int b[],int b1,int n2, int c[]) 6 { 7 int i = a1; 8 int j = b1; 9 int k = a1; 10 while (i n1) 25 ... 阅读全文
posted @ 2017-02-28 12:39 郑哲 阅读(167) 评论(0) 推荐(0) 编辑
插入排序
摘要:1 #include 2 using namespace std; 3 4 5 void Insertion_Sort(int a[],int n) 6 { 7 if (n == 1) 8 exit(1); 9 for (int k = 1; k = 0 && a[j] > x) 14 { 15 a[j... 阅读全文
posted @ 2017-02-27 22:38 郑哲 阅读(86) 评论(0) 推荐(0) 编辑
二元搜索算法(分治法)
摘要:1 #include 2 using namespace std; 3 4 5 int Binary_search(int *a, int p, int r, int x) 6 { 7 if (p > r) 8 return false; 9 int midpoint = (p + r) / 2; 10 if (a[midpoint]... 阅读全文
posted @ 2017-02-27 22:16 郑哲 阅读(355) 评论(0) 推荐(0) 编辑

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示