Home  >  Article  >  Backend Development  >  How to implement the selection sort algorithm in C#

How to implement the selection sort algorithm in C#

PHPz
PHPzOriginal
2023-09-20 13:33:391159browse

How to implement the selection sort algorithm in C#

How to implement the selection sort algorithm in C

#Selection Sort (Selection Sort) is a simple and intuitive sorting algorithm. Its basic idea is to start from the to-be-sorted order every time Select the smallest (or largest) element among the elements and put it at the end of the sorted sequence. Repeat this process until all elements are sorted.

Let’s learn more about how to implement the selection sort algorithm in C#, and attach specific code examples.

  1. Create a selection sort method
    First, we need to create a method for implementing selection sort. This method accepts an integer array as a parameter and returns an ordered integer array.
public static int[] SelectionSort(int[] arr)
{
    int n = arr.Length;

    // 遍历数组
    for (int i = 0; i < n - 1; i++)
    {
        int minIndex = i;

        // 在未排序部分中找到最小元素的索引
        for (int j = i + 1; j < n; j++)
        {
            if (arr[j] < arr[minIndex])
            {
                minIndex = j;
            }
        }

        // 将最小元素与未排序部分的第一个元素交换位置
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }

    return arr;
}
  1. Calling the selection sort method
    Next, we can create a sample program that calls the selection sort method to sort an array.
class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 64, 25, 12, 22, 11 };
        
        Console.WriteLine("原始数组:");
        PrintArray(arr);

        // 调用选择排序方法对数组进行排序
        int[] sortedArr = SelectionSort(arr);

        Console.WriteLine("排序后的数组:");
        PrintArray(sortedArr);
    }

    // 打印数组
    static void PrintArray(int[] arr)
    {
        foreach (int element in arr)
        {
            Console.Write(element + " ");
        }
        Console.WriteLine();
    }
}

In the above sample program, we used an array containing 5 integers as an example of sorting. First, we print out the original array, then call the selection sort method to sort, and finally print the sorted array.

  1. Run the program
    Now, we can run the program and see the specific implementation of the selection sort algorithm.
原始数组:
64 25 12 22 11 
排序后的数组:
11 12 22 25 64 

By running the program, you can see that the original array has become an ordered array after being processed by the selection sort algorithm.

Summary:
Selection sort is a simple but inefficient sorting algorithm. Its time complexity is O(n^2) and is suitable for sorting smaller-scale arrays. In practical applications, we can use more efficient sorting algorithms to replace selection sorting, such as quick sort, merge sort, etc.

I hope the introduction and code examples of this article can help everyone better understand and apply the selection sort algorithm.

The above is the detailed content of How to implement the selection sort algorithm in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn