线性表的顺序表示和实现:sortAscList

//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e改写L中第i个数据元素的值
Status SetElem(SqList L,int i,ElemType e){
    if(i<1 || i>ListLength(L)) return ERROR;
    L.elem[i-1] = e;
    return OK;
}

//将链表按升序进行排序
void sortAscList(SqList L){
    ElemType e,f,min;
    int min_pos;
    int len = L.length;
    for(int i=1;i<=len;i++){
        GetElem(L,i,&e);
        min=e;
        for(int j=i+1;j<=len;j++){
            //printf("%d\n",j);
            GetElem(L,j,&f);
            if(min>f){
                min=f;
                min_pos=j;
            }
        }

        if(min!=e){
            SetElem(L,i,min);
            SetElem(L,min_pos,e);
        }

    }
}
发布了14 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lzdelphi/article/details/104321668