排序算法

5 篇文章 0 订阅
3 篇文章 0 订阅
本文主要编写了一系列到排序算法,并比较了一下时间快慢!在这提供所有到代码给大家作为参考。由于水平有限难免有错误到地方,请大家指出。具体可以参考《大话数据结构》关于排序算法一章。

1. 排序算法头文件

#ifndef __SORTS_H__
#define __SORTS_H__

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

#define MAXSIZE 1000
#define MAX_LENGTH_INSERT_SORT 7

typedef int Status;
#define TRUE 1
#define FALSE 0

typedef struct
{
    int r[MAXSIZE];
    int length;
}SqList;

//打印数组,创建数组
void PrintSqList(SqList *L);
void CreateSqList(SqList *L);

//数据交换
void swap(SqList *L, int i, int j);

/**********************************************************/
//插入排序
void InsertSort(SqList *L);

/*********************************************************/
//选择排序
void SelectSort(SqList *L);

/*********************************************************/
//冒泡排序
void BubbleSort_1(SqList *L);
void BubbleSort_2(SqList *L);
void BubbleSort_3(SqList *L);

/********************************************************/
//希尔排序
void ShellSort(SqList *L);

/********************************************************/
//对排序
void HeapAdjust(SqList *L, int s, int m);
void HeapSort(SqList *L);

/********************************************************/
//归并排序-递归实现
void Merge(int SR[], int TR[], int i, int m, int n);
void MSort(int SR[], int TR[], int s, int t);
void MergeSort_1(SqList *L);

//归并排序-非递归实现
void MergePass(int SR[], int TR[], int s, int n);
void MergeSort_2(SqList *L);

/*******************************************************/
//快速排序算法递归实现
int Partition_1(SqList *L, int low, int high);
void QSort_2(SqList *L, int low, int high);
void QuickSort_1(SqList *L);

//快速排序算法优化取枢轴
int Partition_2(SqList *L, int low, int high);
void QSort_2(SqList *L, int low, int high);
void QuickSort_2(SqList *L);

//快速排序优化不必要交换
int Partition_3(SqList *L, int low, int high);
void QSort_3(SqList *L, int low, int high);
void QuickSort_3(SqList *L);

//快读排序优化小数组时的排序方案
int Partition_4(SqList *L, int low, int high);
void QSort_4(SqList *L, int low, int high);
void QuickSort_4(SqList *L);

//优化递归操作
int Partition_5(SqList *L, int low, int high);
void QSort_5(SqList *L, int low, int high);
void QuickSort_5(SqList *L);

#endif

2.排序算法实现文件

#include "sorts.h"

void PrintSqList(SqList *L)
{
    int i;
    for(i = 0; i < L->length - 1; i++)
    {
        printf("%6d ->", L->r[i]);
    }
    printf(" %6d\n",L->r[i]);
}

void CreateSqList(SqList *L)
{
/*    int i, j, n;
    printf("请输入数组到个数:");
    canf("%d", &n);
    L->length = n;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &L->r[i]);
    }
*/
    int i;
    L->length = MAXSIZE;
    for(i = 0; i < MAXSIZE; i++)
    {
        L->r[i] = rand()%MAXSIZE + 1;
    }
}

void swap(SqList *L, int i, int j)
{
    int temp = L->r[i];
    L->r[i] = L->r[j];
    L->r[j] = temp;
}

void InsertSort(SqList *L)
{
    int i, j, temp;
    for(i = 1; i < L->length; i++)
    {
        if(L->r[i] < L->r[i-1])
        {
            temp = L->r[i];
            for(j = i - 1; (L->r[j] > temp) && (j >= 0); j--)
            {
                L->r[j+1]=L->r[j];
            }
            L->r[j+1] = temp;
        }
    }
}

void SelectSort(SqList *L)
{
    int min, i, j;
    for(i = 0; i < L->length; i++)
    {
        min = i;
        for(j = i + 1; j < L->length; j++)
        {
            if(L->r[min] > L->r[j])
            {
                min = j;
            }
        }
        if(i != min)
        {
            swap(L, i, min);
        }
    }
}

void BubbleSort_1(SqList *L)
{
    int i, j;
    for(i = 0; i < L->length; i++)
    {
        for(j = i + 1; j < L->length; j++)
        {
            if(L->r[i] > L->r[j])
            {
                swap(L, i, j);
            }
        }
    }
}

