C# Sharp Exercises: Sort elements of the array in descending order
C# Sharp Array: Exercise-12 with Solution
Write a program in C# Sharp to sort elements of the array in descending order.
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise12
{
public static void Main()
{
int[] arr1 = new int[10];
int n, i, j, tmp;
Console.Write("\n\nsort elements of array in descending order :\n");
Console.Write("----------------------------------------------\n");
Console.Write("Input the size of array : ");
n = Convert.ToInt32(Console.ReadLine());
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());
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[i] < arr1[j])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Console.Write("\nElements of the array in sorted descending order:\n");
for(i=0; i<n; i++)
{
Console.Write("{0} ", arr1[i]);
}
Console.Write("\n\n");
}
}
Sample Output:
sort elements of array in descending order : ---------------------------------------------- Input the size of array : 3 Input 3 elements in the array : element - 0 : 2 element - 1 : 5 element - 2 : 0 Elements of the array in sorted descending order: 5 2 0
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to sort elements of array in ascending order.
Next: Write a program in C# Sharp to insert New value in the array (sorted list ).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework