各种小函数——C/C++源码
来源:互联网 发布:淘宝赠品送什么好 编辑:程序博客网 时间:2024/05/17 23:59
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != '\0')
NULL ;
}
2、memset:
{
char* pvTo=(char*)buffer;
assert(buffer != NULL);
while(count-->0)
*pvTo++=(char)c;
return buffer;
}
void * ret = dst;while (count--){*(char *)dst = *(char *)src;dst = (char *)dst + 1;src = (char *)src + 1;}return(ret);
char *pDest=static_cast<char *>(dest);const char *pSrc=static_cast<const char *>(src);//注意,这里是关键,为什么要这样比较呢?理由何在?if( pDest>pSrc && pDest<pSrc+count ){
for(size_t i=count-1; i<=0; ++i){
pDest[i]=pSrc[i];
}
}else{
for(size_t i=0; i<count; ++i){
pDest[i]=pSrc[i];
}
}return pDest;
5、快速排序:
#include<iostream>
usingnamespace std;
int cmp = 0; // 总共执行比较操作的次数
int swp = 0; // 总共执行交换操作的次数
int partition(int a[],int p, int r)
{
int x = a[r];
int i = p-1;
for(int j=p; j<=r-1; j++)
{
cmp++;
if( a[j] < x )
{
i++;
swp++;
swap(a[i], a[j]); // 小于x的放到前面,不稳定
}
}
i++;
swp++;
swap(a[i], a[r]);
return i;
}
void quickSort(int a[],int p, int r)
{
if( p < r )
{
int q = partition(a, p, r);
quickSort(a, p, q-1);
quickSort(a, q+1, r);
}
}
int main()
{
int a[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
quickSort(a, 0,sizeof(a)/sizeof(a[0])-1);
cout <<"总共进行比较 " << cmp <<" 次,总共进行交换 " << swp <<" 次" << endl;
for(int i=0; i<sizeof(a)/sizeof(a[0]); i++)
{
cout << a[i] <<" ";
}
cout << endl;
return 0;
}
6、堆排序
#include<iostream>
#include<ctime>
usingnamespace std;
// 注意父子的计算方式。节点编号从0开始。
inlineint parent(constint x) { return ((x-1)/2); }
inlineint left(constint x) { return (2*x+1); }
inlineint right(constint x) { return (2*x+2); }
int cmp = 0; // 总共执行比较操作的次数
int swp = 0; // 总共执行交换操作的次数
// 调整以i为根的子树,使之成为最大堆,size为堆的大小
void maxHeapify(int a[],int size, int i)
{
cmp +=2;
swp++;
int l = left(i);
int r = right(i);
int largest = i; // 最大堆的根
if( (l < size) && (a[l] > a[i]) ) largest = l;
if( (r < size) && (a[r] > a[largest]) ) largest = r;
if( largest != i )
{
swap(a[i], a[largest]); // 三个节点中较大者成为根
maxHeapify(a, size, largest); // 可能破坏了堆性质,重新调整
}
}
void buildMaxHeap(int a[],int size) // 建堆
{
for(int i = (size-1)/2; i>=0; i--)
{
maxHeapify(a, size, i);
}
}
void heapSort(int a[],int size) // 堆排序,(n-1)*O(lgn) = O(nlgn)
{
swp++;
buildMaxHeap(a, size);
for(int i=size-1; i>0; i--) // 重复n-1次
{
swap(a[0], a[i]);
size--;
maxHeapify(a, size, 0); // 每次调整,花费为O(lgn)
}
}
int main()
{
int a[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
heapSort(a,sizeof(a)/sizeof(a[0]));
cout <<"总共进行比较 " << cmp <<" 次,总共进行交换 " << swp <<" 次" << endl;
for(int i=0; i<sizeof(a)/sizeof(a[0]); i++)
{
cout << a[i] <<" ";
}
cout << endl;
return 0;
}
- 各种小函数——C/C++源码
- c语言小函数——strcat
- C 源码学习——memcpy函数
- C语言小函数
- C语言小函数
- c 内存小函数
- c字符串函数源码
- C/C++各种计时函数
- C语言小程序源码
- C语言小函数——atol(自己实现)
- C语言小函数——查找最长数字串
- C语言小程序—实现strlen等字符串函数
- C语言小函数—二进制与十六进制
- thinkphp源码讲解——配置管理C函数
- C 的各种指针 and 小细节。
- 各种排序算法源码(c++)20150913
- Linux C函数小总结
- c语言小函数1
- MySQL学习笔记(一)
- 十年
- zencart变量的定义
- Books, Papers and Links – Computer Graphics
- JSTL
- 各种小函数——C/C++源码
- Windows Metro Style颜色色值表
- 静静的在心里承受所有的伤痛和不快乐
- hdoj 1018
- 连连看算法
- Yii源码分析——collections
- WinPcap在Win32平台下的VC++配置
- 次世代贴图技术
- Linux常用命令使用