数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
来源:互联网 发布:array vb 编辑:程序博客网 时间:2024/06/14 10:53
查找第K个最大值的两种方法;递归的四条基本法则。
书中首先介绍了两种简单的方法:
One way to solve this problem would be to read the n numbers into an array, sort the array in decreasing order by some simple algorithm such as bubble sort, and then return the element in position k.
int find_kth_largest_num(int *a, int num, int k){int exchange = 0;for(int i = 0; i<num-1; i++){exchange = 0;for(int j = 0; j<num-1-i; j++){if(a[j]>a[j+1]){int tmp = a[j];a[j]=a[j+1];a[j+1]=tmp;exchange = 1;}}if(0 == exchange){break;}}return a[k-1];}
A somewhat better algorithm might be to read the first k elements into an array and sort them (in decreasing order).Next, each remaining element is read one by one. As a new element arrives, it is ignored if it is smaller than the kth element in the array. Otherwise, it is placed in its correct spot in the array, bumping one
int find_kth_largest_num_2(int *a, int num, int k){//sort the first k elementsint exchange = 0;for(int i = 0; i<k-1; i++){exchange = 0;for(int j = 0; j<k-1-i; j++){if(a[j]>a[j+1]){int tmp = a[j];a[j]=a[j+1];a[j+1]=tmp;exchange = 1;}}if(0 == exchange){break;}}//As a new element arrives, it is ignored if it is smaller than the kthelement in the array. //Otherwise, it is placed in its correct spot in the array, bumping one element out of the array.for(int i = 0; i<num-k; i++){//Get the next (num-k) number in order; Then put the number in the (k+1)th positiona[k] = a[k+i];for(int j = k; j>0; j--) // sort the (k+1) num{if(a[j]>a[j-1]){int tmp = a[j];a[j] = a[j-1];a[j-1] = tmp;}else{break;}}}return a[k-1];}
递归的四条基本法则:
When writing recursive routines, it is crucial to keep in mind the four basic rules of recursion:
1. Base cases.You must always have some base cases, which can be solved without recursion.
2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case.
3. Design rule.Assume that all the recursive calls work.
4. Compound interest rule. Never duplicate work by solving the same instance of a problem in separate recursive calls.
- 数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
- Four rules of recursion
- Quick sort and the Kth number
- the operation of recursion
- The four number game
- The power of recursion: Number-to-Text conversion
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The Rules of Probability
- 鱼群算法实现-The Three Simple Rules of Flocking Behaviors: Alignment, Cohesion, and Separation
- HDU - 4006 The kth great number (set&数据结构)
- HDU 4006:The kth great number(数据结构?)
- android面试题之五
- hdu 4101 - Ali and Baba(搜索)
- Java多线程程序设计
- 在AWK中输出特殊字符-单引号
- struts2中一个很实用的标签
- 数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
- C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象
- (void) (&_x == &_y)
- Ruby中,&:(ampersand-colon,"与号-冒号")的用法
- C#:System.Array简单使用
- Expected authority at index 7: hdfs://
- 初识Linux—3
- MFC--SendMessage、PostMessage原理
- 计算机科学中最重要的32个算法