zoukankan      html  css  js  c++  java
  • 数据结构--内排序

    直接插入排序

    #include <STDIO.H>
    typedef int Keytype;
    typedef struct
    {
    Keytype key;
    int data;

     

    }RecType;
    void InsertSort(RecType R[],int n)
    {

     

    int i;
    int j;
    RecType temp;
    for(i=1;i<n;i++)
    {

     

    temp=R[i];

     

    j=i-1;
    while (j>=0&&temp.key<R[j].key)
    {
    R[j+1]=R[j];
    j--;

     

    }
    R[j+1]=temp;
    }
    }

     

     

    二分插入排序:

    void InsertSort1(RecType R[],int n)
    {

    int i,j,low,high,mid;
    RecType temp;
    for(i=1;i<n;i++)
    {

    temp=R[i];
    low=0;
    high=i-1;
    while (low<=high)
    {
    mid=(high+low)/2;
    if (temp.key<R[mid].key)
    high=mid-1;
    else
    low=mid+1;
    }
    for (j=i-1;j>=high+1;j--)
    R[j+1]=R[j];
    R[high+1]=temp;

    }

    }

    希尔排序:

    void shellsort(Sqlist R[],int n)
    {
    int i,j,gap;
    Sqlist temp;
    gap=n/2;
    while (gap>0)
    {
    for (i=gap;i<n;i++)
    {
    temp=R[i];
    j=i-gap;
    while (j>=0&&temp.data<R[j].data)
    {
    R[j+gap]=R[j];
    j=j-gap;
    }
    R[j+gap]=temp;

    }
    gap=gap/2;
    }
    }

    冒泡排序:

    void Bubblesort(RecType R[],int n)
    {

    int i,j,exchang;
    RecType temp;
    for (i=0;i<n-1;i++)
    {
    exchang=0;
    for (j=n-1;j>i;j--)

    if (R[j].key<R[j-1].key)
    {
    temp=R[j].key;
    R[j].key=R[j-1].key;
    R[j-1].key=temp;
    exchang=1;

    }
    if(exchang==0)
    return ;

    }
    }

  • 相关阅读:
    FMDB增删查改
    https相关内容
    支付宝、微信支付参考博客
    下标脚本(Swift)
    函数(swift)
    控制流(swift)
    UIView Methods
    oc js 交互
    我和Lua并非一见钟情,我们期待着日久生情(相遇篇)
    与Python Falling In Love_Python跨台阶(面向对象)
  • 原文地址:https://www.cnblogs.com/wbtn6262/p/3795052.html
Copyright © 2011-2022 走看看