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 ;

    }
    }

  • 相关阅读:
    SD卡测试
    测试人员可能会遇到的问题
    HDU 1024 Max Sum Plus Plus
    HDU 1176 免费馅饼
    HDU 1257 最少拦截系统
    HDU 1087 Super Jumping! Jumping! Jumping!
    poj 1328 Radar Installation
    poj 1753 Flip Game
    HDU 1003 Max Sum
    HDU 5592 ZYB's Premutation(BestCoder Round #65 C)
  • 原文地址:https://www.cnblogs.com/wbtn6262/p/3795052.html
Copyright © 2011-2022 走看看