首页 > 代码库 > chapter 15 排序

chapter 15 排序

几种排序方法:冒泡 希尔  插入  快排  堆排  归并


sort.h

#ifndef _SORT_H_
#define _SORT_H_

void insert_sort(int*, int);
void bubble_sort(int*, int);
void shell_sort(int *, int);
void quick_sort(int*, int, int);
void heap_sort(int*, int);
void merge_sort(int *, int *, int, int);

#endif /*SORT_H*/


sort.c

#include "sort.h"

static void merge(int *, int *, int , int , int );
void insert_sort(int *a, int len)
{
    int i, j;
    for(i = 1; i < len; ++i)
        if(a[i] < a[i-1])
        {
            int tmp = a[i];
            for(j = i -1; j >= 0 && a[j] > tmp; j--)
            {
                a[j+1] = a[j];
            }
            a[j+1] = tmp;
        }
}

void bubble_sort(int *a, int len)
{
    int i, j;
    for(i = 1; i < len; ++i)
    {
        for(j = 0; j < len - i; ++j)
            if(a[j] > a[j+1])
            {
                a[j] ^= a[j+1];
                a[j+1] ^= a[j];
                a[j] ^= a[j+1];
            }
    }
}

void shell_sort(int *a, int len)
{
    int dk, i, j;
    for(dk = len/2; dk > 0; dk /= 2)
        for(i = dk ; i < len; ++i)
            if(a[i] < a[i-dk])
            {
                int tmp = a[i];
                for(j = i - dk; j >= 0 && a[j] > tmp; j -= dk)
                {
                    a[j + dk] = a[j];
                } 
                a[j + dk] = tmp;
            }
}

static int partion(int *a, int low, int high)
{
    int tmp = a[low];
    while(low < high)
    {
        while(low < high && a[high] >= tmp)
            high--;
        if(low < high)
            a[low] = a[high];
        while(low < high && a[low] <= tmp)
            low++;
        if(low < high)
            a[high] = a[low];
    }
    a[low] = tmp;
    return low;
}

void quick_sort(int *a, int low, int high)
{
    if(low < high)
    {
        int pos = partion(a, low, high);
        quick_sort(a, low, pos-1);
        quick_sort(a, pos+1, high);
    }
}

static void heapadjust(int *a, int k, int high)
{//小顶堆的调节
    int tmp = a[k];
    int i;
    for(i = 2*k + 1; i <= high; i = 2*i + 1)//下表从1开始的话就可以直接2*k
    {
        if(a[i+1] <  a[i])//找到子节点比较小的哪一个
            i++;
        if(tmp <= a[i])//如果要查看的节点比子节点最小的还小,表示符合小顶堆规则
            break;
        else
        {
            a[k] = a[i];
            k = i;
        }
    }
    a[k] = tmp;
}

void heap_sort(int *a, int len)
{
    int i;
    for(i = len/2 - 1; i >= 0; --i)//因为是从下表为0开始的,所以此处要减一
        heapadjust(a, i, len - 1);
}

void merge_sort(int *a, int *b, int low, int high)
{
    int mid;
    if(low < high)
    {
        mid = (low + high)/2;
        merge_sort(a, b, low, mid) ;
        merge_sort(a, b, mid+1, high);
        merge(a, b, low, mid, high);
    }
}

void merge(int *a, int *b, int low, int mid, int high)
{
    int i, j, k;
    for(i = low; i <= high; ++i)
        b[i] = a[i];
    i = low;
    k = low;
    j = mid + 1;
    while(i <= mid && j <= high)
    {
        if(b[i] < b[j])
            a[k++] = b[i++];
        else
            a[k++] = b[j++];
    }
    while(i <= mid)
        a[k++] = b[i++];
    while(j <= high)
        a[k++] = b[j++];
}

test.c

#include "sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 15 

static void print(int *a, int len);

int main(int argc, const char *argv[])
{
    int a[N], b[N], c[N], d[N], e[N], f[N], tmp[N];
    srand(time(NULL));

    int i;
    for(i = 0; i < N; ++i)
    {
    a[i] = rand()%1000;
    f[i] = e[i] = d[i] = c[i] = b[i] = a[i];
    }
  
    insert_sort(a, N);
    printf("insert:\n");
    print(a, N);
    
    bubble_sort(b, N);
    printf("bubble:\n");
    print(b, N);
    
    shell_sort(c, N);
    printf("shell:\n");
    print(c, N);
    
    quick_sort(d, 0, N-1);
    printf("quick:\n");
    print(d, N);
    
    heap_sort(e, N);
    printf("heap:\n");
    print(e, N);

    merge_sort(f, tmp, 0, N-1);
    printf("merge:\n");
    print(f, N);

    return 0;
}

void print(int *a, int len)
{
 int i;
 for(i = 0; i < len; ++i)
  {
   if(i !=0 && i % 10 ==0)
       printf("\n");
      printf("%-4d", a[i]);
  }
 printf("\n");
}


chapter 15 排序