算法导论代码笔记(一)

插入排序

int i,key;
    for(i=1;i=0&&a[j]>key;--j)
        {
            a[j+1] = a[j];
        }
        a[j+1] = key;
    }


选择排序

int key;
    for(int i=0;i a[j])
            {
                int temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }


冒泡排序

for(int i=0;i=1;--j)
        {
            if(a[j]

归并排序

int b1[1000];
int b2[1000];
void Merge(int *p, int l, int h1, int h2)
{
	int k = 0,n1 = h1 - l + 1,n2 = h2 - h1;
	for (int i = l; i <= h1; ++i)
		b1[k++] = p[i];
	b1[n1] = MAX_INT;
	k = 0;
	for (int i = h1 + 1; i <= h2; ++i)
		b2[k++] = p[i];
	b2[n2] = MAX_INT;
	k = l;
	for (int i = 0, j = 0; k<=h2;++k)
	{
		if (b1[i] < b2[j])
			p[k] = b1[i++];
		else
            p[k] = b2[j++];
	}


}


void merge_sort(int *p, int l, int h)
{


    if (l < h)
	{
		int n = (h + l) / 2;
        merge_sort(p, l, n);
        merge_sort(p, n + 1, h);


		Merge(p, l, n, h);
	}
}

思考题2-4
int Merge(int *p, int l, int h1, int h2)
{
    int ans = 0;

	int k = 0,n1 = h1 - l + 1,n2 = h2 - h1;
	for (int i = l; i <= h1; ++i)
		b1[k++] = p[i];
	b1[n1] = MAX_INT;
	k = 0;
	for (int i = h1 + 1; i <= h2; ++i)
		b2[k++] = p[i];
	b2[n2] = MAX_INT;
	k = l;
	for (int i = 0, j = 0; k<=h2;++k)
	{
		if (b1[i] <= b2[j])
			p[k] = b1[i++];
		else
        {
            p[k] = b2[j++];
            ans += n1 - i;
        }
	}

    return ans;
}

int merge_sort(int *p, int l, int h)
{

    int ans = 0;
    if (l < h)
	{
		int n = (h + l) / 2;
        	ans += merge_sort(p, l, n) + merge_sort(p, n + 1, h);

		ans += Merge(p, l, n, h);
	}
	return ans;
}

快速排序

void quick_sort(int *a,int l,int h)
{
    if(l < h)
    {
        int x = l,temp;
        int i=x+1,j=x+1;
        for(;i<=h;++i)
        {
            if(a[i] < a[x])
            {
                temp = a[j];
                a[j++] = a[i];
                a[i] = temp;
            }
        }
        temp = a[j-1];
        a[j-1] = a[x];
        a[x] = temp;

        quick_sort(a,l,j-1);
        quick_sort(a,j,h);


    }
}



随机数优化快速排序
int Rand(int l,int h)
{
    srand(time(NULL));
    return (rand()%(h-l+1))+l;
}
void Swap(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void quick_sort(int *a,int l,int h)
{
    if(l < h)
    {
        int x = Rand(l,h);
        Swap(&a[x],&a[l]);
        int i=l+1,j=l+1;
        for(;i<=h;++i)
        {
            if(a[i] < a[l])
            {
                Swap(&a[i],&a[j]);
                ++j;
            }
        }
        Swap(&a[j-1],&a[l]);
        quick_sort(a,l,j-1);
        quick_sort(a,j,h);
    }
}


堆排序

#include 
#include 

void swap (int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

MinHeapFixDown(int *a,int i,int n)
{
    int j,temp;
    temp = a[i];
    j = 2 * i + 1;
    while(j < n)
    {
        if(j + 1 < n && a[j+1] < a[j])
            ++j;
        if(a[j] >= temp)
            break;
        a[i] = a[j];
        i = j;
        j = 2 * i + 1;
    }
    a[i] = temp;
}
MinHeapFixUp(int *a,int i,int n)
{
    int temp = a[i],j = 2*i+1;
    while(j < n)
    {
        if(j + 1 < n &&a[j+1] > a[j])
            ++j;
        if(a[j] <= temp)
            break;
        a[i] = a[j];
        i = j;
        j = 2 * i + 1;
    }
    a[i] = temp;
}

BuildMinHeap (int *a,int n)
{
    int i;
    for(i = (n / 2) - 1;i>=0;--i)
    {
        MinHeapFixDown(a,i,n);
    }
}

BuildMaxHeap(int *a,int n)
{
    int i = (n / 2) - 1;
    for(;i>=0;--i)
        MinHeapFixUp(a,i,n);
}

DownHeapSort(int *a,int n)
{
    int i;
    BuildMinHeap (a,n);
    for(i = n-1;i>=0;--i)
    {
        swap(&a[i],&a[0]);
        MinHeapFixDown(a,0,i);
    }
}
UpHeapSort(int *a,int n)
{
    BuildMaxHeap(a,n);
    int i = n-1;
    for(;i>=0;--i)
    {
        swap(&a[0],&a[i]);
        MinHeapFixUp(a,0,i);
    }
}
int main()
{
    int a[10] = {32,324,23,24,235,4,34,45,32,36};

    UpHeapSort(a,10);

    int i;
    for(i = 0;i<10;++i)
        printf("%d ",a[i]);

    return 0;
}


计数排序

int a[] = {100,4,2,5,5,6,22,325,666,4};
int b[10];
int c[1000];
void count_sort(int *a,int *b,int l,int h)
{
    memset(c,0,sizeof(c));
    int k = -1;
    for(int i=l;i<=h;++i)
    {
        k = (k>=a[i]?k:a[i]);
        ++c[a[i]];
    }
    for(int i=1;i<=k;++i)
    {
        c[i] = c[i-1] + c[i];
    }
    for(int i=h;i>=l;--i)
    {
        b[--c[a[i]]] = a[i];
    }
}
基数排序

int a[] = {0x0fff,0x0f0f,0x0ff0,0x000f,0x00ff,0x0f0f,0x00f0,0xffff,0x0000,0x00ff};
int c[256];
void count_sort(int basic,int off,int *a,int *b,int l,int h)
{
    memset(c,0,sizeof(c));
    int k = -1,t;
    for(int i=l;i<=h;++i)
    {
        t = (a[i]&basic)>>(4*off);
        //printf("basic:%x  t:%x\n",basic,t);
        k = (k>=t?k:t);
        ++c[t];
    }
    for(int i=1;i<=k;++i)
    {
        c[i] = c[i-1] + c[i];
    }
    for(int i=h;i>=l;--i)
    {
        t = (a[i]&basic)>>(4*off);
        b[--c[t]] = a[i];
    }
}
//数组初始化为16位 bits=16
void basic_sort(int bits,int *a,int l,int h)
{
    int times = bits / 4;
    int **b = (int**)malloc(times * sizeof(int*));
    for(int i=0;i

查找第i小

int Rand(int l,int h)
{
    srand(time(NULL));
    return (rand()%(h-l+1))+l;
}
void Swap(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
int random_select(int *a,int l,int h,int f)
{
    if(l < h)
    {
        int x = Rand(l,h);
        Swap(&a[x],&a[l]);
        int i=l+1,j=l+1;
        for(;i<=h;++i)
        {
            if(a[i] < a[l])
            {
                Swap(&a[i],&a[j]);
                ++j;
            }
        }
        Swap(&a[j-1],&a[l]);
        cout<<"tt"<






你可能感兴趣的:(学习笔记)