3月 192013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef int Datatype;
int random(int i, int j)
{
    return i + rand() % (j-i+1);
}

int randomPartition(Datatype A[], int low, int high)
{
    int i = random(low,high);
    int temp;
    temp = A[high];
    A[high] = A[i];
    A[i] = temp;
    return  partition(A, low, high);
}
int partition(Datatype A[], int low, int high)
{
    int i=low,
        j=high;
    Datatype pivot = A[low];
    while(i < j)
    {
        while(i<j&&A[j] >= pivot) j--;
        if(i<j) A[i++] = A[j];
        while(i<j&&A[i] <= pivot) i++;
        if(i<j) A[j--] = A[i];
    }
    A[i] = pivot;
    return i;
}

void quicksort(Datatype A[], int low, int high)
{
    int divide;
    if(low<high)
    {
        divide = randomPartition(A,low,high);
        //divide = partition(A,low,high);
        quicksort(A, low, divide);
        quicksort(A, divide+1, high);
    }
}

int main()
{
    int i;
    Datatype A[10] ={2,4,6,1,7,3,9,1,90,23};
    quicksort(A, 0, sizeof(A)/sizeof(Datatype));
    for(i=0;i<(sizeof(A)/sizeof(Datatype));i++)
    {
        printf("%d ",A[i]);
    }
    printf("\n");
    return 0;
}

3月 142013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

strcmp源码
原型:extern int strcmp(const char *s1,const char * s2);
所在头文件:string.h
功能:比较字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)

 int __cdecl strcmp (const char * src, const char * dst)
 {
     int ret = 0 ;
     while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) &amp;&amp; *dst&amp;&amp;*src)
     ++src, ++dst;
     if ( ret &lt; 0 )
         ret = -1 ;
     else if ( ret &gt; 0 )
         ret = 1 ;
     return( ret );
 }
神一般的 strcpy
考虑NULL指针,const修饰,地址覆盖问题
char *mystrcpy2(char* dst, const char *src)
 {
 char *ret = dst;
 int count = strlen(src)+1;
 assert(dst);
 assert(src);
 if( dst&lt;=src || dst&gt;=(src+count) )//地址不重合,从低字节开始复制
 {
 while(count--)
 {
 *dst++ = *src++;
 }
 }
 else //地址重合,从高字节开始复制
 {
 dst = dst + count - 1;
 src = src + count - 1;
 while(count--)
 {
 *dst-- = *src--;
 }
 }
 return dst;
 }