C# program to implement selection Sort to arrange elements in the descending order

Here, we are going to learn how to implement selection Sort to arrange elements in the descending order in C#?
Submitted by Nidhi, on November 06, 2020

Here, we will sort an integer array using selection sort in the descending order.

Program:

The source code to implement selection sort to arrange elements in the descending order is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement selection Sort to arrange elements 
//in the descending order

using System;

class Sort
{
    static void SelectionSort(ref int []intArr)
    {
        int temp=0;
        int max =0;

        int i = 0;
        int j = 0;

        for (i = 0; i < intArr.Length - 1; i++)
        {
            max = i;

            for (j = i + 1; j < intArr.Length; j++)
            {
                if (intArr[j] > intArr[max])
                {
                    max = j;
                }
            }
            temp = intArr[max];
            intArr[max] = intArr[i];
            intArr[i] = temp;
        }
    }
    static void Main(string[] args)
    {
        int[] intArry = new int[5] { 65,34,23,76,21 };

        Console.WriteLine("Array before sorting: ");
        for (int i = 0; i < intArry.Length; i++)
        {
            Console.Write(intArry[i]+" ");
        }
        Console.WriteLine();

        SelectionSort(ref intArry);
        
        Console.WriteLine("Array before sorting: ");
        for (int i = 0; i < intArry.Length; i++)
        {
            Console.Write(intArry[i] + " ");
        }
        Console.WriteLine();
    }
}

Output:

Array before sorting:
65 34 23 76 21
Array before sorting:
76 65 34 23 21
Press any key to continue . . .

Explanation:

In the above program, we created a class Sort that contains two static methods SelectionSort() and Main(). The SelectionSort() method is used to sort the elements of integer array in the descending order.

Here, in every iteration, the largest element is swapped to the current location. After completing the all iterations array will be sorted in descending order.

Now look to the Main() method, The Main() method is the entry point for the program. Here, we created the array of integers then sorted the array in descending order using the SelectionSort() method and print the sorted array on the console screen.

C# Data Structure Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.