少无 2021-05-23 14:41 采纳率: 0%
浏览 70

求大佬看看这个数据结构,孩子刚开始学,搞不懂都

本题要求实现基于顺序表的直接选择排序算法,要求打印出每一趟的排序结果。顺序表的结构体定义如下

typedef int DataType;                                
#define LISTSIZE 100                                
typedef struct
{
    DataType list[LISTSIZE];
    int length;
}SqList;

函数接口定义:

void SelectSort(SqList *L)

L 是用户传入的参数,代表待排序的顺序表,也是排序后返回的顺序表。要求打印出每一趟的排序结果。

裁判测试程序样例:

#include <stdio.h>

int InitList(SqList *L);                      /* 初始化顺序表 */
int ListInsert(SqList *L, int pos, DataType item);    /* 插入结点,细节在此不表 */
int TraverList(SqList L);                              /* 打印顺序表,细节在此不表,可直接调用 */ 
void swap(SqList *L, int i, int j)               /* 交换顺序表里下标为i和j的两个结点 */
{
    DataType temp = L->list[i];
    L->list[i] = L->list[j];
    L->list[j] = temp;
}

void SelectSort(SqList *L);                   /* 本题要求函数 */

int main()
{
    SqList L;
    DataType x;
    char ch;
    int pos = 1;
    InitList(&L);
    do
    {  
        scanf("%d",&x);
        ListInsert( &L , pos++ , x );        
    }while ((ch=getchar())!='\n');
    SelectSort(&L);
    printf("The sorted List is\n");
    TraverList(L);
    return 0;
}

输入样例:

23 45 12 20 31

输出样例:

12 45 23 20 31
12 20 23 45 31
12 20 23 45 31
12 20 23 31 45
The sorted List is
12 20 23 31 45
  • 写回答

1条回答 默认 最新

  • DAY Ⅰ 2023-05-12 10:27
    关注

    你看一下~

    #include <iostream>
    using namespace std;
    
    typedef int DataType;
    #define LISTSIZE 100                                
    typedef struct
    {
        DataType list[LISTSIZE];
        int length;
    }SqList;
    
    int InitList(SqList* L)
    {
        L->length = 0;
        return 1;
    }
    
    int ListInsert(SqList* L, int pos, DataType item)
    {
        if (pos<1 || pos>L->length + 1)
            return 0;
        if (L->length >= LISTSIZE)
            return 0;
        for (int i = L->length; i >= pos; i--)
        {
            L->list[i] = L->list[i - 1];
        }
        L->list[pos - 1] = item;
        L->length++;
        return 1;
    }
    
    int TraverList(SqList L)
    {
        for (int i = 0; i < L.length; i++)
        {
            printf("%d ", L.list[i]);
        }
        printf("\n");
        return 1;
    }
    
    void swap(SqList* L, int i, int j)
    {
        DataType temp = L->list[i];
        L->list[i] = L->list[j];
        L->list[j] = temp;
    }
    
    void SelectSort(SqList* L)
    {
        int i, j, min;
        for (i = 0; i < L->length - 1; i++)
        {
            min = i;
            for (j = i + 1; j < L->length; j++)
            {
                if (L->list[j] < L->list[min])
                {
                    min = j;
                }
            }
            if (min != i)
            {
                swap(L, min, i);
            }
        }
    }
    
    int main()
    {
        SqList L;
        DataType x;
        char ch;
        int pos = 1;
        InitList(&L);
        do
        {
            scanf_s("%d", &x);
            ListInsert(&L, pos++, x);
        } while ((ch = getchar()) != '\n');
        SelectSort(&L);
        printf("The sorted List is");
            TraverList(L);
        return 0;
    }
    
    评论

报告相同问题?