w3resource
C# Sharp Programming Exercises

C# Sharp Searching and Sorting Algorithm Exercises: Selection sort

C# Sharp Searching and Sorting Algorithm: Exercise-11 with Solution

Write a C# Sharp program to sort a list of elements using the selection sort algorithm.

The selection sort improves on the bubble sort by making only one exchange for every pass through the list.

Pictorial Presentation : Selection Sort

C sharp selection short

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Selection_Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            Selection_Sort selection = new Selection_Sort(10);
            selection.Sort();
        }
    }

    class Selection_Sort
    {
        private int[] data;
        private static Random generator = new Random();
        //Create an array of 10 random numbers
           public Selection_Sort(int size)
        {
            data = new int[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = generator.Next(20, 90);
            }
        }

        public void Sort()
        {
            Console.Write("\nSorted Array Elements :(Step by Step)\n\n");
			display_array_elements();
			int smallest; 
            for (int i = 0; i < data.Length - 1; i++)
            {
                smallest = i;

                for (int index = i + 1; index < data.Length; index++)
                {
                    if (data[index] < data[smallest])
                    {
                        smallest = index;
                    }
                }
                Swap(i, smallest);
                display_array_elements();
		   }
            	
        }

        public void Swap(int first, int second)
        {
            int temporary = data[first];    
            data[first] = data[second];  
            data[second] = temporary;  
        }  

        public void display_array_elements()
        {        
            foreach (var element in data)
            {
                Console.Write(element + " ");
            }
            Console.Write("\n\n");
        }
    }
}

Sample Output:

Sorted Array Elements :(Step by Step)                                                                         
                                                                                                              
50 70 50 80 89 25 78 58 83 73                                                                                 
                                                                                                              
25 70 50 80 89 50 78 58 83 73                                                                                 
                                                                                                              
25 50 70 80 89 50 78 58 83 73                                                                                 
                                                                                                              
25 50 50 80 89 70 78 58 83 73                                                                                 
                                                                                                              
25 50 50 58 89 70 78 80 83 73                                                                                 
                                                                                                              
25 50 50 58 70 89 78 80 83 73                                                                                 
                                                                                                              
25 50 50 58 70 73 78 80 83 89                                                                                 
                                                                                                              
25 50 50 58 70 73 78 80 83 89                                                                                 

Flowchart:

C# Sharp Searching and Sorting Algorithm Exercises: Selection sort

C# Sharp Practice online:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to sort a list of elements using Radix sort algorithm.
Next: C# Sharp String Exercise

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming