Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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]);
                }
        }
    }
}
Posted

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]);
            }
        }
    }
 
Share this answer
 
v2
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[^]).
 
Share this answer
 
Look here[^] and use the Array.Sort method for your goal.
 
Share this answer
 
you can use the ArrayList to sort the values inside it. Check the below code for sorting the values in array.

C#
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)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\n\t\tAfter Sorting Of Array.");
            Console.ResetColor();
            ArrayList arr = new ArrayList();
            for (int x = 0; x < (int)array.size; x++)
            {
                arr.Add(a_1[x]);
            }
            arr.Sort();
            for (int x = 0; x < (int)array.size; x++)
            {
                a_1[x] = (int)arr[x];
            }
            for (int x = 0; x < (int)array.size; x++)
            {
                Console.WriteLine(a_1[x]);
            }
        }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900