void BubbleSort_2(SqList *L)
{
    int i, j;
    for(i = 0; i < L->length; i++)
    {
        for(j = L->length-1; j > i; j--)
        {
            if(L->r[j-1] > L->r[j])
            {
                swap(L, j-1, j);
            }
        }
    }
}


void BubbleSort_3(SqList *L)
{
    int i, j;
    Status flag = TRUE;
    for(i = 0; (i < L->length) && flag; i++)
    {
        flag = FALSE;
        for(j = L->length-1; j > i; j--)
        {
            if(L->r[j-1] > L->r[j])
            {
                flag = TRUE;
                swap(L, j -1, j);
            }
        }
    }
}

void ShellSort(SqList *L)
{
    int temp;
    int i, j;
    int increment = L->length;
    do
    {
        increment = increment/3 + 1;
        for(i = increment; i < L->length; i++)
        {
            if(L->r[i] < L->r[i-increment])
            {
                temp = L->r[i];
                for(j = i-increment; (j >= 0) && temp < L->r[j]; j -= increment)
                {
                    L->r[j+increment] = L->r[j];    
                }    
                L->r[j + increment] = temp;
            }
        }
    }
    while(increment > 1);
}

void HeapAdjust(SqList *L, int s, int m)
{
    int temp = L->r[s];
    int i;
    for(i = 2*s + 1; i <= m; i = 2*i + 1)
    {
        if(i+1 <= m)
        {
            if(L->r[i] < L->r[i + 1])
                i++;
        }
        if(L->r[i] <= temp)
            break;
        L->r[s] = L->r[i];
        s = i;
    }
    L->r[s] = temp;
}

void HeapSort(SqList *L)
{
    int i;
    for(i = L->length/2 - 1; i >= 0; i--)
        HeapAdjust(L, i, L->length - 1);
    for(i = L->length-1; i >= 0; i--)
    {
        swap(L, 0, i);
        HeapAdjust(L, 0, i-1);
    }    
}
                
void Merge(int SR[], int TR[], int i, int m, int n)
{
    int j, k, l;
    for(j = m+1, k = i; j <= n && i <= m; k++)
    {
        if(SR[i] < SR[j])
            TR[k] = SR[i++];
        else
            TR[k] = SR[j++];
    }
    
    if(i <= m)
    {
        for( l = 0; l <= m - i; l++)
        {
            TR[k++] = SR[i + l];
        }
    }

    if(j <= n)
    {
        for(l = 0; l <= n - j; l++)
        {
            TR[k++] = SR[j + l];
        }
    }
}

void MSort(int SR[], int TR[], int s, int t)
{
    int m;
    int TR2[MAXSIZE + 1];
    if(s == t)
        TR[s] = SR[s];
    else
    {
        m = (s + t)/2;
        MSort(SR, TR2, s, m);
        MSort(SR, TR2, m+1, t);
        Merge(TR2, TR, s, m, t);
    }
}    

void MergeSort_1(SqList *L)
{
    MSort(L->r, L->r, 0, L->length-1);
}


void MergePass(int SR[], int TR[], int s, int n)
{
    
}


int Partition_1(SqList *L, int low, int high)
{
    int pivotkey = L->r[low];
    
    while(low < high)
    {
        while(low < high && L->r[high] >= pivotkey)
            high--;
        swap(L, low, high);
        while(low < high && L->r[low] <= pivotkey)
            low++;
        swap(L, low, high);
    }
    
    return low;
}
    
void QSort_1(SqList *L, int low, int high)
{
    int pivot;

    if(low < high)
    {
        pivot = Partition_1(L, low, high);
        QSort_1(L, low, pivot-1);
        QSort_1(L, pivot + 1, high);
    }
}

void QuickSort_1(SqList *L)
{
    QSort_1(L, 0, L->length-1);
}    


int Partition_2(SqList *L, int low, int high)
{
    int pivotkey;
    int m = low + (high - low)/2;
    if(L->r[low] > L->r[high])
        swap(L, low, high);
    if(L->r[m] > L->r[high])
        swap(L, m, high);
    if(L->r[low] < L->r[m])
        swap(L, low, m);
    
    pivotkey = L->r[low];
    
    while(low < high)
    {
        while(low < high && L->r[high] >= pivotkey)
            high--;
        swap(L, low, high);
        while(low < high && L->r[low] <= pivotkey)
            low++;
        swap(L, low, high);
    }
    return low;
}
    
