1Insert Sort

int InsertSort(int *a, int len)
{
    int i;
    for(i = 1; i < len; i++)
    {
        int j = i-1;
        int temp = a[i];
        while((j >= 0)&&(a[j] > temp))
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = temp;
    }
}



int  ShellSort(int *a, int len, int d)
{
    int i;
    for(i = 0; i < len; i++)
    {
        int j = i-d;
        int temp = a[i];
        while((j>=0)&&(j<len)&&(a[j]>temp)
&&((j+d)>=0)&&((j+d) < len))
        {
            a[j+d] = a[j];
            j-= d;
        }
        a[j+d] = temp;
    }
}


int BinaryInsertSort(int *a, int len)
{
    int i = 0;
    for(i = 0; i < len; i++)
    {
        int temp = a[i];
        int low = 0, high = i-1, mid;
        while(high >= low)
        {
            mid = (low + high)/2;
            if(a[mid] > temp)
            {
                high = mid - 1;
            }
            else
            {
                low = mid + 1;
            }
        }
        int j = i - 1;
        while((j >= 0)&&(j > high))
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = temp;
    }
}


 

int  QuickSort(int *a, int low, int high)
{        
            printf("a[%d]=%d \n", i, a[i]);                                                                                           
            PrintArray(array, LENGTH);                                                                                                
    int temp;
    int i = low, j = high;
    if( low < high )
    {
        temp = a[low];
        while( i < j )
        {
            while((j > i) && (a[j] >= temp)) j--;
            if( i < j )
            {
                a[i] = a[j];
                i++;
            }
            while((i < j) && a[i] < temp)    i++;
            if( i < j )
            {
                a[j] = a[i];
                j--;
            }
            a[i] = temp;
            QuickSort(a, low, i-1);
            QuickSort(a, i+1, high);
        }
    }
}
int kmp(char *a, char* b)
{
    int i=0, j=0;
    while((j < strlen(b)) && (i < strlen(a)))
    {
        if((j == -1) || (a[i] == b[j]))
        {
            i++;
            j++;
        }
        else
        {
            j = next[j];
        }
    }
    if(j = strlen(b))
    {
        return i - j;
    }
    else
    {
        return -1;
    }
}
相關文章
相關標籤/搜索