【数据结构】C# 面向对象思想完成三种排序

本文详细介绍了三种常见的排序算法:选择排序、冒泡排序和插入排序。通过具体的代码实现展示了每种算法的工作原理,并提供了用于测试排序效果的方法。文章还提供了一个交互式的菜单系统,允许用户输入数据并选择不同的排序算法来观察其效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sort类 主要逻辑

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

namespace Selection
{
    public class sort
    {
        /**
         *selection sort 原理
         * 默认升序排列
         * 由循环找出最小值 
         * 与冒泡排序不同的是--》记录下最小值的下标 而不是每比较一次交换一次
         * 然后把此次循环的最小值放到数组前面
         */
        public int[] selectionsort(int[] a, int n)
        {
            for (int j = 0; j < n - 1; j++)
            {
                int min_index = j;//循环令最小值的下标为j
                for (int i = j; i < n; i++)
                {
                    if (a[i] < a[min_index])
                    {
                        min_index = i;//记录下最小值的下标
                    }
                }
                int temp = a[j];
                a[j] = a[min_index];
                a[min_index] = temp;
                Display(a, n);
            }
            return a;
        }
        /**
         *冒泡排序
         * 双重循环 比较
         */
        public int[] bubblesort(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = i; j < n; j++)
                {
                    if (a[i] > a[j])
                    {
                        int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
                Display(a, n);
            }
            return a;
        }

        //插入排序
        public int[] insertionsort(int[] a, int n)
        {
            for (int i = 1; i < n; i++)
            {
                int j = i - 1;
                //                while 中条件的顺讯有先后
                //                当j不满足就不进行a[j] 与 a[j + 1]的判断了
                //                否则会越界....
                while (j >= 0 && a[j] >= a[j + 1])
                {

                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    j--;
                }
                Display(a, n);
            }
            return a;
        }
        public void inputdata(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("please input the data u want to input!");
                a[i] = Int32.Parse(Console.ReadLine());
            }
        }
        //打印
        public void print(int[] a, int n)
        {
            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("第" + (i+1) + "个数为:" + a[i]);
            }
        }
        //表现排序过程的打印
        public void Display(int[] a, int n)
        {
            Console.WriteLine("\n排序过程 : ");
            for (int i = 0; i < n; i++)
            {
                Console.Write(a[i] + " ");
            }
        }
        //操作菜单
        public void show()
        {
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
            Console.WriteLine("1.selectionsort");
            Console.WriteLine("2.bubblesort");
            Console.WriteLine("3.insertionsort");
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
        }
    }
    
}


test类 测试

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

namespace Selection
{
    class Test
    {

        static void Main(string[] args)
        {
            Console.WriteLine("please input the array size");
            int n = Int32.Parse(Console.ReadLine());
            int[] a = new int[n];
            //储存
            int[] arr = new int[20];
            int length = n;

            sort test = new sort();

            test.inputdata(a, n);
            Array.Copy(a, 0, arr, 0, length);

            char ch;
            do
            {
                Console.Clear();
                int key;
                test.show();
                Console.WriteLine("请输入您要进行的操作");
                key = Int32.Parse(Console.ReadLine());
                switch (key)
                {
                    case 1:
                        a = test.selectionsort(a, n);
                        break;
                    case 2:
                        a = test.bubblesort(a, n);
                        break;
                    case 3:
                        a = test.insertionsort(a, n);
                        break;
                    default: break;
                }
                test.print(a, n);
         //       Console.WriteLine("数组未排序前为:");
         //       test.print(arr, n);
                a = arr;
                Console.WriteLine("您想要继续吗?Y/y");
                ch = Char.Parse(Console.ReadLine());
            } while ('y' == ch || 'Y' == ch);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值