各种小函数——C/C++源码

1、strcpy:
char *strcpy(char *strDest, const char *strSrc)
{
    assert((strDest!=NULL) && (strSrc !=NULL));
    char *address = strDest;    
    while( (*strDest++ = * strSrc++) != '\0')
       NULL ;
    return address ;      
}


2、memset:

void * Memset(void* buffer, int c, int count)
{
    char* pvTo=(char*)buffer;
    assert(buffer != NULL);
    while(count-->0)
         *pvTo++=(char)c;
    return buffer;
}


3、memcpy——不考虑数据区重叠
void   *   __cdecl memcpy (   void   *   dst,   const   void   *   src, size_t count )
 { 
void   *   ret   =   dst; 
while   (count -- )
 { 
    * ( char   * )dst   =   * ( char   * )src; 
    dst   =   ( char   * )dst   +   1
    src   =   ( char   * )src   +   1
} 
return (ret);
 }

4、memmove——考虑数据区重叠
void   * memmove( void   * dest, const   void   * src,size_t count)
 { 
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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值