关于数据结构:简单选择排序

9次阅读

共计 612 个字符,预计需要花费 2 分钟才能阅读完成。

#include <iostream>
using namespace std;
#define  MAXSIZE  20                              
typedef struct{
    int key;
    char *otherinfo;
}ElemType;                      
typedef struct{
    ElemType *r;                                     
    int  length;                                
}SqList;
// 简略抉择排序                                        
void SelectSort(SqList &L) { 
    int i,j,k;
    ElemType t;
    for(i=1;i<L.length;++i){                                              
        k=i;                 
        for(j=i+1;j<=L.length;++j)
            if(L.r[j].key<L.r[k].key)  k=j;            
        if(k!=i) {t=L.r[i];L.r[i]=L.r[k];L.r[k]=t;}    
     }                                                
}                                                                            
void Create_Sq(SqList &L){
    int i,n;
    cout<<"数据个数:";
    cin>>n;                                            
    cout<<"待排序的数据:";
    for(i=1;i<=n;i++){cin>>L.r[i].key;
        L.length++;
    }
}
void show(SqList L){
    int i;
    for(i=1;i<=L.length;i++)
        cout<<L.r[i].key<<" ";
}
int main(){
    SqList L;
    L.r=new ElemType[MAXSIZE+1];
    L.length=0;
    Create_Sq(L);
    SelectSort(L);
    cout<<"简略抉择排序:";
    show(L);
}

正文完
 0