void QSort_2(SqList *L, int low, int high)
{
    int pivot;
    if(low < high)
    {
        pivot = Partition_2(L, low, high);
        QSort_2(L, low, pivot - 1);
        QSort_2(L, pivot + 1, high);
    }
}
                
void QuickSort_2(SqList *L)
{
    QSort_2(L, 0, L->length-1);
}


int Partition_3(SqList *L, int low, int high)
{
    int pivotkey;
    int temp;
    int m = low + (high - low)/2;
    if(L->r[low] > L->r[high])
        swap(L, low, high);
    if(L->r[m] > L->r[high])
        swap(L, m, high);
    if(L->r[low] < L->r[m])
        swap(L, low, m);

    temp = pivotkey = L->r[low];
    
    while(low < high)
    {
        while(low < high && L->r[high] >= pivotkey)
            high--;
        L->r[low] = L->r[high];
        while(low < high && L->r[low] <= pivotkey)
            low++;
        L->r[high] = L->r[low];
    }
    L->r[low] = temp;
    return low;
}    

void QSort_3(SqList *L, int low, int high)
{
    int pivot;
    if(low < high)
    {
        pivot = Partition_3(L, low, high);
        QSort_3(L, low, pivot - 1);
        QSort_3(L, pivot + 1, high);
    }
}


void QuickSort_3(SqList *L)
{
    QSort_3(L, 0, L->length-1);
}


int Partition_4(SqList *L, int low, int high)
{
    int pivotkey;
    int temp;
    int m = low + (high - low)/2;
    if(L->r[low] > L->r[high])
        swap(L, low, high);
    if(L->r[m] > L->r[high])
        swap(L, m, high);
    if(L->r[low] < L->r[m])
        swap(L, m, low);

    temp = pivotkey = L->r[low];
    while(low < high)
    {
        while(low < high && L->r[high] >= pivotkey)
            high--;
        L->r[low] = L->r[high];
        while(low < high && L->r[low] <= pivotkey)
            low++;
        L->r[high] = L->r[low];
    }
    L->r[low] = temp;
    return low;
}

void QSort_4(SqList *L, int low, int high)
{
    int pivot;
    if((high - low) > MAX_LENGTH_INSERT_SORT)
    {
        pivot = Partition_4(L, low, high);
        QSort_4(L, low, pivot - 1);
        QSort_4(L, pivot + 1, high);
    }
    else
    {
        InsertSort(L);
    }
}

void QuickSort_4(SqList *L)
{
    QSort_4(L, 0, L->length-1);
}
    
int Partition_5(SqList *L, int low, int high)
{
    int pivotkey;
    int temp;
    int m = low + (high - low)/2;
    if(L->r[low] > L->r[high])
        swap(L, low, high);
    if(L->r[m] > L->r[high])
        swap(L, m, high);
    if(L->r[low] < L->r[m])
        swap(L, m, low);

    temp = pivotkey = L->r[low];
    
    while(low < high)
    {
        while( low < high && L->r[high] >= pivotkey)
            high--;
        L->r[low] = L->r[high];
        while(low < high && L->r[low] <= pivotkey)
            low++;
        L->r[high] = L->r[low];
    }
    L->r[low] = temp;
    return low;
}

void QSort_5(SqList *L, int low, int high)
{
    int pivot;
    if((high - low) > MAX_LENGTH_INSERT_SORT)
    {
        while(low < high)
        {
            pivot = Partition_5(L, low, high);
            QSort_5(L, low, pivot - 1);
            low = pivot + 1;
        }
    }
    else
    {
        InsertSort(L);
    }
}

void QuickSort_5(SqList *L)
{
    QSort_5(L, 0, L->length - 1);
}

3. main函数
#include "sorts.h"

