C#三种排序 插入排序,选择排序,冒泡排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
            int[] nums = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
            //InsertSort(intArray);
            SelectSort(nums);
            //BubbleSort(intArray);
            foreach (int item in nums)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        //插入排序
        public static void InsertSort(int[] nums)
        {
            for (int i = 1; i < nums.Length; i++)
            {
                int temp = nums[i];
                int j = i;
                while (j > 0 && nums[j - 1] > temp)
                {
                    nums[j] = nums[j - 1];
                    --j;
                }
                nums[j] = temp;
            }
        }

        //选择排序
        public static void SelectSort(int[] nums)
        {
            int min;
            for (int i = 0; i < nums.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < nums.Length; j++)
                {
                    if (nums[j] < nums[min])
                    {
                        min = j;
                    }
                    int temp = nums[min];
                    nums[min] = nums[i];
                    nums[i] = temp;
                }
            }
        }

        //冒泡排序
        public static void BubbleSort(int[] nums)
        {
            int j = 1, temp;
            bool flag = false;
            while (j < nums.Length && !flag)
            {
                flag = true;
                for (int i = 0; i < nums.Length - 1; i++)
                {
                    if (nums[i] > nums[i + 1])
                    {
                        flag = false;
                        temp = nums[i];
                        nums[i] = nums[i + 1];
                        nums[i + 1] = temp;
                    }
                }
                j++;
            }
        }
    }
}

转载于:https://www.cnblogs.com/eva_2010/articles/2045176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值