C#中的简单数组排序问题 [英] Simple Array Sorting Problem In C#

查看:67
本文介绍了C#中的简单数组排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI!

这是简单的程序,它在C#的阵列中输入元素。但是在执行此程序之后它只是输入并且没有在排序表格中显示任何输出。



请告诉我有关问题的信息。



//程序

HI!
This Is The Simple Program Which Sort Enter Element In aArray Of C#.But After Execute this program It Just Take Input and Did Not Show me any output in Sorting Form.

Please Tell Me About the Problem.

//Program

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

namespace Practice_Csharp_2
{
    enum array:int
    {
        size=5,
        size_1 =2,
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            //Declare Array Of Integer
            int[] a_1=new int[(int)array.size];
            Console.WriteLine("\n\t\tThis is a program for All Basic Array Uses.");
            for (x = 0; x < (int)array.size; x++)
            {
                    Console.WriteLine("\nEnter Value In Array [{0}] Index.", x+1);
                    a_1[x] = Convert.ToInt32(Console.ReadLine());
                }
                //Calling Function For Sorting
            sort(a_1);
            Console.ReadLine();

        }
        static void sort(int[] a_1)
        {
            int x, temp=0;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\n\t\tAfter Sorting Of Array.");
            Console.ResetColor();
            for (x = 0; x < (int)array.size; x++)
            {
                    if (a_1[x]> a_1[x+1])
                    {
                        temp = a_1[x];
                        a_1[x] = a_1[ x + 1];
                        a_1[ x + 1] = temp;

                    }
            }
            for (x = 0; x < (int)array.size; x++)
            {
                    Console.WriteLine(a_1[x]);
                }
        }
    }
}

推荐答案

通过一个简单的循环数组你也可以做排序。

检查下面的工作代码



By The simple loop of an array you can also do sorting.
check the below working code

class Program
    {
        enum array : int
        {
            size = 5,
            size_1 = 2,
        }
        static void Main(string[] args)
        {
            int x;
            //Declare Array Of Integer
            int[] a_1 = new int[(int)array.size];
            Console.WriteLine("\n\t\tThis is a program for All Basic Array Uses.");
            for (x = 0; x < (int)array.size; x++)
            {
                Console.WriteLine("\nEnter Value In Array [{0}] Index.", x + 1);
                a_1[x] = Convert.ToInt32(Console.ReadLine());
            }
            //Calling Function For Sorting
            sort(a_1);
            Console.ReadLine();

        }

        
        static void sort(int[] a_1)
        {
            int x, temp = 0;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\n\t\tAfter Sorting Of Array.");
            Console.ResetColor();
            for (x = 0; x < (int)array.size; x++)
            {                
                for (int y = 0; y < (int)array.size; y++)
                {
                    if (a_1[x] < a_1[y])
                    {
                        temp = a_1[x];
                        a_1[x] = a_1[y];
                        a_1[y] = temp;

                    }
                }
            }
            for (x = 0; x < (int)array.size; x++)
            {
                Console.WriteLine(a_1[x]);
            }
        }
    }


sort function doesn不对数组进行排序:如果它们的顺序不正确,它只会交换两个相邻的项目。

这只是排序过程中的一个步骤,你必须在另一个循环中重复它(那将是一个冒泡排序实现,请参阅其维基百科页面 [ ^ ])。
You sort function doesn't sort the array: in its loop it simply exchanges two adjacent items if they are not in the correct order.
This is just a step in the sorting process, you have to repeat it inside another loop (that would be then a bubble sort implementation, see its Wikipedia page[^]).


look 这里 [ ^ ]并使用 Array.Sort 方法作为目标。
Look here[^] and use the Array.Sort method for your goal.


这篇关于C#中的简单数组排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