C++ 简单选择排序

#include
using namespace std;
#include
#include
typedef int KeyType;
typedef char * InfoType;
typedef struct
{
    KeyType key;
    InfoType otherinfo;
}ElemType;
typedef struct
{
    ElemType *R;
    int length;
}SqList;

void CreateList(SqList &L,int n)
{
    int i;
    L.R=new ElemType[n+1];
    srand(time(0));
    for(i=1;i<=n;i++)
    {
        L.R[i].key=rand()%100;    
    }
    L.length=n;
}

void ListTraverse(SqList L)
{
    int i;
    for(i=1;i<=L.length;i++)
        cout<    cout<}

void SelectSort(SqList &L)
{
    int i,j,k;
    ElemType temp;
    for(i=1;i       k=i;
       for(j=i+1;j<=L.length;j++)
          if(L.R[j].key        if(k!=i)
        {//交换R[i]与R[k]
            temp=L.R[i];
            L.R[i]=L.R[k];
            L.R[k]=temp;
        }
    }
}

int main()
{
    SqList L;
    CreateList(L,8);
    cout<<"测试数据:"<    ListTraverse(L);
    SelectSort(L);
    cout<<"排序后:"<    ListTraverse(L);
    return 0;
}

转载于:https://www.cnblogs.com/YY-Xcode/p/4973930.html

你可能感兴趣的:(C++ 简单选择排序)