﻿ C# Sharp Exercises: Sort elements of array in ascending order - w3resource

# C# Sharp Exercises: Sort elements of array in ascending order

## C# Sharp Array: Exercise-11 with Solution

Write a program in C# Sharp to 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];
int n, i, j, tmp;

Console.Write("\n\nSort elements of array in ascending order :\n");
Console.Write("----------------------------------------------\n");

Console.Write("Input the size of array : ");

Console.Write("Input {0} elements in the array :\n",n);
for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
}

for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] < arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Console.Write("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
Console.Write("{0}  ", arr1[i]);
}
Console.Write("\n\n");
}

}
```
```

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:

C# Sharp Code Editor: