排序

二分查找


#include <stdlib.h>
#include <string.h>

#include "search.h"

int bisearch(void *sorted, const void *target, int size, int esize, int (*compare)(const void *key1, const void *key2))
{
    int left,
        right,
        middle;

    left = 0;
    right = size - 1;

    while(left <= right)
    {
        middle = (right + left) / 2;

        switch(compare((char *)sorted+(esize*middle), target))
        {
            case -1:
                left = middle + 1;
                break;

            case 1:
                right = middle - 1;
                break;

            case 0:
                return middle;
        }
    }

    //not found.
    return -1;
}



插入排序

#include "sort.h"
#include <stdlib.h>
#include <string.h>

int issort( void *data, int size, int esize, int (*compare)(const void *key1, const void *key2) )
{
    void *a = data;
    char *key;
    int i,
        j;

    key = (char *)malloc(sizeof(char)*esize);
    if( key == NULL )
        return -1;

    for(j = 1; j < size; j++ )
    {
        memcpy(key, &a[j*esize], esize);
        i = j-1;
        while( i >= 0 && compare( &a[i*esize], key ) > 0 )
        {
            memcpy(&a[(i+1)*esize],&a[i*esize], esize );
            i--;
        }
        
        memcpy(&a[(i+1)*esize], key, esize);

    }

    free(key);
    return 0;
}




快速排序

#include <stdlib.h>
#include <string.h>

#include "sort.h"

/* partition */
static int partition(void *data, int esize,  int i, int k, 
        int (*compare)(const void *key1, const void *key2))
{
    char *a = data;
    void *pval,
         *temp;

    int r[3];

    /* Allocate storage for the partition value and swapping */
    pval = malloc(esize);
    if( pval == NULL )
        return -1;

    temp = malloc(esize);
    if( temp == NULL )
        return -1;

    r[0] = (rand()%(k-i+1)) + i;
    r[1] = (rand()%(k-i+1)) + i;
    r[2] = (rand()%(k-i+1)) + i;

    issort(r,3, sizeof(int), compare_int);

    memcpy(pval, &a[r[1]*esize], esize);

    /* Create two partitions around the partition value. */
    i--;
    k++;

    while(1)
    {
        /* Move left until an element is found in the wrong partition */
        do
        {
            k--;
        }while(compare(&a[k*esize], pval) > 0);

        do
        {
            i++;
        }while(compare(&a[i*esize], pval) < 0 );

        if( i >= k )
        {
            break;
        }
        else
        {
            /* swap the value */
            memcpy(temp, &a[i*esize], esize);
            memcpy(&a[i*esize], &a[k*esize],esize);
            memcpy(&a[k*esize], temp, esize);
        }
    }

    free(pval);
    free(temp);
    return k;
}


int qksort(void *data, int size, int esize, int i, int k, int (*compare)(const void *key1, const void *key2))
{
    while( i < k )
    {
        if( (j = partition(data, size, i, k, compare)) < 0 )
            return -1;

        if( qksort(data, size, esize, i, j, compare) < 0 )
            return -1;

        i = j + 1;
    }

    return 0;
}





计数排序


#include <stdlib.h>
#include <string.h>

#include "sort.h"

int ctsort(void *data, int size, int k)
{
    int *counts,
        *temp;

    int i,
        j;


    /* Allocate storage for the counts */
    counts = malloc(k*sizeof(int));
    if( counts == NULL )
        return -1;

    temp = malloc(size*sizeof(int));
    if( temp  == NULL )
        return -1;
    
    /* init the counts. */
    for(i = 0; i < k; i++ )
        counts[i] = 0;

    /* count the occurrences of each element */
    for(j = 0; j < size; j++ )
        counts[data[j]] = counts[data[j]]+1;

    /* Adjust each count to reflect the counts before it. */
    for(i = 1; i < k; i++ )
        counts[i] += count[i-1];

    /* Use the counts to position each element where it belongs. */
    for(j = size-1; j >= 0; j-- )
    {
        temp[counts[data[j]] - 1] = data[j];
        counts[data[j]] = counts[data[j]] - 1;
    }

    memcpy(data, temp, size * sizeof(int));

    free(counts);
    free(temp);

    return 0;
}




归并排序

#include <stdlib.h>
#include <string.h>

#include "sort.h"

/* merge */
static int merge(void *data, int esize, int i, int j, int k, int (*compare)(const void *key1, const void *key2))
{
    char *a = data,
         *m;

    int ipos,
        jpos,
        mpos;

    /* Initialize the counters used in mergeing. */
    ipos = i;
    jpos = j+1;
    mpos = 0;

    /* Allocate the storage for the merged elements. */
    if( (m = (char *)malloc(esize*((k-i)+1))) == NULL  )
        return -1;

    while( ipos <= j || jpos <= k )
    {
        if( ipos > j )
        {
            /* the left division has no more elements to merge */
            while( jpos <= k )
            {
                memcpy( &m[mpos * esize], &a[jpos*esize], esize);
                jpos++;
                mpos++;
            }
            continue;
        }
        else if( jpos > k )
        {
            /* The right division has no more elements to merge */
            while( ipos <= j )
            {
                memcpy(&m[mpos * esize], &a[ipos *esize], esize);
                ipos++;
                mpos++;
            }
            continue;
        }

        /* Append the next ordered element to the merged elements */
        if( compare(&a[ipos * esize], &a[jpos * esize]) < 0 )
        {
            memcpy( &m[mpos * esize], &a[ipos * esize], esize );
            ipos++;
            mpos++;
        }
        else
        {
            memcpy( &m[mpos * esize], &a[jpos * esize], esize);
            jpos++;
            mpos++;
        }
    }

    /* Prepare to pass back the merged data.*/
    memcpy(&a[i * esize], m, esize * ((k - i) + 1));
    free(m);

    return 0;
}


int mgsort(void *data, int size, int esize, int i, int k, int (*compare)(const void *key1, const void *key2))
{
    int j;

    if( i < k )
    {
        j = (int)(((k - i)+1)/2);
        
        if( mgsort(data, size, esize, i, j, compare) < 0 )
            return -1;

        if( mgsort(data, size, esize, j+1, k, compare) < 0 )
            return -1;

        if( merge(data, esize, i, j, k, compare) < 0 )
            return -1;
    }

    return 0;
}





String实现

class String
{
    public:
        String(const char *str = NULL);//common constructor
        String(const String &other); //copy constructor

        ~String(void); //desconstructor
        
        String & operate=(const String &other);//assignment function

    private:
        char *m_data; //use to keep ths string.
}

String::String(const char *str = NULL)
{
    if( str == NULL )
    {
        m_data = new char[1];
        *m_data = '\0';
    }
    else
    {
        int length = strlen(str);
        m_data = new char[length+1];
        strcpy(m_data, str);
    }
}


String::String(const String &other)
{
    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);
}



String & String::operate=(const String &other)
{
    if(this == &other)
        return *this;

    delete [] m_date;

    int length = strlen(other.m_data);
    m_data = new char[length+1];

    strcpy(m_data, other.m_data);

    return *this;
}




你可能感兴趣的:(排序)