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

一、冒泡排序原理及实战

 

 

* 代码实战:

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

typedef int ElemType;
typedef struct{
    ElemType *elem;
    int TableLen;
}SSTable;

void InitTable(SSTable &ST,ElemType count)
{
    ST.TableLen=count;
    int i;
    srand(time(NULL));
    ST.elem=(ElemType*)malloc(sizeof(ElemType)*ST.TableLen);
    for(i=0;i<ST.TableLen;i++)
    {
        ST.elem[i]=rand() % 100;
    }
}

void PrintTable(SSTable ST)
{
    int i;
    for(i=0;i<ST.TableLen;i++)
    {
        printf("%3d",ST.elem[i]);
    }
    printf("\n");
}

void reverse(ElemType &a,ElemType &b)
{
    ElemType temp;
    temp=a;
    a=b;
    b=temp;
}

void BubbleSearch(ElemType *data,ElemType count)
{
    bool flag;
    int i,j;
    for(j=0;j<count-1;j++)
    {
        flag= false;
        for(i=count-1;i>j;i--)
        {
            if(data[i-1]>data[i])
            {
                reverse(data[i-1],data[i]);
                flag= true;
            }
        }
        if(false==flag)
        {
            return;
        }
    }
}

int main() {
    SSTable ST;
    InitTable(ST,10);
    PrintTable(ST);
    BubbleSearch(ST.elem,10);
    PrintTable(ST);
    return 0;
}

二、快速排序原理及实战

* 快速排序实战:

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

typedef int ElemType;
typedef struct{
    ElemType *elem;
    int TableLen;
}SSTable;

void InitTable(SSTable &ST,ElemType count)
{
    ST.TableLen=count;
    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 PrintTable(SSTable ST)
{
    int i;
    for(i=0;i<ST.TableLen;i++)
    {
        printf("%3d",ST.elem[i]);
    }
    printf("\n");
}

int Division(ElemType *A,ElemType low,ElemType high)
{
    ElemType temp=A[low];
    while(low<high)
    {
        while(low<high&&A[high]>=temp)
        {
            high--;
        }
        A[low]=A[high];
        while(low<high&&A[low]<=temp)
        {
            low++;
        }
        A[high]=A[low];
    }
    A[high]=temp;
    return high;
}

void QuickSearch(ElemType *A,ElemType low,ElemType high)
{
    if(low<high)
    {
        ElemType pos_return=Division(A,low,high);
        QuickSearch(A,low,pos_return-1);
        QuickSearch(A,pos_return+1,high);
    }
}

int main() {
    SSTable ST;
    InitTable(ST,10);
//    ElemType A[10]={ 64, 94, 95, 79, 69, 84, 18, 22, 12 ,78};
//    memcpy(ST.elem,A,sizeof(A));
    PrintTable(ST);
    ElemType low=0,high=ST.TableLen-1;
    QuickSearch(ST.elem,low,high);
    PrintTable(ST);
    return 0;
}

三、插入排序原理及实战

* 代码实战:

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

typedef int ElemType;
typedef struct{
    ElemType *elem;
    int TableLen;
}SSTable;

void InitTable(SSTable &ST,ElemType count)
{
    ST.TableLen=count;
    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 PrintTable(SSTable ST)
{
    int i;
    for(i=0;i<ST.TableLen;i++)
    {
        printf("%3d",ST.elem[i]);
    }
    printf("\n");
}

void InsertSort(ElemType *A,ElemType count)
{
    ElemType i,j,temp;
    for(i=1;i<count;i++)
    {
        temp=A[i];
        for(j=i-1;j>=0&&A[j]>temp;j--)
        {
            A[j+1]=A[j];
        }
        A[j+1]=temp;
    }
}

int main() {
    SSTable ST;
    InitTable(ST,10);
    PrintTable(ST);
    InsertSort(ST.elem,10);
    PrintTable(ST);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值