码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法05-选择排序(用C++、C#、lua实现)

时间:2019-11-08 22:32:36      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:void   red   out   排序算法   sele   排序   ==   font   number   




本文为排序算法-选择排序的代码实现。
作者水平比较差,有错误的地方请见谅。

1、选择排序

简单选择排序:每次选择一个最小的值放到前面。

2、C#实现

SelectSort.cs

public static class SelectSort
    {
        public static void Select(int[] numbers)
        {
            if (numbers == null || numbers.Length < 2)
            {
                Console.WriteLine("参数数组有误");
                return;
            }

            //寻找length-1次最小值
            for (int i = 0; i < numbers.Length-1; i++)
            {
                int minIndex = i;
                int minNum = numbers[i];
                //每次找到最小值放到i的位置
                for (int j = i; j < numbers.Length; j++)
                {
                    if (numbers[j] < minNum)
                    {
                        minIndex = j;
                        minNum = numbers[j];
                    }
                }
                //i位置值和每次找到的最小值交换
                if (minIndex != i)
                {
                    int temp = numbers[i];
                    numbers[i] = numbers[minIndex];
                    numbers[minIndex] = temp;
                }
            }
        }
    }

Program.cs

class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 49, 38, 65, 97, 76, 13, 27, 49 };
            SelectSort.Select(numbers);

            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write(numbers[i] + " ");
            }
            Console.WriteLine();

            Console.ReadKey();
        }
    }

3、C++实现

SelectSort.cpp

///选择排序
class SelectSort
{
public:
    static void Select(int numbers[],int length);
};

void SelectSort::Select(int numbers[],int length)
{
    if (numbers == NULL || length < 2)
    {
        cout<<"参数数组有误"<<endl;
        return;
    }

    for (int i = 0; i < length-1; i++)
    {
        int minIndex = i;
        int minNum = numbers[i];
        for (int j = i; j < length; j++)
        {
            if (numbers[j] < minNum)
            {
                minIndex = j;
                minNum = numbers[j];
            }
        }
        if (minIndex != i)
        {
            int temp = numbers[i];
            numbers[i] = numbers[minIndex];
            numbers[minIndex] = temp;
        }
    }
}

main.cpp

    int numbers[] = {49,38,65,97,76,13,27,49};
    int length = sizeof(numbers)/sizeof(numbers[0]);

    SelectSort::Select(numbers,length);

    for(int i=0;i<length;i++){
        cout<<numbers[i]<<" ";
    }
    cout<<endl;

4、lua实现

numbers = {49,38,65,97,76,13,27,49}
length = #numbers


function SelectSort(nums)
    local length = #nums
    if (nums == nil or length < 2) then
        print("参数数组有误")
        return
    end

    for i=1,length-1,1 do
        local minIndex = i
        local minNum = numbers[i]
        for j=i,length,1 do
            if (numbers[j] < minNum) then
                minIndex = j
                minNum = numbers[j]
            end
        end
        
        if (minIndex ~= i) then
            local temp = numbers[i]
            numbers[i] = numbers[minIndex]
            numbers[minIndex] = temp
        end
    end
end




SelectSort(numbers)

for i=1,length,1 do
    io.write(numbers[i] .. ' ')
end
print()

5、新知识和疑问

排序算法05-选择排序(用C++、C#、lua实现)

标签:void   red   out   排序算法   sele   排序   ==   font   number   

原文地址:https://www.cnblogs.com/Fflyqaq/p/11823641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!