C语言-中级阶段-17考研必会的排序算法(下)

一、选择排序原理解析及代码实战

1.简单选择排序 

 

* 代码实战:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef int ElemType;
typedef struct{
    ElemType *elem;
    int TableLen;
}SSTable;

void ST_Init(SSTable &ST,int len)//申请空间,并进行随机数生成
{
    ST.TableLen=len;
    ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
    int i;
    srand(time(NULL));
    for(i=0;i<ST.TableLen;i++)
    {
        ST.elem[i]=rand()%100;
    }
}
void ST_print(SSTable ST)
{
    for(int i=0;i<ST.TableLen;i++)
    {
        printf("%3d",ST.elem[i]);
    }
    printf("\n");
}
void swap(ElemType &a,ElemType &b)
{
    ElemType tmp;
    tmp=a;
    a=b;
    b=tmp;
}

void SelectSort(ElemType *A,ElemType count)
{
    ElemType i,j,min;
    for(i=0;i<count-1;i++)
    {
        min=i;
        for(j=i+1;j<count;j++)
        {
            if(A[min]>A[j])
            {
                min=j;
            }
        }
        if(min!=i)
        {
            swap(A[min],A[i]);
        }
    }
}

int main() {
    SSTable ST;
    ST_Init(ST,10);
    ST_print(ST);
    SelectSort(ST.elem,10);
    ST_print(ST);
    return 0;
}

 2.堆排序

 注意:这里的堆是用数组去表示二叉树的一种结构,而且表示的一定是完全二叉树

* 堆排序实战:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef int ElemType;
typedef struct{
    ElemType *elem;
    int TableLen;
}SSTable;

void ST_Init(SSTable &ST,int len)//申请空间,并进行随机数生成
{
    ST.TableLen=len;
    ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
    int i;
    srand(time(NULL));
    for(i=0;i<ST.TableLen;i++)
    {
        ST.elem[i]=rand()%100;
    }
}
void ST_print(SSTable ST)
{
    for(int i=0;i<ST.TableLen;i++)
    {
        printf("%3d",ST.elem[i]);
    }
    printf("\n");
}
void swap(ElemType &a,ElemType &b)
{
    ElemType tmp;
    tmp=a;
    a=b;
    b=tmp;
}

void Adjust(ElemType A[],ElemType k,ElemType len)
{
    static ElemType s=len;
    ElemType parent,son;
    parent=(s/2)-k;
    son=2*parent+1;
    while(son<len)
    {
        if(son+1<len&&A[son]<A[son+1])
        {
            son++;
        }
        if(A[parent]<A[son])
        {
            swap(A[parent],A[son]);
            parent=son;
            son=2*parent+1;
        }else{
            break;
        }
    }
}

void HeapSort1(ElemType A[], int len)
{
    int i;
    for(i=1;i<=len/2;i++)
    {
        Adjust(A,i,len);
    }
    swap(A[0],A[len-1]);
    for(i=len-1;i>1;i--)
    {
        Adjust(A,len/2,i);
        swap(A[0],A[i-1]);
    }
}

int main() {
    SSTable ST;
    ST_Init(ST,10);//初始化
    ElemType A[10] = { 3, 87, 2, 93, 78, 56, 61, 38, 12, 40 };
    memcpy(ST.elem,A,sizeof(A));
    ST_print(ST);
    //HeapSort(ST.elem, 9);//王道书零号元素不参与排序,考研考的都是零号元素要参与排序
    HeapSort1(ST.elem,10);//所有元素参与排序
    ST_print(ST);
    return 0;
}

 二、归并排序原理解析及实战

* 归并排序代码实战:

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

#define N 7
typedef int ElemType;

void print(int* a)
{
    for(int i=0;i<N;i++)
    {
        printf("%3d",a[i]);
    }
    printf("\n");
}

void Merge(int A[],int low,int mid,int high)
{
    static ElemType B[N];
    int i,j,k;
    for(i=low;i<=high;i++)
    {
       B[i]=A[i];
    }
    k=low;
    for(i=low,j=mid+1;i<=mid && j<=high;)
    {
        if(B[i]<B[j])
        {
            A[k]=B[i];
            i++;
            k++;
        }else{
            A[k]=B[j];
            j++;
            k++;
        }
    }
    while(i<=mid)
    {
        A[k]=B[i];
        i++;
        k++;
    }
    while(j<=high)
    {
        A[k]=B[j];
        j++;
        k++;
    }
}

void MergeSort(int A[],int low,int high)
{
    if(low<high)
    {
        int mid=(low+high)/2;
        MergeSort(A,low,mid);
        MergeSort(A,mid+1,high);
        Merge(A,low,mid,high);
    }
}

int main() {
    int A[N]={49,38,65,97,76,13,27};
    MergeSort(A,0,6);
    print(A);
    return 0;
}

 三、所有排序算法时间和空间复杂度汇总(重要)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值