C语言排序算法

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

#define ARRAYSIZE 10

int data[ARRAYSIZE];

void initData()
{
    int i = 0;
    srand((int)time(0));
    for(;i<ARRAYSIZE;i++)
    {
        data[i] = (int)(100*rand()/(RAND_MAX + 1.0)) + 1;
    }
}
void printData(char *string)
{
    int i = 0;
    printf("%s\n",string);
    for(;i<ARRAYSIZE;i++)
    {
        printf("%-4d",data[i]);
    }
    printf("\n");
}
void swap(int i,int j)
{
    int temp;
    temp = data[i];
    data[i] = data[j];
    data[j] = temp;
}
/*直接插入排序。从第一个元素开始,形成长度为1,2,3乃至N的有序数组,
选取第N+1个元素插入到有序数组中,直到最后一个元素,从而形成有序排列的数组*/
void DirectInsertSort(int data[], int length)
{
    int index = 1;
    int left;
    int temp;
    while(index < length)
    {
        left = index - 1;
        temp = data[index];
        if(temp > data[index-1])
        {
            index++;
            continue;
        }
        else
        {
            while(left >= 0 && data[left] > temp)
            {
                data[left+1] = data[left];
                left--;
            }
            data[left+1] = temp;
            index++;
        }
    }
}
/*冒泡排序。如其名,从第一个元素开始,依次比较相邻的两个元素
并始终保证较大的元素在右侧,从而通过一次次的排序将大元素放在右侧小元素放在左侧。*/
void BubbleSort(int data[], int length)
{
    int index = length - 1;
    int i;
    int flag = -1;
    while(index > 1)
    {
        for(i = 0;i < index;i++)
        {
            if(data[i] > data[i+1])
            {
                swap(i,i+1);
                flag = 1;
            }
        }
        index--;
        if(flag==-1)
            break;
    }
}
/*选择排序。顾名思意,就是直接从待排序数组里选择一个最小(或最大)的数字,
每次都拿一个最小数字出来,顺序放入新数组,直到全部拿完*/
void SelectSort(int data[], int length)
{
    int index = 0;
    int temp;
    int min;
    int minIndex;
    while(index < length)
    {
        temp = index;
        min = data[temp];
        for(;temp<length;temp++)
        {
            if(data[temp] < min)
            {
                min = data[temp];
                minIndex = temp;
            }
        }
        data[minIndex] = data[index];
        data[index] = min;
        index++;
    }
}
/*快速排序*/
void QuickSort(int data[],int left,int right)
{
    int i = left;
    int j = right;
    int base;
    if(left < right)
    {
        base = data[left];
        while(i != j)
        {
            while(j > i && data[j] > base)
                j--;
            data[i] = data[j];
            while(i < j && data[i] < base)
                i++;
            data[j] = data[i];
        }
        data[i] = base;
        QuickSort(data,left,i-1);
        QuickSort(data,i+1,right);
    }
}
/*希尔排序。在直接插入排序的基础上增加了一个增量。*/
void ShellSort(int data[],int length)
{
    int i;
    int j;
    int temp;
    int gap = length/2;
    while(gap > 0)
    {
        for(i = gap;i < length;i++)
        {
            temp = data[i];
            j = i - gap;
            while(j>=0 && data[j] > temp)
            {
                data[j+gap] = data[j];//数据往后移动一位
                j -= gap;
            }
            data[j+gap] = temp;
        }
        gap /= 2;
    }
}
/*归并排序*/ 
void Merge(int data[],int low,int mid,int high)
{
    int *temp;
    int i = low;
    int j = mid + 1;
    int k = 0;
    temp = (int *)malloc((high-low+1)*sizeof(int));
    while(i <= mid && j <= high)
    {
        if(data[i] <= data[j])
        {
            temp[k] = data[i];
            i++;
            k++;
        }
        else
        {
            temp[k] = data[j];
            j++;
            k++;
        }
    }
    while(i <= mid)
    {
        temp[k] = data[i];
        i++;
        k++;
    }
    while(j <= high)
    {
        temp[k] = data[j];
        j++;
        k++;
    }
    for(k = 0,i = low;i <= high;k++,i++)
    {
        data[i] = temp[k];
    }
}
void MergeSort(int data[],int low,int high)
{
    int mid;
    if(low < high)
    {
        mid = (low + high)/2;
        MergeSort(data,low,mid);
        MergeSort(data,mid+1,high);
        Merge(data,low,mid,high);
    }
}
int main(void)
{
    initData();
    printData("Initial Data:");
    MergeSort(data,0,ARRAYSIZE-1);
    printData("Afert Sort:");
    return 0;
}

 

posted @ 2013-08-21 16:17  Berne  阅读(271)  评论(0编辑  收藏  举报