陈越姥姥数据结构-选择排序

/**
 * @fileName:      main.c 
 * @author:        QWQ534 
 * @date:          2023-12-07 10:47:40
 * @description:   Selection Sorting Algorithm
 **/

#include <stdio.h>

//#define ElementType int                                           //Can't be set as float or double
typedef int ElementType;                                            //Best to use a typedef to create a new type name

#define Swap(a, b) \
        a ^= b;    \
        b ^= a;    \
        a ^= b

int ScanForMin( ElementType List[], int i, int N );
void SelectionSort( ElementType List[], int N );

int main(void)
{
    ElementType List[10] = { 1, 3, 5, 3, 9, 7, 0, 11, 10, 4 };

    SelectionSort(List, 10);

    for(int i = 0; i < 10; i++)
    {
        printf("%d\t", List[i]);
    }

    return 0;
}

/**
 * @brief  This function 
 * @param  ElementType List[] 传入的待排序的数组
 *         i 从第几个数字开始扫描
 *         N 待排序数字总数,也表示循环次数
 * @retval result 此次扫描中找到的未排序数列中的最小的数字的下标
 **/
int ScanForMin( ElementType List[], int i, int N )
{
    int result = i;
    int j;

    for(j = i; j < N; j++)
    {
        if(List[result] > List[j])
        {
            result = j;
        }
    }

    return result;
}

/**
 * @brief  This function 
 * @param  ElementType List[] 传入的待排序的数组
 *         N 待排序数字总数
 * @retval None
 **/
void SelectionSort( ElementType List[], int N )
{
    int i;
    int MinPosition = 0;
    for(i = 0; i < N; i++)
    {
        MinPosition = ScanForMin( List, i, N );
        if(i != MinPosition)
        {
            Swap(List[i], List[MinPosition]);
        }
    }
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值