C#用冒泡排序对String型二维数组进行排序

本文采用冒泡排序法对二维数组进行排序

代码内容如下

using System;

namespace experment4
{
    class Program
    {
        private static int result;
        private static int result1;

        static void Main(string[] args)
        {
            string[,] a = new String[5,2];
            a[0, 0] = "2"; a[0, 1] = "Smith";
            a[1, 0] = "4"; a[1, 1] = "John";
            a[2, 0] = "5"; a[2, 1] = "Mary";
            a[3, 0] = "1"; a[3, 1] = "Cherr";
            a[4, 0] = "3"; a[4, 1] = "Tom";
            Console.WriteLine("排序前:");
            Console.Write("学号:");
            for(int i = 0; i < 5; i++)
            {
                Console.Write(a[i, 0].ToString().PadRight(8,' '));

            }
            Console.WriteLine();
            Console.Write("姓名:");
            for(int j = 0; j < 5; j++)
            {
                Console.Write(a[j, 1].ToString().PadRight(8, ' '));
            }
            Console.WriteLine();
            //按学号排序
            Console.WriteLine("按学号排序后:");
            
            for (int i = 0; i < 5; i++)
            {


                

                for (int j = 0; j < 5 - i; j++)
                {
                    if(j!=4-i)
                     result = string.Compare(a[j , 0], a[j+1,0],true);
                    if (j!=4-i&&result >0)
                    {
                        string temp = a[j, 0];
                        a[j, 0] = a[j + 1, 0];
                        a[j + 1, 0] = temp;
                        string temp2 = a[j, 1];
                        a[j, 1] = a[j + 1, 1];
                        a[j + 1, 1] = temp2;

                    }
                }

            }
            Console.Write("学号:");
            for (int i = 0; i < 5; i++)
            {

                Console.Write(a[i, 0].ToString().PadRight(8, ' '));

            }
            Console.WriteLine();
            Console.Write("姓名:");
            for (int i = 0; i < 5; i++)
            {

                Console.Write(a[i, 1].ToString().PadRight(8, ' '));

            }
            Console.WriteLine();
            //按姓名排序
            Console.WriteLine("按姓名排序后:");
            Console.WriteLine();

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5 - i; j++)
                {
                    if (j != 4 - i)
                        result1 = string.CompareOrdinal(a[j, 1], a[j + 1, 1]);
                    if (j != 4 - i && result1 >= 1)
                    {
                        string temp = a[j, 1];
                        a[j, 1] = a[j + 1, 1];
                        a[j + 1, 1] = temp;
                        string temp2 = a[j, 0];
                        a[j, 0] = a[j + 1, 0];
                        a[j + 1, 0] = temp2;
                    }
                }

            }
            Console.Write("学号:");
            for (int i = 0; i < 5; i++)
            {

                Console.Write(a[i, 0].ToString().PadRight(8, ' '));
           
            }
            Console.WriteLine();
            Console.Write("姓名:");
            for (int i = 0; i < 5; i++)
            {

                Console.Write(a[i, 1].ToString().PadRight(8, ' '));

            }
            Console.WriteLine();

        }
        }
    }

运行结果:

在这里插入图片描述

发布了1 篇原创文章 · 获赞 3 · 访问量 46

猜你喜欢

转载自blog.csdn.net/qq_45359138/article/details/105705421