C语言:选择排序法对指针list所指数组进行升序排列。

题目:

输入整数n(1≤n≤10)和n个整数存入数组,要求定义一个函数voidsort(int *list,int n),使用选择排序法对指针list所指数组进行升序排列,n为list所指向数组的元素个数。在main函数中调用sort函数实现排序,最后输出排序后的结果。

选择排序法:

选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。

选择排序法代码传送门:https://blog.csdn.net/lbcbjtlhmjq/article/details/128741011

代码:

#include<stdio.h>
#define N 10
void swap(int* a, int* b)//两数交换函数
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void sort(int* list, int n)//选择排序函数
{
    int j, i,min,max;
    for (j = 0; j < n / 2; j++)
    {
        min = j;
        max = n - j - 1;
        for (i = j; i < n - j; i++)//筛选出最大值与最小值
        {
            if (list[i] < list[min])
                min = i;
            if (list[i] > list[max])
                max = i;
        }
        swap(&list[j], &list[min]);
        if (max == j)//考虑最大值位置与未排序数组最左端重合的情况
            swap(&list[n - j - 1], &list[min]);
        else
            swap(&list[n - j - 1], &list[max]);
    }
}
int main()
{
    int n,i;
    int list[N]={ };
    printf("输入整数n(1≤n≤10):");
    scanf("%d", &n);
    printf("输入n个整数:");
    for (i = 0; i < n; i++)
        scanf("%d", &list[i]);
    sort(list, n);//调用函数
    for (i = 0; i < n; i++)
        printf("%d ", list[i]);
    return 0;
}

运行结果:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言单链表选择排序的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct node { int data; struct node *next; } Node; // 创建节点 Node *create_node(int data) { Node *node = (Node *)malloc(sizeof(Node)); node->data = data; node->next = NULL; return node; } // 插入节点 void insert_node(Node **head, Node *node) { if (*head == NULL) { *head = node; } else { Node *temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = node; } } // 选择排序 void selection_sort(Node **head) { Node *current = *head; while (current != NULL) { Node *min = current; Node *temp = current->next; while (temp != NULL) { if (temp->data < min->data) { min = temp; } temp = temp->next; } // 交换节点数据 int temp_data = current->data; current->data = min->data; min->data = temp_data; current = current->next; } } // 打印链表 void print_list(Node *head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { Node *head = NULL; insert_node(&head, create_node(5)); insert_node(&head, create_node(2)); insert_node(&head, create_node(8)); insert_node(&head, create_node(3)); insert_node(&head, create_node(9)); printf("排序前的链表:\n"); print_list(head); selection_sort(&head); printf("排序后的链表:\n"); print_list(head); return 0; } ``` 在上面的代码中,我们定义了链表节点结构体 Node,并创建了 create_node() 函数来创建节点,insert_node() 函数来插入节点,print_list() 函数来打印链表。同时,我们实现了 selection_sort() 函数来对链表进行选择排序。在 selection_sort() 函数中,我们使用两个指针 current 和 min 来遍历链表,并找到链表中的最小节点,然后交换节点数据。最后,我们在 main() 函数中进行测试,输出排序前和排序后的链表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值