algorithm
bigsnowstorm
这个作者很懒,什么都没留下…
展开
-
冒泡排序
//升序排列;先确定最大的数void bubble_sort(int a[], int size){ bool swapped = false; for(int i = 0; i < size-1 ; i++) //外层进行size-1次循环,如有10个数,外层进行进行9次循环 { swapped = false; for(int j=0; j < size-1-i ; j++)原创 2013-08-17 17:46:14 · 590 阅读 · 0 评论 -
求两个数的最大值
#include int cal_max(const int &a, const int &b){ int c; c = (a+b+abs(a-b))/2; return c;}int cal_max_2(const int &a, const int &b){ bool c=false; c = (bool)((unsigned int)(a-b) >> (sizeof(原创 2013-08-17 17:48:18 · 1339 阅读 · 0 评论 -
数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
书中首先介绍了两种简单的方法: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 elemen原创 2013-09-24 23:59:01 · 1087 阅读 · 0 评论 -
数据结构与算法分析笔记:CHAPTER 2: ALGORITHM ANALYSIS
2.1. Mathematical BackgroundThroughout the book we will use the following four definitions:DEFINITION: T(n) = O(f(n)) if there areconstants c and n0 such that T(n)cf (n) when n>= n0.DEFINITI原创 2013-11-25 02:39:58 · 1567 阅读 · 0 评论 -
数据结构与算法分析笔记:第一章:递归
一、递归的四条基本法则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 recursio原创 2013-09-27 07:58:07 · 672 阅读 · 0 评论 -
用递归实现n是2的几次方
//用递归实现n是2的几次方int log2(int n){ if(!(n&(n-1))) { if(1 == n) { return 0; } else { return 1+log2(n>>1); } } else { return 0; }}转载 2013-08-17 17:49:31 · 1974 阅读 · 0 评论 -
找到第二大的数
int find_sec_max(int *a, int size){ int max, sec_max; if(a[0] > a[1]) { max = a[0]; sec_max = a[1]; } else { max = a[1]; sec_max = a[0]; } for(int i=2; i < size; i++) { if(a[i] >转载 2013-08-17 17:50:38 · 591 阅读 · 0 评论 -
给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数
//给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数。int cal_num_of_1(int n){ int count = 0; int temp; for(int i = 1; i<n+1; i++) { temp = i; while(temp>0) { if(temp%10 == 1) { count++; } tem转载 2013-08-17 17:56:08 · 1424 阅读 · 0 评论