Numbers Programming QuestionsC# program to check if a number is divisible by 2C# program to accept 2 integers and return remainderC# program to calculate factorial without using recursionC# program to print nth number in Fibonacci seriesC# program to swap two numbers without using a temp variableC# program to check if the entered number is Armstrong numberC# program to find GCD and LCMC# program to check if a number is prime or notC# program to check if a number is Palindromic or notC# program to determine total ways stairs can be climbedC# program to solve FizzBuzz problemC# program to display factor of entered numberPatterns Programming QuestionsC# program to print star triangleC# program to print star triangleC# program to print star triangleC# program to print star diamondC# program to print star M patternC# program to print number triangle-1C# program to print number triangle-2C# program to print number triangle-3C# program to print number triangle-4C# program to print number triangle-5C# program to print number patternC# program to print number diamondC# program to print alphabet patternStrings Programming QuestionsC# program to print duplicate characters in a StringC# program to check if two Strings are anagrams of each otherC# program to reverse String by using Iteration and RecursionC# program to count number of words in a StringC# program to check if String is PalindromeC# program to remove duplicate characters from StringC# program to return highest occurred character in a StringC# program to determine if the string has all unique charactersC# program to replace all spaces in a string with %20C# program to find all substring in a stringGiven a string containing just the characters (, ), {, }, [ and ], determine if the input string is validGiven two words, beginWord and endWord, and a word list, find the length of the shortest transformation sequence from beginWord to endWordRecursion Programming QuestionsC# program to find factorial of a number using recursionC# program to find the sum of digits of a number using recursionC# program to calculate Power of a number using recursionC# program to form the Fibonacci series using recursionC# program to find GCD using recursionC# program to convert a number from Decimal to Binary using recursionC# program to reverse a LinkedList using recursionC# program to do a recursive binary search in an arrayC# program to write recursive Quicksort algorithmC# program to print a singly linked list backwards using recursionC# program to Towers of Hanoi using recursionC# program to print table of any given number by using recursionArray Programming QuestionsC# program to sort an array in ascending orderC# program to sort an array in descending orderC# program to reverse an arrayC# program to find majority element in an unsorted arrayC# program to find missing number in integer array of 1 to 20C# program to merge two sorted arrays into oneC# program to swap min and max element in integer arrayC# program to determine if any two integers in array sum to given integerC# program to check if array contains a duplicate numberC# program to rotate array to a given pivotC# program to move zeros to end of arrayC# program to find the longest common prefix string amongst an array of stringsC# program to find majority number which appears more than 50% in the unsorted arrayData structures Programming QuestionsC# program to reverse a LinkedlistC# program to find node in LinkedlistC# program to merge two sorted LinkedlistC# program to traverse singly LinkedListC# program to traverse circular singly LinkedListC# program to remove duplicates from a sorted LinkedlistC# program to find nth to last element in singly LinkedlistC# program to delete nth element from headnodeC# program to detect a cycle in LinkedlistC# program to implement binary search treeC# program to implement binary search tree TraversalC# program to find min and max in binary search treeC# program to delete nodes from binary search treeC# program to Breadth First Search (BFS)C# program to Depth First Search (DFS)C# program to implement stackC# program to stack with Push & Pop operationC# program to reverse a StackSorting AlgorithmsSelection Sort Algorithm in C#Insertion Sort Algorithm in C#Heap Sort Algorithm in C#Merge Sort Algorithm in C#Quick Sort Algorithm in C#Bubble Sort Algorithm in C#Shell Sort Algorithm in C#Comb Sort Algorithm in C#Bucket Sort Algorithm in C#Radix Sort Algorithm in C#Searching AlgorithmsLinear or Sequntial Search AlgorithmBinary Search AlgorithmLinear vs Binary Search AlgorithmInterpolation Search AlgorithmInterpolation vs Binary Search AlgorithmTernary Search AlgorithmWhy is Binary Search preferred over Ternary Search?Jump Search AlgorithmExponential Search Algorithm

Selection sort program in C#

Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and places it at the beginning of the sorted part. It works by dividing the array into two parts: the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty, and the entire array is unsorted.

Here's how the selection sort algorithm works step by step:
  1. Find the minimum element in the unsorted part of the array.
  2. Swap the minimum element with the leftmost element of the unsorted part (i.e., the first element of the unsorted part becomes the minimum element).
  3. Expand the sorted part by one element (i.e., move the boundary between the sorted and unsorted parts one element to the right).
  4. Repeat steps 1-3 until the entire array is sorted.

Here's an example implementation of selection sort in C#:


public class SelectionSort
{
    public static void Main()
    {
        int[] arr = { 64, 25, 12, 22, 11 };

        Console.WriteLine("Original array:");
        PrintArray(arr);

        Sort(arr);

        Console.WriteLine("Sorted array:");
        PrintArray(arr);
    }
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        // One by one move the boundary of the unsorted subarray
        for (int i = 0; i < n - 1; i++)
        {
            // Find the minimum element in the unsorted part of the array
            int minIndex = i;
            for (int j = i + 1; j < n; j++)
            {
                if (arr[j] < arr[minIndex])
                {
                    minIndex = j;
                }
            }

            // Swap the minimum element with the leftmost element of the unsorted part
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}

Now, let's consider an example to see how selection sort works. Suppose we have the following array of integers:



int[] arr = { 64, 25, 12, 22, 11 };

The step-by-step execution of the selection sort algorithm on this array would be as follows:

  1. Initially, the array is: [64, 25, 12, 22, 11]. The first element (64) is considered as the minimum.
  2. The minimum element (11) is found at index 4. Swap it with the first element. The array becomes: [11, 25, 12, 22, 64].
  3. The sorted part is expanded, and the second element (25) is considered as the minimum.
  4. The minimum element (12) is found at index 2. Swap it with the second element. The array becomes: [11, 12, 25, 22, 64].
  5. The sorted part is expanded, and the third element (25) is considered as the minimum.
  6. The minimum element (22) is found at index 3. Swap it with the third element. The array becomes: [11, 12, 22, 25, 64].
  7. The sorted part is expanded, and the fourth element (25) is considered as the minimum.
  8. The fourth element (25) is already the minimum. No swap is required.
  9. The sorted part is expanded, and the fifth element (64) is considered as the minimum.
  10. The fifth element (64) is already the minimum. No swap is required.

In this program, the Sort method implements the selection sort algorithm. The Main method initializes an array, calls the Sort method to sort the array, and then prints both the original and sorted arrays using the PrintArray method.
When you run the program, it will output:



Original array:
64 25 12 22 11 
Sorted array:
11 12 22 25 64 

Selection sort has a time complexity of O(n^2) in all cases, where n is the number of elements in the array. Although it is an inefficient algorithm for large amount of data.