int main()
{
    clock_t t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
    clock_t t11, t12, t13, t14, t15, t16, t17, t18;
    clock_t t19, t20, t21, t22, t23, t24, t25, t26;
    srand(time(0));
    printf("*********************************\n");
    SqList L1;
    CreateSqList(&L1);
//    PrintSqList(&L1);
    t1 = clock();    
    BubbleSort_1(&L1);
    t2 = clock();
    PrintSqList(&L1);

    printf("*********************************\n");
    SqList L2;
    CreateSqList(&L2);
//    PrintSqList(&L2);
    t3 = clock();
    BubbleSort_2(&L2);
    t4 = clock();
    PrintSqList(&L2);

    printf("*********************************\n");
    SqList L3;
    CreateSqList(&L3);
//    PrintSqList(&L3);
    t5 = clock();
    BubbleSort_3(&L3);
    t6 = clock();
    PrintSqList(&L3);

    printf("*********************************\n");
    SqList L4;
    CreateSqList(&L4);
//    PrintSqList(&L4);
    t7 = clock();
    SelectSort(&L4);
    t8 = clock();
    PrintSqList(&L4);
    
    printf("*********************************\n");
    SqList L5;
    CreateSqList(&L5);
//    PrintSqList(&L5);
    t9 = clock();
    InsertSort(&L5);
    t10 = clock();
    PrintSqList(&L5);    

    printf("*********************************\n");
    SqList L6;
    CreateSqList(&L6);
//    PrintSqList(&L6);
    t11 = clock();
    ShellSort(&L6);
    t12 = clock();
    PrintSqList(&L6);

    printf("*********************************\n");
    SqList L7;
    CreateSqList(&L7);
//    PrintSqList(&L7);
    t13 = clock();
    HeapSort(&L7);
    t14 = clock();
    PrintSqList(&L7);

    printf("*********************************\n");
    SqList L8;
    CreateSqList(&L8);
//    PrintSqList(&L8);
    t15 = clock();
    MergeSort_1(&L8);
    t16 = clock();
    PrintSqList(&L8);

    printf("*********************************\n");
    SqList L9;
    CreateSqList(&L9);
//    PrintSqList(&L9);
    t17 = clock();
    QuickSort_1(&L9);
    t18 = clock();
    PrintSqList(&L9);

    printf("*********************************\n");
    SqList L10;
    CreateSqList(&L10);
//    PrintSqList(&L10);
    t19 = clock();
    QuickSort_2(&L10);
    t20 = clock();
    PrintSqList(&L10);

    printf("********************************\n");
    SqList L11;
    CreateSqList(&L11);
//    PrintSqList(&L11);
    t21 = clock();
    QuickSort_3(&L11);
    t22 = clock();
    PrintSqList(&L11);

    printf("********************************\n");
    SqList L12;
    CreateSqList(&L12);
//    PrintSqList(&L12);
    t23 = clock();
    QuickSort_4(&L12);
    t24 = clock();
    PrintSqList(&L12);

    printf("********************************\n");
    SqList L13;
    CreateSqList(&L13);
//    PrintSqList(&L13);
    t25 = clock();
    QuickSort_5(&L13);
    t26 = clock();
    PrintSqList(&L13);

    
    printf("Time of BubbleSort_1: %f\n", (double)(t2 - t1)/CLOCKS_PER_SEC);
    printf("Time of BubbleSort_2: %f\n", (double)(t4 - t3)/CLOCKS_PER_SEC);
    printf("Time of BubbleSort_3: %f\n", (double)(t6 - t5)/CLOCKS_PER_SEC);
    printf("Time of SelectSort  : %f\n", (double)(t8 - t7)/CLOCKS_PER_SEC);
    printf("Time of InsertSort  : %f\n", (double)(t10 - t9)/CLOCKS_PER_SEC);
    printf("Time of ShellSort   : %f\n", (double)(t12 - t11)/CLOCKS_PER_SEC);
    printf("Time of HeapSort    : %f\n", (double)(t14 - t13)/CLOCKS_PER_SEC);
    printf("Time of MergeSort_1 : %f\n", (double)(t16 - t15)/CLOCKS_PER_SEC);

    printf("Time of QuickSort_1 : %f\n", (double)(t18 - t17)/CLOCKS_PER_SEC);
    printf("Time of QuickSort_2 : %f\n", (double)(t20 - t19)/CLOCKS_PER_SEC);
    printf("Time of QuickSort_3 : %f\n", (double)(t22 - t21)/CLOCKS_PER_SEC);
    printf("Time of QuickSort_4 : %f\n", (double)(t24 - t23)/CLOCKS_PER_SEC);
    printf("Time of QuickSort_5 : %f\n", (double)(t26 - t25)/CLOCKS_PER_SEC);
    
    return 0;
}

4. 由于每个人到点电脑上面内存不一样,如果每一个被排序到数组创建时元素个数过大,可能回导致段错误,请大家根据自己到情况进行调整(#define MAXSIZE 1000)。下面是为到测试结果:





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值