w3resource

C#: Sort elements of array in ascending order

C# Sharp Array: Exercise-11 with Solution

Write a C# Sharp program to sort elements of an array in ascending order.

C# Sharp: Sort elements of array in ascending order

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise11  
{  
    public static void Main() 
    {
        int[] arr1 = new int[10]; // Declare an array to store integers
        int n, i, j, tmp; // Declare variables for array size, counting, and temporary storage

        // Display a message prompting the user to input the size of the array
        Console.Write("\n\nSort elements of array in ascending order :\n");
        Console.Write("----------------------------------------------\n");	

        Console.Write("Input the size of array : ");
        n = Convert.ToInt32(Console.ReadLine()); // Read the size of the array entered by the user

        // Prompt the user to input 'n' elements in the array
        Console.Write("Input {0} elements in the array :\n", n);
        for (i = 0; i < n; i++)
        {
            Console.Write("element - {0} : ", i);
            arr1[i] = Convert.ToInt32(Console.ReadLine()); // Store user input in the array
        }

        // Sort the elements of the array in ascending order using nested loops and swapping if needed
        for (i = 0; i < n; i++)
        {
            for (j = i + 1; j < n; j++)
            {
                if (arr1[j] < arr1[i]) // Compare elements to arrange them in ascending order
                {
                    tmp = arr1[i]; // Swap elements if the condition is met
                    arr1[i] = arr1[j];
                    arr1[j] = tmp;
                }
            }
        }

        // Display the sorted elements of the array in ascending order
        Console.Write("\nElements of array in sorted ascending order:\n");
        for (i = 0; i < n; i++)
        {
            Console.Write("{0}  ", arr1[i]); // Print sorted elements
        }
        Console.Write("\n\n"); // Print a new line for formatting
    }
}

Sample Output:

Sort elements of array in ascending order :                                                                   
----------------------------------------------                                                                
Input the size of array : 4                                                                                   
Input 4 elements in the array :                                                                               
element - 0 : 1                                                                                               
element - 1 : 2                                                                                               
element - 2 : 3                                                                                               
element - 3 : 4                                                                                               
Elements of array in sorted ascending order:                                                                  
1  2  3  4

Flowchart:

Flowchart: Separate odd and even integers in separate arrays

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to separate odd and even integers in separate arrays.
Next: Write a program in C# Sharp to sort elements of the array in descending order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.